Skip to content

Accessing cursorPosition may print debugging information #58

@mkustermann

Description

@mkustermann

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions