Skip to content

Instantly share code, notes, and snippets.

@scottchiefbaker
Created October 18, 2013 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottchiefbaker/7046044 to your computer and use it in GitHub Desktop.
Save scottchiefbaker/7046044 to your computer and use it in GitHub Desktop.
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