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() the stdin file descriptor (to avoid clashing with other dart:io functionality that assumes stdin fd 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 (return null)
This would give applications the option to manually call tryReadKey() until it returns null and only ask for cursorPosition afterwards.
If seems that accessing
Console.cursorPositionis implemented via writing escape sequences tostdoutand waiting instdinto 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
cursorPositionwhen there's remaining data to be read. This will cause thecursorPositionto 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()If
stdinsupports non-blocking IO, one coulddup2()thestdinfile descriptor (to avoid clashing with otherdart:iofunctionality that assumesstdinfd is blocking)Key), or not if no data is available (returnnull)This would give applications the option to manually call
tryReadKey()until it returnsnulland only ask forcursorPositionafterwards.