1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected]) |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
9
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
10
|
|
|
* the Software without restriction, including without limitation the rights to |
11
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
12
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
13
|
|
|
* subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in all |
16
|
|
|
* copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
20
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
21
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
22
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
23
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Kint\Renderer; |
27
|
|
|
|
28
|
|
|
use Kint\Object\BasicObject; |
29
|
|
|
|
30
|
|
|
class CliRenderer extends TextRenderer |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var bool enable colors when Kint is run in *UNIX* command line |
34
|
|
|
*/ |
35
|
|
|
public static $cli_colors = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Forces utf8 output on windows. |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
public static $force_utf8 = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Detects the terminal width on startup. |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
public static $detect_width = true; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The minimum width to detect terminal size as. |
53
|
|
|
* |
54
|
|
|
* Less than this is ignored and falls back to default width. |
55
|
|
|
* |
56
|
|
|
* @var int |
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
|
|
|
|