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

stderred doesn't handle ANSI escape codes split on multiple buffers #87

Open
peoro opened this issue Dec 27, 2022 · 0 comments
Open

stderred doesn't handle ANSI escape codes split on multiple buffers #87

peoro opened this issue Dec 27, 2022 · 0 comments

Comments

@peoro
Copy link

peoro commented Dec 27, 2022

Issue

Take a program like this, which writes an ANSI escape code to stderr, flushing the buffer after each character is written:

import sys;
for c in "\033[;1mHi!\n":
  sys.stderr.write( c )
  sys.stderr.flush()

Stderr doesn't handle it properly. It tries to color every single character in red, but this breaks the original escape code.

With the program above, stderr prints "[;1mHi!" (in red):

$ python -c 'for c in "\\033[;1mHi!\n": import sys; sys.stderr.write(c); sys.stderr.flush()'
[;1mHi!

An explicit flush isn't even needed: writing the string character by character on an unbuffered stderr has the same effect:

$ python -uc 'for c in "\\033[;1mHi!\n": import sys; sys.stderr.write(c)'
[;1mHi!

Example in the wild

I ran into this issue while using GHCi, the Haskell's REPL. This is what you get when you input something wrong while stderred is loded:

ghci> variableNotInScope
[;1m<interactive>:3:1: [;1m[31merror:[0m[0m[;1m[0m[0m[;1m Variable not in scope: variableNotInScope[0m[0m

What should happen instead

I'm assuming that it's unnecessary to maintain the original stderr as-is, since stderred transforms it anyways with its own color codes.

Thus I think stderred should either remove all the ANSI escape code and all the non-printable characters (according to the term's character encoding) from the stderr, or otherwise keep the ANSI escape code intact without interlacing them with the stderred's escape code. Both solutions require detecting and buffering the escape codes.

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