|
| 1 | +<?php |
| 2 | +namespace Olive\Tools; |
| 3 | + |
| 4 | +/** |
| 5 | + * customize https://github.com/kevinlebrun/colors.php/ for Olive |
| 6 | + * color.php license: (The MIT License) Copyright (c) 2018 Kevin Le Brun lebrun.k@gmail.com |
| 7 | + */ |
| 8 | +class ColorConsole |
| 9 | +{ |
| 10 | + // italic and blink may not work depending of your terminal |
| 11 | + private const styles = [ |
| 12 | + 'reset' => '0', |
| 13 | + 'bold' => '1', |
| 14 | + 'dark' => '2', |
| 15 | + 'italic' => '3', |
| 16 | + 'underline' => '4', |
| 17 | + 'blink' => '5', |
| 18 | + 'reverse' => '7', |
| 19 | + 'concealed' => '8', |
| 20 | + 'default' => '39', |
| 21 | + 'black' => '30', |
| 22 | + 'red' => '31', |
| 23 | + 'green' => '32', |
| 24 | + 'yellow' => '33', |
| 25 | + 'blue' => '34', |
| 26 | + 'magenta' => '35', |
| 27 | + 'cyan' => '36', |
| 28 | + 'light_gray' => '37', |
| 29 | + 'dark_gray' => '90', |
| 30 | + 'light_red' => '91', |
| 31 | + 'light_green' => '92', |
| 32 | + 'light_yellow' => '93', |
| 33 | + 'light_blue' => '94', |
| 34 | + 'light_magenta' => '95', |
| 35 | + 'light_cyan' => '96', |
| 36 | + 'white' => '97', |
| 37 | + 'bg_default' => '49', |
| 38 | + 'bg_black' => '40', |
| 39 | + 'bg_red' => '41', |
| 40 | + 'bg_green' => '42', |
| 41 | + 'bg_yellow' => '43', |
| 42 | + 'bg_blue' => '44', |
| 43 | + 'bg_magenta' => '45', |
| 44 | + 'bg_cyan' => '46', |
| 45 | + 'bg_light_gray' => '47', |
| 46 | + 'bg_dark_gray' => '100', |
| 47 | + 'bg_light_red' => '101', |
| 48 | + 'bg_light_green' => '102', |
| 49 | + 'bg_light_yellow' => '103', |
| 50 | + 'bg_light_blue' => '104', |
| 51 | + 'bg_light_magenta' => '105', |
| 52 | + 'bg_light_cyan' => '106', |
| 53 | + 'bg_white' => '107', |
| 54 | + ]; |
| 55 | + |
| 56 | + public static function render($string = '', $option = []) |
| 57 | + { |
| 58 | + if (isset($GLOBALS['OliveConsoleColor']) and $GLOBALS['OliveConsoleColor'] == false) { |
| 59 | + return $string; |
| 60 | + } |
| 61 | + $color = 'default'; |
| 62 | + $background = 'default'; |
| 63 | + $styles = []; |
| 64 | + if (isset($option['color'])) { |
| 65 | + $color = $option['color']; |
| 66 | + } |
| 67 | + if (isset($option['background'])) { |
| 68 | + $background = $option['background']; |
| 69 | + } |
| 70 | + if (isset($option['style'])) { |
| 71 | + $st = $option['style']; |
| 72 | + if (is_array($st)) { |
| 73 | + $styles = $st; |
| 74 | + } else { |
| 75 | + $styles = [$st]; |
| 76 | + } |
| 77 | + } |
| 78 | + if (isset($option['align']) and $option['align'] == 'center') { |
| 79 | + $string = self::center($string); |
| 80 | + } |
| 81 | + $message = self::stylize($color, $string); |
| 82 | + $message = self::stylize('bg_' . $background, $message); |
| 83 | + foreach ($styles as $style) { |
| 84 | + $message = self::stylize($style, $message); |
| 85 | + } |
| 86 | + |
| 87 | + return $message; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns true if the stream supports colorization. |
| 92 | + * |
| 93 | + * Colorization is disabled if not supported by the stream: |
| 94 | + * |
| 95 | + * - Windows without Ansicon, ConEmu or Babun |
| 96 | + * - non tty consoles |
| 97 | + * |
| 98 | + * @return bool true if the stream supports colorization, false otherwise |
| 99 | + * |
| 100 | + * @link https://github.com/symfony/Console/blob/master/Output/StreamOutput.php#L94 |
| 101 | + * @codeCoverageIgnore |
| 102 | + */ |
| 103 | + private static function shouldStylize() |
| 104 | + { |
| 105 | + if (DIRECTORY_SEPARATOR === '\\') { |
| 106 | + return (function_exists('sapi_windows_vt100_support') |
| 107 | + && @sapi_windows_vt100_support(STDOUT)) |
| 108 | + || false !== getenv('ANSICON') |
| 109 | + || 'ON' === getenv('ConEmuANSI') |
| 110 | + || 'xterm' === getenv('TERM'); |
| 111 | + } |
| 112 | + |
| 113 | + if (function_exists('stream_isatty')) { |
| 114 | + return @stream_isatty(STDOUT); |
| 115 | + } |
| 116 | + |
| 117 | + if (function_exists('posix_isatty')) { |
| 118 | + return @posix_isatty(STDOUT); |
| 119 | + } |
| 120 | + |
| 121 | + $stat = @fstat(self::stream); |
| 122 | + // Check if formatted mode is S_IFCHR |
| 123 | + return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; |
| 124 | + } |
| 125 | + |
| 126 | + private static function stylize($style, $text) |
| 127 | + { |
| 128 | + if (! self::shouldStylize()) { |
| 129 | + return $text; |
| 130 | + } |
| 131 | + |
| 132 | + $style = strtolower($style); |
| 133 | + |
| 134 | + if (array_key_exists($style, self::styles)) { |
| 135 | + return self::buildEscSeq(self::styles[$style]) . $text . self::buildEscSeq(self::styles['reset']); |
| 136 | + } |
| 137 | + |
| 138 | + if (preg_match('/^((?:bg_)?)color\[([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\]$/', $style, $matches)) { |
| 139 | + $option = $matches[1] == 'bg_' ? 48 : 38; |
| 140 | + |
| 141 | + return self::buildEscSeq("{$option};5;{$matches[2]}") . $text . self::buildEscSeq(self::styles['reset']); |
| 142 | + } |
| 143 | + |
| 144 | + throw new \Exception("Invalid style $style"); |
| 145 | + } |
| 146 | + |
| 147 | + private static function buildEscSeq($style) |
| 148 | + { |
| 149 | + return sprintf("\033[%sm", $style); |
| 150 | + } |
| 151 | + |
| 152 | + private static function center($text, $width = 80) |
| 153 | + { |
| 154 | + foreach (explode(PHP_EOL, $text) as $line) { |
| 155 | + $line = trim($line); |
| 156 | + $lineWidth = strlen($line) - mb_strlen($line, 'UTF-8') + $width; |
| 157 | + $centered .= str_pad($line, $lineWidth, ' ', STR_PAD_BOTH) . PHP_EOL; |
| 158 | + } |
| 159 | + |
| 160 | + return trim($centered, PHP_EOL); |
| 161 | + } |
| 162 | +} |
0 commit comments