Skip to content
New issue

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

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

Already on GitHub? # to your account

Should not use colored output unless in a TTY #1

Open
adnelson opened this issue Jul 10, 2015 · 0 comments
Open

Should not use colored output unless in a TTY #1

adnelson opened this issue Jul 10, 2015 · 0 comments

Comments

@adnelson
Copy link
Contributor

LSI should not be inserting terminal control codes (i.e. ANSI color codes) unless it is printing to a TTY. When stdout is redirected (i.e. piping to grep, awk, etc, or to a file), then LSI should not insert terminal control codes in its output.

Background info about terminal control codes / ANSI escape sequences:

Some commands that demonstate current LSI behavior:

# display in terminal the fourth column of every EC2 instance returned by this LSI query
$ lsi [query filter] | awk '{ print $4 }'
# redirect stdout of previous command to a file
$ lsi [query filter] | awk '{ print $4 }' > foo.txt
# terminal control codes appear in stdout file contents
$ vim foo.txt
# terminal control codes are included in file contents but aren't displayed when printed to terminal
$ cat foo.txt

We want to emulate grep's --color=auto option, which is smart about when to inject terminal control codes in the output:

# ANSI control codes are evaluated and output is colored when stdout is a terminal
$ echo 'hello world!' | grep --color=auto hello
# ANSI control codes are not included in output sequence when stdout is redirected
$ echo 'hello world!' | grep --color=auto hello > hello.txt
$ vim hello.txt

Suggestion: Instead of unconditionally formatting LSI output with ANSI color codes, we can use the following to check whether stdout is a TTY before doing so:
sys.stdout.isatty()
sys.stderr.isatty()

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

No branches or pull requests

1 participant