| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | namespace Kint\Renderer; |
| 27: | |
| 28: | use Kint\Object\BasicObject; |
| 29: | |
| 30: | class CliRenderer extends TextRenderer |
| 31: | { |
| 32: | |
| 33: | |
| 34: | |
| 35: | public static $cli_colors = true; |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | public static $force_utf8 = false; |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | public static $detect_width = true; |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | public static $min_terminal_width = 40; |
| 59: | |
| 60: | protected static $terminal_width = null; |
| 61: | |
| 62: | protected $windows_output = false; |
| 63: | |
| 64: | protected $colors = false; |
| 65: | |
| 66: | public function __construct() |
| 67: | { |
| 68: | parent::__construct(); |
| 69: | |
| 70: | if (!self::$force_utf8) { |
| 71: | $this->windows_output = KINT_WIN; |
| 72: | } |
| 73: | |
| 74: | if (!self::$terminal_width) { |
| 75: | if (!KINT_WIN && self::$detect_width) { |
| 76: | self::$terminal_width = \exec('tput cols'); |
| 77: | } |
| 78: | |
| 79: | if (self::$terminal_width < self::$min_terminal_width) { |
| 80: | self::$terminal_width = self::$default_width; |
| 81: | } |
| 82: | } |
| 83: | |
| 84: | $this->colors = $this->windows_output ? false : self::$cli_colors; |
| 85: | |
| 86: | $this->header_width = self::$terminal_width; |
| 87: | } |
| 88: | |
| 89: | public function colorValue($string) |
| 90: | { |
| 91: | if (!$this->colors) { |
| 92: | return $string; |
| 93: | } |
| 94: | |
| 95: | return "\x1b[32m".\str_replace("\n", "\x1b[0m\n\x1b[32m", $string)."\x1b[0m"; |
| 96: | } |
| 97: | |
| 98: | public function colorType($string) |
| 99: | { |
| 100: | if (!$this->colors) { |
| 101: | return $string; |
| 102: | } |
| 103: | |
| 104: | return "\x1b[35;1m".\str_replace("\n", "\x1b[0m\n\x1b[35;1m", $string)."\x1b[0m"; |
| 105: | } |
| 106: | |
| 107: | public function colorTitle($string) |
| 108: | { |
| 109: | if (!$this->colors) { |
| 110: | return $string; |
| 111: | } |
| 112: | |
| 113: | return "\x1b[36m".\str_replace("\n", "\x1b[0m\n\x1b[36m", $string)."\x1b[0m"; |
| 114: | } |
| 115: | |
| 116: | public function renderTitle(BasicObject $o) |
| 117: | { |
| 118: | if ($this->windows_output) { |
| 119: | return $this->utf8ToWindows(parent::renderTitle($o)); |
| 120: | } |
| 121: | |
| 122: | return parent::renderTitle($o); |
| 123: | } |
| 124: | |
| 125: | public function preRender() |
| 126: | { |
| 127: | return PHP_EOL; |
| 128: | } |
| 129: | |
| 130: | public function postRender() |
| 131: | { |
| 132: | if ($this->windows_output) { |
| 133: | return $this->utf8ToWindows(parent::postRender()); |
| 134: | } |
| 135: | |
| 136: | return parent::postRender(); |
| 137: | } |
| 138: | |
| 139: | public function escape($string, $encoding = false) |
| 140: | { |
| 141: | return \str_replace("\x1b", '\\x1b', $string); |
| 142: | } |
| 143: | |
| 144: | protected function utf8ToWindows($string) |
| 145: | { |
| 146: | return \str_replace( |
| 147: | array('┌', '═', '┐', '│', '└', '─', '┘'), |
| 148: | array("\xda", "\xdc", "\xbf", "\xb3", "\xc0", "\xc4", "\xd9"), |
| 149: | $string |
| 150: | ); |
| 151: | } |
| 152: | } |
| 153: | |