Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test terminal color detection.
my $depth = terminal_color_depth();
print "My terminal has " . $depth . " colors\n";
sub terminal_color_depth {
# If we're not attached to an STDOUT don't go any further
if (!-t STDOUT) {
return 8;
}
# This is only good on Linux/Mac right now
# Windows will always return 8 colors...
my $cmd = "tput colors"; # FIXME
my $out = int(`$cmd`);
my $exit = $? >> 8;
# If we don't get anything from tput, assume 8 colors
if ($exit != 0 || !$out) {
return 8;
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment