1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ConsolePrettyLog; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Line |
7
|
|
|
* @package ConsolePrettyLog |
8
|
|
|
* @author William Alvares <30/07/2022 20:09> |
9
|
|
|
*/ |
10
|
|
|
class Line |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int[] |
14
|
|
|
*/ |
15
|
|
|
private array $codes; |
16
|
|
|
/** |
17
|
|
|
* @var array|null |
18
|
|
|
*/ |
19
|
|
|
private ?array $texts; |
20
|
|
|
/** |
21
|
|
|
* @var array|null |
22
|
|
|
*/ |
23
|
|
|
private ?array $textsInitial; |
24
|
|
|
/** |
25
|
|
|
* @var array|null |
26
|
|
|
*/ |
27
|
|
|
private ?array $styles; |
28
|
|
|
/** |
29
|
|
|
* @var array|null |
30
|
|
|
*/ |
31
|
|
|
private ?array $stylesInitial; |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private string $mask; |
36
|
|
|
/** |
37
|
|
|
* @var array|null |
38
|
|
|
*/ |
39
|
|
|
private ?array $columnsSize; |
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private string $separator; |
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private string $paddingCharacter; |
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
private bool $enableDate; |
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private string $dateFormat; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
*/ |
59
|
|
|
public function __construct() |
60
|
|
|
{ |
61
|
|
|
$this->codes = [ |
62
|
|
|
'bold' => 1, |
63
|
|
|
'italic' => 3, |
64
|
|
|
'underline' => 4, |
65
|
|
|
'strikethrough' => 9, |
66
|
|
|
'red' => 31, |
67
|
|
|
'green' => 32, |
68
|
|
|
'yellow' => 33, |
69
|
|
|
'blue' => 34, |
70
|
|
|
'magenta' => 35, |
71
|
|
|
'cyan' => 36, |
72
|
|
|
'white' => 37, |
73
|
|
|
'redbg' => 41, |
74
|
|
|
'greenbg' => 42, |
75
|
|
|
'yellowbg' => 43, |
76
|
|
|
'bluebg' => 44, |
77
|
|
|
'magentabg' => 45, |
78
|
|
|
'cyanbg' => 46, |
79
|
|
|
'lightgreybg' => 47 |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$this->mask = ""; |
83
|
|
|
$this->textsInitial = null; |
84
|
|
|
$this->texts = null; |
85
|
|
|
$this->stylesInitial = null; |
86
|
|
|
$this->styles = null; |
87
|
|
|
$this->columnsSize = null; |
88
|
|
|
$this->separator = "|"; |
89
|
|
|
$this->paddingCharacter = "."; |
90
|
|
|
$this->enableDate = true; |
91
|
|
|
$this->dateFormat = "Y-m-d H:i:s"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $separator |
96
|
|
|
* @return Line |
97
|
|
|
*/ |
98
|
|
|
public function separator(string $separator): Line |
99
|
|
|
{ |
100
|
|
|
$this->separator = $separator; |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $paddingCharacter |
106
|
|
|
* @return Line |
107
|
|
|
*/ |
108
|
|
|
public function paddingCharacter(string $paddingCharacter): Line |
109
|
|
|
{ |
110
|
|
|
$this->paddingCharacter = $paddingCharacter; |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param bool $enableDate |
116
|
|
|
* @return Line |
117
|
|
|
*/ |
118
|
|
|
public function enableDate(bool $enableDate = true): Line |
119
|
|
|
{ |
120
|
|
|
$this->enableDate = $enableDate; |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $dateFormat |
126
|
|
|
* @return Line |
127
|
|
|
*/ |
128
|
|
|
public function dateFormat(string $dateFormat): Line |
129
|
|
|
{ |
130
|
|
|
$this->dateFormat = $dateFormat; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string|null $text |
136
|
|
|
* @param array|null $styles |
137
|
|
|
* @return Line |
138
|
|
|
*/ |
139
|
|
|
public function text(?string $text, ?array $styles = []): Line |
140
|
|
|
{ |
141
|
|
|
$this->texts[] = $this->textEnconding($text); |
142
|
|
|
$this->styles[] = $this->getStyleCodes($styles); |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string|null $text |
149
|
|
|
* @param array|null $styles |
150
|
|
|
* @return Line |
151
|
|
|
*/ |
152
|
|
|
public function textInitial(?string $text, ?array $styles = []): Line |
153
|
|
|
{ |
154
|
|
|
$this->textsInitial[] = $this->textEnconding($text); |
155
|
|
|
$this->stylesInitial[] = $this->getStyleCodes($styles); |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string|null $text |
162
|
|
|
* @return false|string |
163
|
|
|
*/ |
164
|
|
|
private function textEnconding(?string $text) |
165
|
|
|
{ |
166
|
|
|
return iconv('UTF-8', 'ascii//TRANSLIT', $text); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param array|null $styles |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
private function getStyleCodes(?array $styles = []): string |
174
|
|
|
{ |
175
|
|
|
$formatMap = array_map(function ($v) { |
176
|
|
|
return $this->codes[$v]; |
177
|
|
|
}, $styles); |
178
|
|
|
|
179
|
|
|
return implode(';', $formatMap); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param array|null $columnsSize |
184
|
|
|
* @return Line |
185
|
|
|
*/ |
186
|
|
|
public function columnsSize(?array $columnsSize): Line |
187
|
|
|
{ |
188
|
|
|
$this->columnsSize = $columnsSize; |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
|
|
public function print(): void |
196
|
|
|
{ |
197
|
|
|
$this->textsInitial(); |
198
|
|
|
|
199
|
|
|
if ($this->columnsSize === null) { |
200
|
|
|
$this->texts(); |
201
|
|
|
} else { |
202
|
|
|
$this->textsWithPaddings(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if ($this->enableDate === true) { |
206
|
|
|
echo sprintf("\e[34m[%s] \e[0m", date($this->dateFormat)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
echo vsprintf($this->mask . "\n", $this->texts); |
|
|
|
|
210
|
|
|
|
211
|
|
|
$this->newLine(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return void |
216
|
|
|
*/ |
217
|
|
|
private function texts(): void |
218
|
|
|
{ |
219
|
|
|
$count = count($this->texts); |
|
|
|
|
220
|
|
|
|
221
|
|
|
$i = 0; |
222
|
|
|
foreach ($this->texts as $key => $text) { |
223
|
|
|
$i++; |
224
|
|
|
|
225
|
|
|
$codes = $this->styles[$key] ?? null; |
226
|
|
|
if ($codes === null) { |
227
|
|
|
continue; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$this->mask .= "\e[{$codes}m"; |
231
|
|
|
$this->mask .= "%s"; |
232
|
|
|
$this->mask .= "\e[0m"; |
233
|
|
|
$this->mask .= $count === $i ? "" : " {$this->separator} "; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return void |
239
|
|
|
*/ |
240
|
|
|
private function textsWithPaddings(): void |
241
|
|
|
{ |
242
|
|
|
$count = count($this->columnsSize); |
|
|
|
|
243
|
|
|
|
244
|
|
|
$i = 0; |
245
|
|
|
foreach ($this->columnsSize as $key => $size) { |
246
|
|
|
$i++; |
247
|
|
|
|
248
|
|
|
$codes = $this->styles[$key] ?? null; |
249
|
|
|
if ($codes === null) { |
250
|
|
|
continue; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$this->mask .= "\e[{$codes}m"; |
254
|
|
|
$this->mask .= "%-'{$this->paddingCharacter}{$size}s"; |
255
|
|
|
$this->mask .= "\e[0m"; |
256
|
|
|
$this->mask .= $count === $i ? "" : " {$this->separator} "; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return void |
262
|
|
|
*/ |
263
|
|
|
private function textsInitial(): void |
264
|
|
|
{ |
265
|
|
|
if ($this->textsInitial === null) { |
266
|
|
|
return; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$this->texts = array_merge($this->textsInitial, $this->texts); |
|
|
|
|
270
|
|
|
$this->styles = array_merge($this->stylesInitial, $this->styles); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return void |
275
|
|
|
*/ |
276
|
|
|
private function newLine(): void |
277
|
|
|
{ |
278
|
|
|
$this->texts = []; |
279
|
|
|
$this->styles = []; |
280
|
|
|
$this->mask = ""; |
281
|
|
|
} |
282
|
|
|
} |