Re: [RFC] Io\Terminal

From: Date: Tue, 01 Dec 2026 10:53:44 +0000
Subject: Re: [RFC] Io\Terminal
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-133273@lists.php.net to get a copy of this message
Hi Tim, On 2026-12-01 09:58, Tim Düsterhus wrote: > I was quite surprised to see the RFC text completely rewritten in 0.7 > ( https://wiki.php.net/rfc/io_terminal?do=diff&rev2%5B0%5D=1794200215&rev2%5B1%5D=1795177398&difftype=sidebyside ). Sorry for the surprise. I edited the prose across the page during the v0.7 update to make it clearer and should have noted that in the announcement. The semantic changes are only the ones listed in the changelog. > (1) > Is there always a "unique byte sequence" for every possible key in the > Key enum? If so it might be a rare case where making the enum a > backed > enum might be warranted to enable cleaner compatibility with the enum > changing, because users could retrieve the original byte sequence from > each enum case to compare it. No, there is no 1:1 mapping between Key cases and byte sequences: * On POSIX, several keys map from multiple sequences depending on terminal and mode (for example, Key::Up can be "\x1b[A" or "\x1bOA", Key::F1 through Key::F4 have both SS3 and CSI forms, Key::Backspace is 0x7f or 0x08, and Key::Enter is "\r" or "\n"). * Key::Resize has no byte sequence at all (SIGWINCH on POSIX, WINDOW_BUFFER_SIZE_EVENT on Windows). * Native Windows consoles deliver KEY_EVENT records rather than VT byte sequences. Picking a single canonical backing string per case would not reliably match what a given terminal sent and would not cover Key::Resize, so Key should stay a unit enum. > (2) > Writing out the example also made me realize that there is no explicit > example for readKey() returning sequences, or rather no > dedicated > example for readKey() at all. [...] In particular it is not > clear to > me whether the sequence timeout of readKey() is only relevant > for the > keys in the Key enum or if it can also return strings with > length > greater than one. $sequenceTimeout applies to escape sequence decoding in general, not only to recognized Key cases. After an initial Escape byte arrives, the decoder waits within $sequenceTimeout (capped by $timeout) to collect the sequence. If it matches a Key case, that case is returned; otherwise the collected sequence is returned as a string (such as "\x1b[1;5D"). readKey() can also return multi-byte strings for multi-byte UTF-8 characters and for an incomplete escape prefix when the sequence timeout expires. I will add a dedicated readKey() example and state this explicitly in the RFC. > (3) > It is not clear to me why readLine() returns > null on EOF instead of > throwing. This is inconsistent with readSecret() as the closest > relative (since it also reads entire lines), and breaks the pattern in > the try-catch example in the "Error Model" section. Two reasons: * readKey() and readSecret() both take a $timeout where null already means the timeout expired, so EOF has to throw EofException to stay distinct. readLine() has no timeout, so null is unambiguous. * In canonical mode on POSIX, EOF is not only a closed terminal; pressing Ctrl+D on an empty line or reaching the end of a piped stream also produces EOF, and while (($line = $terminal->readLine()) !== null) is the usual loop pattern for line reads. That said, I see the consistency argument for making all three read*() methods throw EofException on EOF (so readLine() returns non-nullable string and a single try/catch covers mixed reads). If you still prefer throwing EofException from readLine() given the Ctrl+D and loop cases, I am fine changing it. > (4) > I'm noting that the voting widget does not render properly. That likely > indicates some Wiki syntax error elsewhere in the text. I found a mismatched heading delimiter (`=== Key cases and compatibility ====`) and will fix the voting widget markup as well when updating the RFC. Regards, Pratik

« previous php.internals (#133273) next »