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