File tree Expand file tree Collapse file tree 4 files changed +96
-1
lines changed Expand file tree Collapse file tree 4 files changed +96
-1
lines changed Original file line number Diff line number Diff line change @@ -791,6 +791,32 @@ $menu = (new CliMenuBuilder)
791
791
->build();
792
792
```
793
793
794
+ If no items have display extra set to true, then the item extra will not be displayed. If you toggle the item to show
795
+ it's item extra in a callback or at runtime it will render incorrectly.
796
+
797
+ In order to fix that you need to tell the menu to display item extra explicitly. You can do this when constructing the
798
+ menu like so:
799
+
800
+ ``` php
801
+ <?php
802
+
803
+ use PhpSchool\CliMenu\Builder\CliMenuBuilder;
804
+ use PhpSchool\CliMenu\CliMenu;
805
+
806
+ $menu = (new CliMenuBuilder)
807
+ ->setItemExtra('✔')
808
+ ->addItem('Exercise 1', function (CliMenu $menu) {
809
+ $selectedItem = $menu->getSelectedItem();
810
+ if ($selectedItem->showsItemExtra()) {
811
+ $selectedItem->hideItemExtra();
812
+ } else {
813
+ $selectedItem->showItemExtra();
814
+ }
815
+ })
816
+ ->displayExtra()
817
+ ->build();
818
+ ```
819
+
794
820
## Menu Methods
795
821
796
822
The next set of documentation applies to methods available directly on the ` \PhpSchool\CliMenu\CliMenu ` instance. Typically
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 @@ -425,6 +425,9 @@ public function setItemExtra(string $extra) : self
425
425
{
426
426
$ this ->style ->setItemExtra ($ extra );
427
427
428
+ //if we customise item extra, it means we most likely want to display it
429
+ $ this ->displayExtra ();
430
+
428
431
return $ this ;
429
432
}
430
433
@@ -505,6 +508,13 @@ public function disableDefaultItems() : self
505
508
return $ this ;
506
509
}
507
510
511
+ public function displayExtra () : self
512
+ {
513
+ $ this ->style ->setDisplaysExtra (true );
514
+
515
+ return $ this ;
516
+ }
517
+
508
518
private function itemsHaveExtra (array $ items ) : bool
509
519
{
510
520
return !empty (array_filter ($ items , function (MenuItemInterface $ item ) {
@@ -518,7 +528,9 @@ public function build() : CliMenu
518
528
$ this ->menu ->addItems ($ this ->getDefaultItems ());
519
529
}
520
530
521
- $ this ->style ->setDisplaysExtra ($ this ->itemsHaveExtra ($ this ->menu ->getItems ()));
531
+ if (!$ this ->style ->getDisplaysExtra ()) {
532
+ $ this ->style ->setDisplaysExtra ($ this ->itemsHaveExtra ($ this ->menu ->getItems ()));
533
+ }
522
534
523
535
return $ this ->menu ;
524
536
}
Original file line number Diff line number Diff line change @@ -820,6 +820,35 @@ public function testAddSplitItemWithClosureBinding() : void
820
820
$ this ->checkItems ($ menu ->getItems ()[0 ]->getItems (), $ expected );
821
821
}
822
822
823
+ public function testDisplayExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra () : void
824
+ {
825
+ $ cb = function () {
826
+ };
827
+ $ builder = new CliMenuBuilder ;
828
+ $ builder ->addItem ('Item 1 ' , $ cb );
829
+ $ builder ->addItem ('Item 2 ' , $ cb );
830
+ $ builder ->displayExtra ();
831
+
832
+ $ menu = $ builder ->build ();
833
+
834
+ self ::assertTrue ($ menu ->getStyle ()->getDisplaysExtra ());
835
+ }
836
+
837
+ public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra () : void
838
+ {
839
+ $ cb = function () {
840
+ };
841
+ $ builder = new CliMenuBuilder ;
842
+ $ builder ->addItem ('Item 1 ' , $ cb );
843
+ $ builder ->addItem ('Item 2 ' , $ cb );
844
+ $ builder ->setItemExtra ('DONE ' );
845
+
846
+ $ menu = $ builder ->build ();
847
+
848
+ self ::assertTrue ($ menu ->getStyle ()->getDisplaysExtra ());
849
+ }
850
+
851
+
823
852
private function checkMenuItems (CliMenu $ menu , array $ expected ) : void
824
853
{
825
854
$ this ->checkItems ($ menu ->getItems (), $ expected );
You can’t perform that action at this time.
0 commit comments