The idea is for my application to know not to color the output if the program can't print, say, logging output from through a cron job to a file, no need to log colored output, but when running manually, i like to view the output colored.
For this use case, what programs typically do (e.g. GNU ls or GNU grep with
--color=auto) is to use colors if their output is going to a terminal, and no colors otherwise. Terminals that don't support ANSI color-changing sequences are rare enough that it's acceptable to make their users override the default choice. In any case, make sure your application has an option to force colors on or off.
In a shell script, use
[ -t 1 ] to test if standard output is a terminal.
# option processing has set $color to yes, no or auto
if [ $color = auto ]; then
if [ -t 1 ]; then color=yes; else color=no; fi
From a program using the C API, call
# option processing has set use_color to 0 for no, 1 for yes or 2 for auto
if (use_color == 2) use_color = isatty(1);