-
Notifications
You must be signed in to change notification settings - Fork 48
Description
If seems that accessing Console.cursorPosition is implemented via writing escape sequences to stdout and waiting in stdin to receive the answer. That may be inherent in how terminals work.
That poses a problem when pasting longer text into the command prompt, since one may access cursorPosition when there's remaining data to be read. This will cause the cursorPosition to get into bad state (probably because it sees this other pasted - but not-yet-read data instead of the answer from cursor-position query).
It's hard to work around this problem with the current API, since one doesn't know whether there's data available before accessing cursorPosition.
One suggestion I have is to provide a tryReadKey()
class Console {
Key readKey() { ... }
Key? tryReadKey() { ... }
}If stdin supports non-blocking IO, one could
dup2()thestdinfile descriptor (to avoid clashing with otherdart:iofunctionality that assumesstdinfd is blocking)- make the copied file descriptor non-blocking
- try to read data: which may be successful if there's data available (return
Key), or not if no data is available (returnnull)
This would give applications the option to manually call tryReadKey() until it returns null and only ask for cursorPosition afterwards.