Skip to content

Instantly share code, notes, and snippets.

@okj579
Created January 15, 2019 12:05
Show Gist options
  • Save okj579/532890a80c24982ae9949bcb7ab16cc6 to your computer and use it in GitHub Desktop.
Save okj579/532890a80c24982ae9949bcb7ab16cc6 to your computer and use it in GitHub Desktop.
<?php
function ansiColor($color, $text) {
static $colorDict;
if (!$colorDict) {
$colorDict = array_flip(['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']);
}
$colorVals = [];
foreach ((array)$color as $i => $name) {
$colorVals[$i] = $i ? 40 : 30;
if (substr_compare($name, 'bright', 0, 6) === 0) {
$name = substr($name, 6);
$colorVals[$i] += 60;
}
$colorVals[$i] += $colorDict[$name];
}
return "\e[" . join(';', $colorVals) . "m" . $text . "\e[m";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment