src.dualinventive.com/dinet/common/ditest-php/DiTestColor.php

69 lines
1.8 KiB
PHP

<?php
class DiTestColor {
protected static $ANSI_CODES = array(
"off" => 0,
"bold" => 1,
"italic" => 3,
"underline" => 4,
"blink" => 5,
"inverse" => 7,
"hidden" => 8,
"black" => 30,
"red" => 31,
"green" => 32,
"yellow" => 33,
"blue" => 34,
"magenta" => 35,
"cyan" => 36,
"white" => 37,
"black_bg" => 40,
"red_bg" => 41,
"green_bg" => 42,
"yellow_bg" => 43,
"blue_bg" => 44,
"magenta_bg" => 45,
"cyan_bg" => 46,
"white_bg" => 47
);
public static function set($str, $color) {
if (isset($_ENV['DI_TEST_COLOR']))
$usecolor = $_ENV['DI_TEST_COLOR'] === 'true' ? true : false;
else
$usecolor = true;
if (!posix_isatty(STDOUT) && $usecolor == false) {
return $str;
}
$color_attrs = explode("+", $color);
$ansi_str = "";
foreach ($color_attrs as $attr) {
$ansi_str .= "\033[" . self::$ANSI_CODES[$attr] . "m";
}
$ansi_str .= $str . "\033[" . self::$ANSI_CODES["off"] . "m";
return $ansi_str;
}
public static function log($message, $color)
{
error_log(self::set($message, $color));
}
public static function replace($full_text, $search_regexp, $color)
{
$new_text = preg_replace_callback(
"/($search_regexp)/",
function ($matches) use ($color) {
return Color::set($matches[1], $color);
},
$full_text
);
return is_null($new_text) ? $full_text : $new_text;
}
}