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

ANSI escape codes appearing in piped output #21

Closed
keeperofdakeys opened this issue Feb 13, 2016 · 8 comments
Closed

ANSI escape codes appearing in piped output #21

keeperofdakeys opened this issue Feb 13, 2016 · 8 comments
Assignees
Milestone

Comments

@keeperofdakeys
Copy link

I'm using this library for one of my programs, but I'm getting ANSI escape codes when piping the output (IE: The output isn't to a terminal). From a quick look at the code, there is a terminal detection feature. So I'm not sure why this is happening.

For reference, I'm on linux, using rxvt-unicode as my terminal And if it helps, here is where I'm calling your library - https://github.com/keeperofdakeys/Process-Query/blob/master/src/psq/main.rs#L21.

@phsym phsym self-assigned this Feb 13, 2016
@phsym
Copy link
Owner

phsym commented Feb 13, 2016

Hi again !

Humm, I know the term library detects if the current terminal supports unicode, but I'm not sure it's aware you are piping the output to another program. I'll investigate into this tomorrow.

@keeperofdakeys
Copy link
Author

I've had a quick look at the term library you're using, and it looks like it just checks for the TERM environment variable, and has no 'isatty' checks on stdout. There is a bug along the same lines there already Stebalien/term#54 too.

I'm not sure if windows would suffer from the same problem.

@phsym
Copy link
Owner

phsym commented Feb 14, 2016

I can use atty crate to detect if the stdout is a tty or not (in the printstd method), and use term to colorize only if it is. I tried, and colors are not displayed when redirecting the output (in a file or piped to another program).

However, there is some cases where colorizing when output is redirected may be needed. Eg, when piping to more or less -R, but I don't know any way to detect we are doing this. So I could maybe add a boolean argument to printstd to force colorizing even if not attached to a tty stdout.

What do you think about that ?

Windows would not suffer from this (at least in the standard msdos-like sh*tty terminal) since colors are not rendered with ANSI characters, but with windows system calls instead.

@keeperofdakeys
Copy link
Author

Thanks for that, this seems to fix the issue. Having a flag for printstd is probably the best you can do, I don't know of anyway around this - most unix commands have to do this as well though.

@phsym
Copy link
Owner

phsym commented Feb 15, 2016

Great. Instead of adding an argument flag to printstd() and break compatibility, I created print_tty(bool) method. printstd() now simply calls print_tty(false) and does not enforce colorization.

@keeperofdakeys
Copy link
Author

Just FYI, I added a comment to the upstream bug, since this seems like something that the term library should tell you Stebalien/term#54 (comment).

@phsym
Copy link
Owner

phsym commented Feb 15, 2016

👍

@phsym phsym closed this as completed in #23 Feb 15, 2016
@phsym
Copy link
Owner

phsym commented Feb 15, 2016

Fixed published on crates.io in v0.6.1

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

No branches or pull requests

2 participants