diff --git a/includes/kernel.php b/includes/kernel.php index cb1279f8..2e89b24d 100644 --- a/includes/kernel.php +++ b/includes/kernel.php @@ -12,9 +12,12 @@ else mb_internal_encoding('UTF-8'); - +// OS_WIN as per compile info of php define('OS_WIN', substr(PHP_OS, 0, 3) == 'WIN'); +// WIN10 and later support ANSI escape sequences +define('CLI_HAS_E', !OS_WIN || version_compare(php_uname('r'), '10.0') >= 0); + require_once 'includes/defines.php'; require_once 'includes/libs/DbSimple/Generic.php'; // Libraray: http://en.dklab.ru/lib/DbSimple (using variant: https://github.com/ivan1986/DbSimple/tree/master) diff --git a/includes/utilities.php b/includes/utilities.php index 8e1a2197..5fbac5f4 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -171,7 +171,7 @@ abstract class CLI $nCols = count($row); for ($j = 0; $j < $nCols - 1; $j++) // don't pad last column - $pads[$j] = max($pads[$j], mb_strlen($row[$j])); + $pads[$j] = max($pads[$j] ?? 0, mb_strlen($row[$j])); } self::write(); @@ -214,27 +214,27 @@ abstract class CLI public static function red(string $str) : string { - return OS_WIN ? $str : "\e[31m".$str."\e[0m"; + return CLI_HAS_E ? "\e[31m".$str."\e[0m" : $str; } public static function green(string $str) : string { - return OS_WIN ? $str : "\e[32m".$str."\e[0m"; + return CLI_HAS_E ? "\e[32m".$str."\e[0m" : $str; } public static function yellow(string $str) : string { - return OS_WIN ? $str : "\e[33m".$str."\e[0m"; + return CLI_HAS_E ? "\e[33m".$str."\e[0m" : $str; } public static function blue(string $str) : string { - return OS_WIN ? $str : "\e[36m".$str."\e[0m"; + return CLI_HAS_E ? "\e[36m".$str."\e[0m" : $str; } public static function bold(string $str) : string { - return OS_WIN ? $str : "\e[1m".$str."\e[0m"; + return CLI_HAS_E ? "\e[1m".$str."\e[0m" : $str; } public static function write(string $txt = '', int $lvl = self::LOG_BLANK, bool $timestamp = true, bool $tmpRow = false) : void @@ -267,7 +267,7 @@ abstract class CLI $msg .= $txt; } - $msg = (self::$overwriteLast && !OS_WIN ? "\e[1G\e[0K" : "\n") . $msg; + $msg = (self::$overwriteLast && CLI_HAS_E ? "\e[1G\e[0K" : "\n") . $msg; self::$overwriteLast = $tmpRow; echo $msg;