Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing cursorPosition may print debugging information #58

Open
mkustermann opened this issue Nov 24, 2022 · 0 comments
Open

Accessing cursorPosition may print debugging information #58

mkustermann opened this issue Nov 24, 2022 · 0 comments

Comments

@mkustermann
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant