I’m creating custom dotfiles (.bash_profile
and the like) for use with OS X’s Terminal.app and iTerm.app.
I’d like to configure some settings conditionally, based on whether the active application supports 256 colors or not. (iTerm.app supports it, but Terminal.app doesn’t.) This would be possible using an if
statement (pseudo-code):
if 256_COLOR_SUPPORT; then
# Make use of all available colors (iTerm.app)
else
# Fall back to 16-color mode (Terminal.app)
fi
The problem is I don’t know how to write the condition for the if
statement.
Hence my question: is it possible to detect 256 color support? If so, how?
Update: I found out how to detect which terminal application I’m currently in:
iTerm.app:
$ echo $TERM_PROGRAM
iTerm.app
Terminal.app:
$ echo $TERM_PROGRAM
Apple_Terminal
So for now, I’m using the following check:
if [ $TERM_PROGRAM == "iTerm.app" ]; then
# Groovy 256-color stuff
fi
I’d still like to know if it’s possible to detect the number of available colors in the active application though. I tried tput colors
but that seems useless since Terminal.app lies and returns 256
.