-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Labels
Description
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:
- http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/c327.html
- http://wiki.bash-hackers.org/scripting/terminalcodes
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()