Skip to content

Core\Cli::ColorDepth

Says how much colour a terminal can show, so a program can pick a palette that will actually arrive.

Core\Cli::ColorDepth is declared in the class Core\Cli. The method colorDepth returns it.

Terminals differ. A log collector or a pipe shows no colour at all, an old console shows the sixteen ANSI colours, most terminals today show a 256-entry palette, and a modern one shows all 24-bit colours. Core\Cli::ColorDepth has one case for each of those — None, Ansi16, Ansi256 and TrueColor — and Core\Cli::colorDepth() answers with the one this run has, after reading NO_COLOR, TERM and the rest.

Most programs never ask. You write styled text and Novis drops or downgrades what the terminal cannot show. Ask when the content changes with the answer: how many shades a chart uses, or whether to draw a picture at all.

Good to know: the cases are listed from least colour to most, so at least this much is a comparison of their numbers — ($have as int) >= ($want as int). A case is a name, not a number, so the conversion is written out.

Examples

How many shades a chart can use

<?nvs
// This program picks how many shades of one colour a heat map uses for each
// `Core\Cli::ColorDepth` case.
class HeatMap
{
public static function shades(Core\Cli::ColorDepth $depth): int
{
return match ($depth) {
Core\Cli::ColorDepth::None => 1,
Core\Cli::ColorDepth::Ansi16 => 4,
Core\Cli::ColorDepth::Ansi256 => 24,
Core\Cli::ColorDepth::TrueColor => 256,
};
}
}
// The cases in the order they are declared: least colour first.
array<Core\Cli::ColorDepth> $everything = [
Core\Cli::ColorDepth::None,
Core\Cli::ColorDepth::Ansi16,
Core\Cli::ColorDepth::Ansi256,
Core\Cli::ColorDepth::TrueColor,
];
foreach ($everything as Core\Cli::ColorDepth $depth) {
echo HeatMap::shades($depth), " shade(s)\n";
}
// `Core\Cli::colorDepth()` returns the colour depth of the current terminal.
// This note goes to standard error, because it is not part of the result.
Core\Cli::write(
'this run has ' . HeatMap::shades(Core\Cli::colorDepth()) . ' shade(s)',
{ stream: Core\Cli::Stream::Err, newline: true },
);
Output
1 shade(s)
4 shade(s)
24 shade(s)
256 shade(s)

At least this much colour

<?nvs
// The `Core\Cli::ColorDepth` cases go from least colour to most. Convert two
// cases to `int` with `as int`, and `>=` checks if one has at least the colour
// of the other.
class Colour
{
public static function atLeast(Core\Cli::ColorDepth $have, Core\Cli::ColorDepth $want): bool
{
return ($have as int) >= ($want as int);
}
}
// Two terminals. The code checks both for at least `Ansi256`.
Core\Cli::ColorDepth $laptop = Core\Cli::ColorDepth::TrueColor;
Core\Cli::ColorDepth $buildLog = Core\Cli::ColorDepth::None;
echo 'laptop has 256: ', Colour::atLeast($laptop, Core\Cli::ColorDepth::Ansi256) ? 'yes' : 'no', "\n";
echo 'build log has 256: ', Colour::atLeast($buildLog, Core\Cli::ColorDepth::Ansi256) ? 'yes' : 'no', "\n";
// `Chart::pick` checks the drawing with the most colour first. It returns the
// first drawing that the terminal can show.
class Chart
{
public static function pick(Core\Cli::ColorDepth $have): string
{
if (Colour::atLeast($have, Core\Cli::ColorDepth::TrueColor)) {
return 'a smooth gradient';
}
if (Colour::atLeast($have, Core\Cli::ColorDepth::Ansi256)) {
return 'twenty-four steps';
}
if (Colour::atLeast($have, Core\Cli::ColorDepth::Ansi16)) {
return 'four blocks';
}
return 'numbers, no colour';
}
}
echo 'laptop draws ', Chart::pick($laptop), "\n";
echo 'build log draws ', Chart::pick($buildLog), "\n";
echo 'old console draws ', Chart::pick(Core\Cli::ColorDepth::Ansi16), "\n";
Output
laptop has 256: yes
build log has 256: no
laptop draws a smooth gradient
build log draws numbers, no colour
old console draws four blocks

Drawing something else when there is no colour

<?nvs
// Ask once, at the start of the run, and hand the answer down. What changes
// with it here is the drawing itself and not the colour of it: a terminal with
// no colour gets a line that still says everything the bar would have said.
class Progress
{
// `$tenths` is how much of the step is done, from 0 to 10.
public static function line(Core\Cli::ColorDepth $depth, string $step, uint $tenths): string
{
uint $percent = $tenths * 10;
if ($depth == Core\Cli::ColorDepth::None) {
return $step . ': ' . $percent . '% done';
}
return $step
. ': '
. Core\Str::repeat('#', $tenths)
. Core\Str::repeat('.', 10 - $tenths)
. ' '
. $percent
. '%';
}
}
array<string> $steps = ['build', 'upload', 'restart'];
array<uint> $done = [10, 6, 0];
// In your program the depth is `Core\Cli::colorDepth()`, read once at the top.
// Both answers are written out here so the two drawings stand side by side.
uint $at = 0;
foreach ($steps as string $step) {
echo Progress::line(Core\Cli::ColorDepth::Ansi256, $step, $done[$at]), "\n";
$at = $at + 1;
}
echo "\n";
$at = 0;
foreach ($steps as string $step) {
echo Progress::line(Core\Cli::ColorDepth::None, $step, $done[$at]), "\n";
$at = $at + 1;
}
Output
build: ########## 100%
upload: ######.... 60%
restart: .......... 0%
build: 100% done
upload: 60% done
restart: 0% done