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

If stdin is closed cursorPosition throws a RangeError #34

Open
bsutton opened this issue Jun 17, 2021 · 4 comments · Fixed by #36
Open

If stdin is closed cursorPosition throws a RangeError #34

bsutton opened this issue Jun 17, 2021 · 4 comments · Fixed by #36

Comments

@bsutton
Copy link
Collaborator

bsutton commented Jun 17, 2021

if stdin is closed (seems to be within docker)
hen the call to Console.cursorPosition will fail.
RangeError: Invalid value: Not in inclusive range 0..1114111: -1
new String.fromCharCode (dart:core-patch/string_patch.dart:45)
Console.cursorPosition (package:dart_console/src/console.dart:304)

looks like the results from the read need to be checked for -1 (end of file) before trying to decode as a char code.

@timsneath timsneath linked a pull request Aug 10, 2021 that will close this issue
@timsneath
Copy link
Owner

Thanks! Fixed by exclusion. Finding cursor position requires writing an ANSI character code to stdout and then reading the response to stdin, so if the latter isn't available, the call will fail. That seems to be a limitation of GitHub Actions, so I've edited the tests to require a working stdin/stdout. Was there something else you were looking at other than the tests?

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 10, 2021

This is a broader problem than just the unit tests.
I'm using the cursor position in production code.
In some instances the production code runs without a head (e.g. docker).
I would be nice if the code behaved in a more consistent manner when running headless.

I'm a little unsure just what the correct action is.
I think there are two components.

  1. discoverability
    How can a user check/know that the function will work.
  2. what should the function do if called without the pre-conditions being met.

There is probably a reasonable argument that this can be done by documenting the limitations on the method call and then throwing an appropriate exception such as:

throw IllegalState('cursor position not supported when not attached to a console');

Perhaps also a method such as 'isAttached' so that the user can easily discover the state without having to work out stdin and stdout.

@timsneath
Copy link
Owner

Yeah, there's certainly the opportunity to detect whether the console is running headless or not. I'm wondering how other tools do it.

Help me understand the scenario: is it something like a progress bar, where you want to keep rewriting the same line? Looking at our own tools (e.g. dart test), I see we just spew out multiple lines in that scenario. There may be a variant of the method that resets to the home column even if it's not a terminal that supports ANSI escape codes.

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 10, 2021 via email

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

Successfully merging a pull request may close this issue.

2 participants