File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use PhpSchool \CliMenu \CliMenu ;
4
+ use PhpSchool \CliMenu \Builder \CliMenuBuilder ;
5
+
6
+ require_once (__DIR__ . '/../vendor/autoload.php ' );
7
+
8
+ $ itemCallable = function (CliMenu $ menu ) {
9
+ if ($ menu ->getSelectedItem ()->showsItemExtra ()) {
10
+ $ menu ->getSelectedItem ()->hideItemExtra ();
11
+ } else {
12
+ $ menu ->getSelectedItem ()->showItemExtra ();
13
+ }
14
+ $ menu ->redraw ();
15
+ echo $ menu ->getSelectedItem ()->getText ();
16
+ };
17
+
18
+ $ menu = (new CliMenuBuilder )
19
+ ->setTitle ('Basic CLI Menu Custom Item Extra ' )
20
+ ->addItem ('First Item ' , $ itemCallable )
21
+ ->addItem ('Second Item ' , $ itemCallable )
22
+ ->addItem ('Third Item ' , $ itemCallable )
23
+ ->setItemExtra ('[COMPLETE!] ' )
24
+ ->displayExtra ()
25
+ ->addLineBreak ('- ' )
26
+ ->build ();
27
+
28
+ $ menu ->open ();
Original file line number Diff line number Diff line change @@ -399,6 +399,9 @@ public function setItemExtra(string $extra) : self
399
399
{
400
400
$ this ->style ->setItemExtra ($ extra );
401
401
402
+ //if we customise item extra, it means we most likely want to display it
403
+ $ this ->displayExtra ();
404
+
402
405
return $ this ;
403
406
}
404
407
@@ -479,6 +482,13 @@ public function disableDefaultItems() : self
479
482
return $ this ;
480
483
}
481
484
485
+ public function displayExtra () : self
486
+ {
487
+ $ this ->style ->setDisplaysExtra (true );
488
+
489
+ return $ this ;
490
+ }
491
+
482
492
private function itemsHaveExtra (array $ items ) : bool
483
493
{
484
494
return !empty (array_filter ($ items , function (MenuItemInterface $ item ) {
@@ -492,7 +502,9 @@ public function build() : CliMenu
492
502
$ this ->menu ->addItems ($ this ->getDefaultItems ());
493
503
}
494
504
495
- $ this ->style ->setDisplaysExtra ($ this ->itemsHaveExtra ($ this ->menu ->getItems ()));
505
+ if (!$ this ->style ->getDisplaysExtra ()) {
506
+ $ this ->style ->setDisplaysExtra ($ this ->itemsHaveExtra ($ this ->menu ->getItems ()));
507
+ }
496
508
497
509
return $ this ->menu ;
498
510
}
You can’t perform that action at this time.
0 commit comments