<?php
class ansi {
public static function set_back_color
($color_name, $return = FALSE) { 'black' => '40',
'red' => '41',
'green' => '42',
'yellow' => '43',
'blue' => '44',
'magenta' => '45',
'cyan' => '46',
'white' => '47',
);
if ('bright' == substr($color_name, 0, 6)) { $suffix = ';1';
$color_name = substr($color_name, 6); } elseif ('dim' == substr($color_name, 0, 3)) { $suffix = ';2';
$color_name = substr($color_name, 3); } else {
$suffix = FALSE;
};
$result = sprintf(chr(27).'[%sm', $colors[$color_name].$suffix);
if ((bool) $return)
return $result;
else
}
public static function set_fore_color
($color_name, $return = FALSE) { 'reset' => '0',
'black' => '30',
'red' => '31',
'green' => '32',
'yellow' => '33',
'blue' => '34',
'magenta' => '35',
'cyan' => '36',
'white' => '37',
);
if ('bright' == substr($color_name, 0, 6)) { $suffix = ';1';
$color_name = substr($color_name, 6); } elseif ('dim' == substr($color_name, 0, 3)) { $suffix = ';2';
$color_name = substr($color_name, 3); } else {
$suffix = FALSE;
};
$result = sprintf(chr(27).'[%sm', $colors[$color_name].$suffix);
if ((bool) $return)
return $result;
else
}
public static function set_color
($fore_color = FALSE, $back_color = FALSE, $return = FALSE) { if (FALSE === $fore_color AND FALSE === $back_color) {
return self::set_fore_color('reset', $return);
} else {
$result = '';
if ((bool) $fore_color)
$result .= self::set_fore_color($fore_color, $return);
if ((bool) $back_color)
$result .= self::set_back_color($back_color, $return);
return $result;
}
}
public static function cprintf
($fore_color = FALSE, $back_color = FALSE, $format) { // Get the args after $format for the vprintf call.
self::set_color($fore_color, $back_color);
self::set_color();
}
public static function csprintf
($fore_color = FALSE, $back_color = FALSE, $format) { // Get the args after $format for the vprintf call.
$result = self::set_color($fore_color, $back_color, TRUE);
$result .= self::set_color(FALSE, FALSE, TRUE);
return $result;
}
}