ubuntu-update.sh icon indicating copy to clipboard operation
ubuntu-update.sh copied to clipboard

Only use colors in a TTY

Open AlexanderOtavka opened this issue 10 months ago • 0 comments

Summary

Right now, the script always uses colorized output. This is a nice user experience when run interactively in the terminal, but isn't so nice when printed to a log file and those pretty colors become ugly escape sequences. This PR preserves the colors when output is connected to a terminal, but removes them when output is piped somewhere else. The behavior is similar to tools like ls and grep --color=auto.

Manual Testing

Tested on Ubuntu 22.04.4 LTS.

Current Behavior

When running sudo ./ubuntu-update.sh in a terminal, we see pretty colors: Screenshot 2024-04-13 at 3 33 30 PM

But when running sudo ./ubuntu-update.sh > out-before.txt, we get these file contents:

^[[31m
ubuntu-update.sh Copyright (C) 2018 Ted LeRoy

Easily update, upgrade, and clean up your Ubuntu system with this bash script.

This program comes with ABSOLUTELY NO WARRANTY see
https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md

This is free software, and you are welcome to redistribute it
under certain conditions.

See https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md
for details.^[(B^[[0m

^[[32m#############################
#     Updating Data Base    #
#############################^[[0m
...

New Behavior

When running sudo ./ubuntu-update.sh in a terminal, we still see pretty colors: Screenshot 2024-04-13 at 3 41 36 PM

And when running sudo ./ubuntu-update.sh > out-before.txt, we get these nicer, un-colored file contents:

ubuntu-update.sh Copyright (C) 2018 Ted LeRoy

Easily update, upgrade, and clean up your Ubuntu system with this bash script.

This program comes with ABSOLUTELY NO WARRANTY see
https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md

This is free software, and you are welcome to redistribute it
under certain conditions.

See https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md
for details.

#############################
#     Updating Data Base    #
#############################
...

AlexanderOtavka avatar Apr 13 '24 22:04 AlexanderOtavka