/
Console.php
248 lines (208 loc) · 6.03 KB
/
Console.php
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace eXpansion\Framework\Core\Services;
use eXpansion\Framework\Core\Helpers\ColorConversion;
use eXpansion\Framework\Core\Services\Application\Dispatcher;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Class Console to print in the console.
*
* @package eXpansion\Framework\Core\Services
* @author Reaby
*/
class Console
{
const black = "\e[0;30m";
const b_black = "\e[30;1m";
const red = "\e[0;31m";
const b_red = "\e[1;31m";
const green = "\e[0;32m";
const b_green = "\e[1;32m";
const yellow = "\e[0;33m";
const b_yellow = "\e[1;33m";
const blue = "\e[0;34m";
const b_blue = "\e[1;34m";
const magenta = "\e[0;35m";
const b_magenta = "\e[1;35m";
const cyan = "\e[0;36m";
const b_cyan = "\e[1;36m";
const white = "\e[0;37m";
const b_white = "\e[1;37m";
// define aliases for colors
const error = "\e[37;1m\e[41m";
const success = self::b_green;
const normal = "\e[0m";
const bold = self::b_white;
/** @var OutputInterface */
protected $consoleOutput;
/** @var SymfonyStyle */
protected $sfStyleOutput;
/** @var boolean Color console enabled */
protected $colorEnabled;
/**
* @var Dispatcher
*/
private $dispatcher;
/**
* Console constructor.
*
* @param bool $colorEnabled
*/
public function __construct($colorEnabled)
{
$this->colorEnabled = $colorEnabled;
}
/**
* Initialize service with the console output.
*
* @param OutputInterface $consoleOutput
* @param $dispatcher
*/
public function init(OutputInterface $consoleOutput, $dispatcher)
{
$this->consoleOutput = $consoleOutput;
$this->dispatcher = $dispatcher;
$this->sfStyleOutput = new SymfonyStyle(new StringInput(''), $consoleOutput);
}
/**
* @inheritdoc
*/
public function writeln($string)
{
$this->write($string, true);
}
/**
* @inheritdoc
*/
public function write($string, $newline = false)
{
if ($this->dispatcher instanceof Dispatcher) {
$this->dispatcher->dispatch("expansion.console.message", [$string]);
}
if ($this->colorEnabled && $this->consoleOutput->isDecorated()) {
$matches = array();
preg_match_all("/\\$[A-Fa-f0-9]{3}/", $string, $matches);
$split = preg_split("/\\$[A-Fa-f0-9]{3}/", $string);
$out = "";
foreach ($matches[0] as $i => $rgb) {
$code = $this->doHslConvert(hexdec($rgb[1].$rgb[1]), hexdec($rgb[2].$rgb[2]), hexdec($rgb[3].$rgb[3]));
$out .= $code.$this->stripStyles($split[$i + 1]);
$end = $this->stripStyles($split[$i + 1]);
}
if (!empty($end)) {
if ($end == $this->stripStyles(end($split))) {
$end = "";
}
} else {
$end = "";
}
$out = self::white.$this->stripStyles(reset($split)).$out.$end.self::normal;
} else {
$out = $this->stripStyles($string);
}
$this->ansiOut($out, $newline);
}
/**
* Outoyt brute text.
*
* @param string $msg
* @param boolean $newline
*
*/
protected function ansiOut($msg, $newline)
{
$nl = "";
if ($newline) {
$nl = "\n";
}
echo $msg.$nl;
}
/**
* Strip styles from a string.
*
* @param $string
*
* @return mixed
*/
protected function stripStyles($string)
{
return preg_replace('/(\$[wnoitsgz><]|\$[lh]\[.+\]|\$[lh]|\$[0-9a-f]{3})+/i', '', $string);
}
protected function doHslConvert($r, $g, $b)
{
$hsl = ColorConversion::rgbToHsl($r, $g, $b);
$lightness = 100 * $hsl[2];
$color = "37";
// if color has saturation
if ($hsl[1] > 0) {
$h = $hsl[0];
if ($h < 20) {
$color = "31";
} else {
if ($h < 70) {
$color = "33"; // yellow
} else {
if ($h < 160) {
$color = "32"; // green
} else {
if ($h < 214) {
$color = "36"; // cyan
} else {
if ($h < 284) {
$color = "34"; // blue
} else {
if ($h < 333) {
$color = "35"; // magenta
} else {
$color = "31"; // red
}
}
}
}
}
}
}
if ($lightness < 10) {
$attr = "1";
$color = "30";
} else {
if ($lightness < 44) {
$attr = "0";
} else {
if ($lightness < 95) {
$attr = "1";
} else {
$color = "37";
$attr = "1";
}
}
}
return "\e[".$attr.";".$color."m";
}
/**
* Get symphony console.
*
* @return OutputInterface
*/
public function getConsoleOutput()
{
if (is_null($this->consoleOutput)) {
$this->consoleOutput = new NullOutput();
}
return $this->consoleOutput;
}
/**
* Get symfony style output.
*
* @return SymfonyStyle
*/
public function getSfStyleOutput(): SymfonyStyle
{
if (is_null($this->sfStyleOutput)) {
$this->sfStyleOutput = new SymfonyStyle(new StringInput(''), new NullOutput());
}
return $this->sfStyleOutput;
}
}