Skip to content

Should not use colored output unless in a TTY #1

@adnelson

Description

@adnelson

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()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions