1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\Exception\InvalidInstantiationException; |
6
|
|
|
use PhpSchool\CliMenu\Terminal\TerminalFactory; |
7
|
|
|
use PhpSchool\CliMenu\Util\ColourUtil; |
8
|
|
|
use PhpSchool\Terminal\Terminal; |
9
|
|
|
|
10
|
|
|
//TODO: B/W fallback |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Michael Woodward <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class MenuStyle |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Terminal |
19
|
|
|
*/ |
20
|
|
|
protected $terminal; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int|string |
24
|
|
|
*/ |
25
|
|
|
protected $fg; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int|string |
29
|
|
|
*/ |
30
|
|
|
protected $bg; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $width; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
protected $padding; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
protected $margin; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $contentWidth; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $selectedMarker; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $unselectedMarker; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
private $itemExtra; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var bool |
69
|
|
|
*/ |
70
|
|
|
private $displaysExtra; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
private $titleSeparator; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var string |
79
|
|
|
*/ |
80
|
|
|
private $coloursSetCode; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
private $invertedColoursSetCode = "\033[7m"; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var string |
89
|
|
|
*/ |
90
|
|
|
private $invertedColoursUnsetCode = "\033[27m"; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var string |
94
|
|
|
*/ |
95
|
|
|
private $coloursResetCode = "\033[0m"; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var int |
99
|
|
|
*/ |
100
|
|
|
private $borderTopWidth; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var int |
104
|
|
|
*/ |
105
|
|
|
private $borderRightWidth; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var int |
109
|
|
|
*/ |
110
|
|
|
private $borderBottomWidth; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var int |
114
|
|
|
*/ |
115
|
|
|
private $borderLeftWidth; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @var string |
119
|
|
|
*/ |
120
|
|
|
private $borderColour; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @var bool |
124
|
|
|
*/ |
125
|
|
|
private $marginAuto = false; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Default Values |
129
|
|
|
* |
130
|
|
|
* @var array |
131
|
|
|
*/ |
132
|
|
|
private static $defaultStyleValues = [ |
133
|
|
|
'fg' => 'white', |
134
|
|
|
'bg' => 'blue', |
135
|
|
|
'width' => 100, |
136
|
|
|
'padding' => 2, |
137
|
|
|
'margin' => 2, |
138
|
|
|
'selectedMarker' => '●', |
139
|
|
|
'unselectedMarker' => '○', |
140
|
|
|
'itemExtra' => '✔', |
141
|
|
|
'displaysExtra' => false, |
142
|
|
|
'titleSeparator' => '=', |
143
|
|
|
'borderTopWidth' => 0, |
144
|
|
|
'borderRightWidth' => 0, |
145
|
|
|
'borderBottomWidth' => 0, |
146
|
|
|
'borderLeftWidth' => 0, |
147
|
|
|
'borderColour' => 'white', |
148
|
|
|
'marginAuto' => false, |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
public static function getDefaultStyleValues() : array |
152
|
|
|
{ |
153
|
|
|
return static::$defaultStyleValues; |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @var array |
158
|
|
|
*/ |
159
|
|
|
private static $availableForegroundColors = array( |
160
|
|
|
'black' => 30, |
161
|
|
|
'red' => 31, |
162
|
|
|
'green' => 32, |
163
|
|
|
'yellow' => 33, |
164
|
|
|
'blue' => 34, |
165
|
|
|
'magenta' => 35, |
166
|
|
|
'cyan' => 36, |
167
|
|
|
'white' => 37, |
168
|
|
|
'default' => 39, |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @var array |
173
|
|
|
*/ |
174
|
|
|
private static $availableBackgroundColors = array( |
175
|
|
|
'black' => 40, |
176
|
|
|
'red' => 41, |
177
|
|
|
'green' => 42, |
178
|
|
|
'yellow' => 43, |
179
|
|
|
'blue' => 44, |
180
|
|
|
'magenta' => 45, |
181
|
|
|
'cyan' => 46, |
182
|
|
|
'white' => 47, |
183
|
|
|
'default' => 49, |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @var array |
188
|
|
|
*/ |
189
|
|
|
private static $availableOptions = array( |
190
|
|
|
'bold' => array('set' => 1, 'unset' => 22), |
191
|
|
|
'dim' => array('set' => 2, 'unset' => 22), |
192
|
|
|
'underscore' => array('set' => 4, 'unset' => 24), |
193
|
|
|
'blink' => array('set' => 5, 'unset' => 25), |
194
|
|
|
'reverse' => array('set' => 7, 'unset' => 27), |
195
|
|
|
'conceal' => array('set' => 8, 'unset' => 28) |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Initialise style |
200
|
|
|
*/ |
201
|
|
|
public function __construct(Terminal $terminal = null) |
202
|
|
|
{ |
203
|
|
|
$this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
204
|
|
|
|
205
|
|
|
$this->setFg(static::$defaultStyleValues['fg']); |
|
|
|
|
206
|
|
|
$this->setBg(static::$defaultStyleValues['bg']); |
|
|
|
|
207
|
|
|
$this->setWidth(static::$defaultStyleValues['width']); |
|
|
|
|
208
|
|
|
$this->setPadding(static::$defaultStyleValues['padding']); |
|
|
|
|
209
|
|
|
$this->setMargin(static::$defaultStyleValues['margin']); |
|
|
|
|
210
|
|
|
$this->setSelectedMarker(static::$defaultStyleValues['selectedMarker']); |
|
|
|
|
211
|
|
|
$this->setUnselectedMarker(static::$defaultStyleValues['unselectedMarker']); |
|
|
|
|
212
|
|
|
$this->setItemExtra(static::$defaultStyleValues['itemExtra']); |
|
|
|
|
213
|
|
|
$this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']); |
|
|
|
|
214
|
|
|
$this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']); |
|
|
|
|
215
|
|
|
$this->setBorderTopWidth(static::$defaultStyleValues['borderTopWidth']); |
|
|
|
|
216
|
|
|
$this->setBorderRightWidth(static::$defaultStyleValues['borderRightWidth']); |
|
|
|
|
217
|
|
|
$this->setBorderBottomWidth(static::$defaultStyleValues['borderBottomWidth']); |
|
|
|
|
218
|
|
|
$this->setBorderLeftWidth(static::$defaultStyleValues['borderLeftWidth']); |
|
|
|
|
219
|
|
|
$this->setBorderColour(static::$defaultStyleValues['borderColour']); |
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function getDisabledItemText(string $text) : string |
223
|
|
|
{ |
224
|
|
|
return sprintf( |
225
|
|
|
"\033[%sm%s\033[%sm", |
226
|
|
|
self::$availableOptions['dim']['set'], |
227
|
|
|
$text, |
228
|
|
|
self::$availableOptions['dim']['unset'] |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Generates the ansi escape sequence to set the colours |
234
|
|
|
*/ |
235
|
|
|
private function generateColoursSetCode() : void |
236
|
|
|
{ |
237
|
|
|
if (is_string($this->fg)) { |
238
|
|
|
$fgCode = self::$availableForegroundColors[$this->fg]; |
239
|
|
|
} else { |
240
|
|
|
$fgCode = sprintf("38;5;%s", $this->fg); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if (is_string($this->bg)) { |
244
|
|
|
$bgCode = self::$availableBackgroundColors[$this->bg]; |
245
|
|
|
} else { |
246
|
|
|
$bgCode = sprintf("48;5;%s", $this->bg); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$this->coloursSetCode = sprintf("\033[%s;%sm", $fgCode, $bgCode); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get the colour code for Bg and Fg |
254
|
|
|
*/ |
255
|
|
|
public function getColoursSetCode() : string |
256
|
|
|
{ |
257
|
|
|
return $this->coloursSetCode; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get the inverted escape sequence (used for selected elements) |
262
|
|
|
*/ |
263
|
|
|
public function getInvertedColoursSetCode() : string |
264
|
|
|
{ |
265
|
|
|
return $this->invertedColoursSetCode; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Get the inverted escape sequence (used for selected elements) |
270
|
|
|
*/ |
271
|
|
|
public function getInvertedColoursUnsetCode() : string |
272
|
|
|
{ |
273
|
|
|
return $this->invertedColoursUnsetCode; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Get the escape sequence used to reset colours to default |
278
|
|
|
*/ |
279
|
|
|
public function getColoursResetCode() : string |
280
|
|
|
{ |
281
|
|
|
return $this->coloursResetCode; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Calculate the contents width |
286
|
|
|
*/ |
287
|
|
|
protected function calculateContentWidth() : void |
288
|
|
|
{ |
289
|
|
|
$this->contentWidth = $this->width |
290
|
|
|
- ($this->padding * 2) |
291
|
|
|
- ($this->borderRightWidth + $this->borderLeftWidth); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function getFg() |
295
|
|
|
{ |
296
|
|
|
return $this->fg; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
View Code Duplication |
public function setFg($fg, string $fallback = null) : self |
|
|
|
|
300
|
|
|
{ |
301
|
|
|
$this->fg = ColourUtil::validateColour( |
302
|
|
|
$this->terminal, |
303
|
|
|
$fg, |
304
|
|
|
$fallback |
305
|
|
|
); |
306
|
|
|
$this->generateColoursSetCode(); |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function getBg() |
312
|
|
|
{ |
313
|
|
|
return $this->bg; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
View Code Duplication |
public function setBg($bg, string $fallback = null) : self |
|
|
|
|
317
|
|
|
{ |
318
|
|
|
$this->bg = ColourUtil::validateColour( |
319
|
|
|
$this->terminal, |
320
|
|
|
$bg, |
321
|
|
|
$fallback |
322
|
|
|
); |
323
|
|
|
$this->generateColoursSetCode(); |
324
|
|
|
|
325
|
|
|
return $this; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function getWidth() : int |
329
|
|
|
{ |
330
|
|
|
return $this->width; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function setWidth(int $width) : self |
334
|
|
|
{ |
335
|
|
|
if ($width >= $this->terminal->getWidth()) { |
336
|
|
|
$width = $this->terminal->getWidth(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
$this->width = $width; |
340
|
|
|
if ($this->marginAuto) { |
341
|
|
|
$this->setMarginAuto(); |
342
|
|
|
} |
343
|
|
|
$this->calculateContentWidth(); |
344
|
|
|
|
345
|
|
|
return $this; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function getPadding() : int |
349
|
|
|
{ |
350
|
|
|
return $this->padding; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function setPadding(int $padding) : self |
354
|
|
|
{ |
355
|
|
|
$this->padding = $padding; |
356
|
|
|
|
357
|
|
|
$this->calculateContentWidth(); |
358
|
|
|
|
359
|
|
|
return $this; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function getMargin() : int |
363
|
|
|
{ |
364
|
|
|
return $this->margin; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function setMarginAuto() : self |
368
|
|
|
{ |
369
|
|
|
$this->marginAuto = true; |
370
|
|
|
$this->margin = floor(($this->terminal->getWidth() - $this->width) / 2); |
|
|
|
|
371
|
|
|
|
372
|
|
|
return $this; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
public function setMargin(int $margin) : self |
376
|
|
|
{ |
377
|
|
|
$this->marginAuto = false; |
378
|
|
|
$this->margin = $margin; |
379
|
|
|
|
380
|
|
|
return $this; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function getContentWidth() : int |
384
|
|
|
{ |
385
|
|
|
return $this->contentWidth; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Get padding for right had side of content |
390
|
|
|
*/ |
391
|
|
|
public function getRightHandPadding(int $contentLength) : int |
392
|
|
|
{ |
393
|
|
|
return $this->getContentWidth() - $contentLength + $this->getPadding(); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
public function getSelectedMarker() : string |
397
|
|
|
{ |
398
|
|
|
return $this->selectedMarker; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
public function setSelectedMarker(string $marker) : self |
402
|
|
|
{ |
403
|
|
|
$this->selectedMarker = mb_substr($marker, 0, 1); |
404
|
|
|
|
405
|
|
|
return $this; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
public function getUnselectedMarker() : string |
409
|
|
|
{ |
410
|
|
|
return $this->unselectedMarker; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
public function setUnselectedMarker(string $marker) : self |
414
|
|
|
{ |
415
|
|
|
$this->unselectedMarker = mb_substr($marker, 0, 1); |
416
|
|
|
|
417
|
|
|
return $this; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Get the correct marker for the item |
422
|
|
|
*/ |
423
|
|
|
public function getMarker(bool $selected) : string |
424
|
|
|
{ |
425
|
|
|
return $selected ? $this->selectedMarker : $this->unselectedMarker; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
public function setItemExtra(string $itemExtra) : self |
429
|
|
|
{ |
430
|
|
|
$this->itemExtra = $itemExtra; |
431
|
|
|
|
432
|
|
|
return $this; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
public function getItemExtra() : string |
436
|
|
|
{ |
437
|
|
|
return $this->itemExtra; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
public function getDisplaysExtra() : bool |
441
|
|
|
{ |
442
|
|
|
return $this->displaysExtra; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
public function setDisplaysExtra(bool $displaysExtra) : self |
446
|
|
|
{ |
447
|
|
|
$this->displaysExtra = $displaysExtra; |
448
|
|
|
|
449
|
|
|
return $this; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
public function getTitleSeparator() : string |
453
|
|
|
{ |
454
|
|
|
return $this->titleSeparator; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
public function setTitleSeparator(string $actionSeparator) : self |
458
|
|
|
{ |
459
|
|
|
$this->titleSeparator = $actionSeparator; |
460
|
|
|
|
461
|
|
|
return $this; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Shorthand function to set all borders values at once |
466
|
|
|
*/ |
467
|
|
|
public function setBorder( |
468
|
|
|
int $topWidth, |
469
|
|
|
$rightWidth = null, |
470
|
|
|
$bottomWidth = null, |
471
|
|
|
$leftWidth = null, |
472
|
|
|
string $colour = null |
473
|
|
|
) : self { |
474
|
|
View Code Duplication |
if (!is_int($rightWidth)) { |
|
|
|
|
475
|
|
|
$colour = $rightWidth; |
476
|
|
|
$rightWidth = $bottomWidth = $leftWidth = $topWidth; |
477
|
|
|
} elseif (!is_int($bottomWidth)) { |
478
|
|
|
$colour = $bottomWidth; |
479
|
|
|
$bottomWidth = $topWidth; |
480
|
|
|
$leftWidth = $rightWidth; |
481
|
|
|
} elseif (!is_int($leftWidth)) { |
482
|
|
|
$colour = $leftWidth; |
483
|
|
|
$leftWidth = $rightWidth; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
$this->borderTopWidth = $topWidth; |
487
|
|
|
$this->borderRightWidth = $rightWidth; |
488
|
|
|
$this->borderBottomWidth = $bottomWidth; |
489
|
|
|
$this->borderLeftWidth = $leftWidth; |
490
|
|
|
|
491
|
|
|
if (is_string($colour)) { |
492
|
|
|
$this->borderColour = $colour; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
$this->calculateContentWidth(); |
496
|
|
|
|
497
|
|
|
return $this; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
public function setBorderTopWidth(int $width) : self |
501
|
|
|
{ |
502
|
|
|
$this->borderTopWidth = $width; |
503
|
|
|
|
504
|
|
|
return $this; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
public function setBorderRightWidth(int $width) : self |
508
|
|
|
{ |
509
|
|
|
$this->borderRightWidth = $width; |
510
|
|
|
$this->calculateContentWidth(); |
511
|
|
|
|
512
|
|
|
return $this; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
public function setBorderBottomWidth(int $width) : self |
516
|
|
|
{ |
517
|
|
|
$this->borderBottomWidth = $width; |
518
|
|
|
|
519
|
|
|
return $this; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
public function setBorderLeftWidth(int $width) : self |
523
|
|
|
{ |
524
|
|
|
$this->borderLeftWidth = $width; |
525
|
|
|
$this->calculateContentWidth(); |
526
|
|
|
|
527
|
|
|
return $this; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
public function setBorderColour(string $colour) : self |
531
|
|
|
{ |
532
|
|
|
$this->borderColour = $colour; |
533
|
|
|
|
534
|
|
|
return $this; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
public function getBorderTopWidth() : int |
538
|
|
|
{ |
539
|
|
|
return $this->borderTopWidth; |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
public function getBorderRightWidth() : int |
543
|
|
|
{ |
544
|
|
|
return $this->borderRightWidth; |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
public function getBorderBottomWidth() : int |
548
|
|
|
{ |
549
|
|
|
return $this->borderBottomWidth; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
public function getBorderLeftWidth() : int |
553
|
|
|
{ |
554
|
|
|
return $this->borderLeftWidth; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
public function getBorderColour() : string |
558
|
|
|
{ |
559
|
|
|
return $this->borderColour; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
public function getBorderColourCode() : string |
563
|
|
|
{ |
564
|
|
|
return sprintf("\033[%sm", self::$availableBackgroundColors[$this->getBorderColour()]['set']); |
565
|
|
|
} |
566
|
|
|
} |
567
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: