Skip to content

Core\Cli::colorDepth

Says how much colour the terminal can show, so a program can leave colour out where there is none.

Terminals differ. Some show no colour at all, some show the sixteen basic colours, some show 256, and some show every colour a screen has. Where the output goes matters too: text sent to a file or to another program has no colour. Core\Cli::colorDepth returns one of four cases: None, Ansi16, Ansi256 or TrueColor. It reads the settings a person expects it to read, such as NO_COLOR and TERM, and it gives the same answer for the whole run.

Good to know: most programs never ask. When you write a Core\Cli\Text, Novis already leaves the colour out on a terminal that cannot show it. You ask when the choice is your program's own, such as picking a different mark or a different drawing.

The examples below print what the terminal can show, pick between two ways of drawing a chart, and write the state as a word where colour cannot carry it.

Return value

Returns Core\Cli::ColorDepth. The depth as a Core\Cli::ColorDepth case. It is None when standard output is not a terminal and no setting forces colour on. So myprog | grep and a CI log get plain text.

Examples

What this terminal can show

<?nvs
// Shows how to ask how much colour the terminal can show. Run it in a terminal
// to see what yours can do. Text sent to a file has no colour, so this prints
// `no colour` here.
Core\Cli::ColorDepth $depth = Core\Cli::colorDepth();
if ($depth == Core\Cli::ColorDepth::None) {
echo "no colour\n";
}
if ($depth == Core\Cli::ColorDepth::Ansi16) {
echo "16 colours\n";
}
if ($depth == Core\Cli::ColorDepth::Ansi256) {
echo "256 colours\n";
}
if ($depth == Core\Cli::ColorDepth::TrueColor) {
echo "every colour\n";
}
Output
no colour

Pick a way of drawing

<?nvs
// Shows how to pick between two ways of drawing a chart. The four cases go up
// from `None` to `TrueColor`. Compare them as numbers to ask whether the
// terminal has at least the colour you want.
int $depth = Core\Cli::colorDepth() as int;
int $shaded = Core\Cli::ColorDepth::Ansi256 as int;
string $chart = 'bars of hash marks';
if ($depth >= $shaded) {
$chart = 'bars in 256 shades';
}
echo 'drawing: ', $chart, "\n";
Output
drawing: bars of hash marks

A word where colour cannot carry it

<?nvs
// Shows the everyday use: a tool that prints the lines a change added and
// removed. A terminal with colour shows them in green and red. Without colour
// the program writes `+` and `-` in front, so the meaning stays.
class Changes
{
public static function show(array<string> $removed, array<string> $added): void
{
// `None` means the text goes somewhere that shows no colour, such as a
// file or another program.
bool $plain = Core\Cli::colorDepth() == Core\Cli::ColorDepth::None;
foreach ($removed as string $line) {
echo $plain ? '-' : ' ', $line, "\n";
}
foreach ($added as string $line) {
echo $plain ? '+' : ' ', $line, "\n";
}
}
}
Changes::show(['price: 11.00'], ['price: 12.50']);
Output
-price: 11.00
+price: 12.50