@@ -94,6 +94,41 @@ class MenuStyle
94
94
*/
95
95
private $ coloursResetCode = "\033[0m " ;
96
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 = 'white ' ;
121
+
122
+ /**
123
+ * @var array
124
+ */
125
+ private $ borderTopRows = [];
126
+
127
+ /**
128
+ * @var array
129
+ */
130
+ private $ borderBottomRows = [];
131
+
97
132
/**
98
133
* @var bool
99
134
*/
@@ -115,6 +150,11 @@ class MenuStyle
115
150
'itemExtra ' => '✔ ' ,
116
151
'displaysExtra ' => false ,
117
152
'titleSeparator ' => '= ' ,
153
+ 'borderTopWidth ' => 0 ,
154
+ 'borderRightWidth ' => 0 ,
155
+ 'borderBottomWidth ' => 0 ,
156
+ 'borderLeftWidth ' => 0 ,
157
+ 'borderColour ' => 'white ' ,
118
158
'marginAuto ' => false ,
119
159
];
120
160
@@ -185,6 +225,11 @@ public function __construct(Terminal $terminal = null)
185
225
$ this ->setItemExtra (static ::$ defaultStyleValues ['itemExtra ' ]);
186
226
$ this ->setDisplaysExtra (static ::$ defaultStyleValues ['displaysExtra ' ]);
187
227
$ this ->setTitleSeparator (static ::$ defaultStyleValues ['titleSeparator ' ]);
228
+ $ this ->setBorderTopWidth (static ::$ defaultStyleValues ['borderTopWidth ' ]);
229
+ $ this ->setBorderRightWidth (static ::$ defaultStyleValues ['borderRightWidth ' ]);
230
+ $ this ->setBorderBottomWidth (static ::$ defaultStyleValues ['borderBottomWidth ' ]);
231
+ $ this ->setBorderLeftWidth (static ::$ defaultStyleValues ['borderLeftWidth ' ]);
232
+ $ this ->setBorderColour (static ::$ defaultStyleValues ['borderColour ' ]);
188
233
}
189
234
190
235
public function getDisabledItemText (string $ text ) : string
@@ -254,7 +299,9 @@ public function getColoursResetCode() : string
254
299
*/
255
300
protected function calculateContentWidth () : void
256
301
{
257
- $ this ->contentWidth = $ this ->width - ($ this ->padding * 2 );
302
+ $ this ->contentWidth = $ this ->width
303
+ - ($ this ->padding * 2 )
304
+ - ($ this ->borderRightWidth + $ this ->borderLeftWidth );
258
305
}
259
306
260
307
public function getFg ()
@@ -306,7 +353,9 @@ public function setWidth(int $width) : self
306
353
if ($ this ->marginAuto ) {
307
354
$ this ->setMarginAuto ();
308
355
}
356
+
309
357
$ this ->calculateContentWidth ();
358
+ $ this ->generateBorderRows ();
310
359
311
360
return $ this ;
312
361
}
@@ -334,7 +383,9 @@ public function setMarginAuto() : self
334
383
{
335
384
$ this ->marginAuto = true ;
336
385
$ this ->margin = floor (($ this ->terminal ->getWidth () - $ this ->width ) / 2 );
337
-
386
+
387
+ $ this ->generateBorderRows ();
388
+
338
389
return $ this ;
339
390
}
340
391
@@ -343,6 +394,8 @@ public function setMargin(int $margin) : self
343
394
$ this ->marginAuto = false ;
344
395
$ this ->margin = $ margin ;
345
396
397
+ $ this ->generateBorderRows ();
398
+
346
399
return $ this ;
347
400
}
348
401
@@ -426,4 +479,151 @@ public function setTitleSeparator(string $actionSeparator) : self
426
479
427
480
return $ this ;
428
481
}
482
+
483
+ private function generateBorderRows () : void
484
+ {
485
+ $ borderRow = sprintf (
486
+ "%s%s%s%s%s \n" ,
487
+ str_repeat (' ' , $ this ->margin ),
488
+ $ this ->getBorderColourCode (),
489
+ str_repeat (' ' , $ this ->width ),
490
+ $ this ->coloursResetCode ,
491
+ str_repeat (' ' , $ this ->margin )
492
+ );
493
+
494
+ $ this ->borderTopRows = array_fill (0 , $ this ->borderTopWidth , $ borderRow );
495
+ $ this ->borderBottomRows = array_fill (0 , $ this ->borderBottomWidth , $ borderRow );
496
+ }
497
+
498
+ public function getBorderTopRows () : array
499
+ {
500
+ return $ this ->borderTopRows ;
501
+ }
502
+
503
+ public function getBorderBottomRows () : array
504
+ {
505
+ return $ this ->borderBottomRows ;
506
+ }
507
+
508
+ /**
509
+ * Shorthand function to set all borders values at once
510
+ */
511
+ public function setBorder (
512
+ int $ topWidth ,
513
+ $ rightWidth = null ,
514
+ $ bottomWidth = null ,
515
+ $ leftWidth = null ,
516
+ string $ colour = null
517
+ ) : self {
518
+ if (!is_int ($ rightWidth )) {
519
+ $ colour = $ rightWidth ;
520
+ $ rightWidth = $ bottomWidth = $ leftWidth = $ topWidth ;
521
+ } elseif (!is_int ($ bottomWidth )) {
522
+ $ colour = $ bottomWidth ;
523
+ $ bottomWidth = $ topWidth ;
524
+ $ leftWidth = $ rightWidth ;
525
+ } elseif (!is_int ($ leftWidth )) {
526
+ $ colour = $ leftWidth ;
527
+ $ leftWidth = $ rightWidth ;
528
+ }
529
+
530
+ $ this ->borderTopWidth = $ topWidth ;
531
+ $ this ->borderRightWidth = $ rightWidth ;
532
+ $ this ->borderBottomWidth = $ bottomWidth ;
533
+ $ this ->borderLeftWidth = $ leftWidth ;
534
+
535
+ if (is_string ($ colour )) {
536
+ $ this ->setBorderColour ($ colour );
537
+ } elseif ($ colour !== null ) {
538
+ throw new \InvalidArgumentException ('Invalid colour ' );
539
+ }
540
+
541
+ $ this ->calculateContentWidth ();
542
+ $ this ->generateBorderRows ();
543
+
544
+ return $ this ;
545
+ }
546
+
547
+ public function setBorderTopWidth (int $ width ) : self
548
+ {
549
+ $ this ->borderTopWidth = $ width ;
550
+
551
+ $ this ->generateBorderRows ();
552
+
553
+ return $ this ;
554
+ }
555
+
556
+ public function setBorderRightWidth (int $ width ) : self
557
+ {
558
+ $ this ->borderRightWidth = $ width ;
559
+ $ this ->calculateContentWidth ();
560
+
561
+ return $ this ;
562
+ }
563
+
564
+ public function setBorderBottomWidth (int $ width ) : self
565
+ {
566
+ $ this ->borderBottomWidth = $ width ;
567
+
568
+ $ this ->generateBorderRows ();
569
+
570
+ return $ this ;
571
+ }
572
+
573
+ public function setBorderLeftWidth (int $ width ) : self
574
+ {
575
+ $ this ->borderLeftWidth = $ width ;
576
+ $ this ->calculateContentWidth ();
577
+
578
+ return $ this ;
579
+ }
580
+
581
+ public function setBorderColour (string $ colour , $ fallback = null ) : self
582
+ {
583
+ $ this ->borderColour = ColourUtil::validateColour (
584
+ $ this ->terminal ,
585
+ $ colour ,
586
+ $ fallback
587
+ );
588
+
589
+ $ this ->generateBorderRows ();
590
+
591
+ return $ this ;
592
+ }
593
+
594
+ public function getBorderTopWidth () : int
595
+ {
596
+ return $ this ->borderTopWidth ;
597
+ }
598
+
599
+ public function getBorderRightWidth () : int
600
+ {
601
+ return $ this ->borderRightWidth ;
602
+ }
603
+
604
+ public function getBorderBottomWidth () : int
605
+ {
606
+ return $ this ->borderBottomWidth ;
607
+ }
608
+
609
+ public function getBorderLeftWidth () : int
610
+ {
611
+ return $ this ->borderLeftWidth ;
612
+ }
613
+
614
+ public function getBorderColour () : string
615
+ {
616
+ return $ this ->borderColour ;
617
+ }
618
+
619
+ public function getBorderColourCode () : string
620
+ {
621
+ if (!ctype_digit ($ this ->borderColour )) {
622
+ $ borderColourCode = self ::$ availableBackgroundColors [$ this ->borderColour ];
623
+ } else {
624
+ $ borderColourCode = sprintf ("48;5;%s " , $ this ->borderColour );
625
+ }
626
+
627
+ return sprintf ("\033[%sm " , $ borderColourCode );
628
+ }
429
629
}
0 commit comments