Skip to content

Commit 42aad08

Browse files
authored
Merge pull request #187 from php-school/item-extra-toggling
Item extra toggling support
2 parents 3c4db90 + b2febc9 commit 42aad08

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,32 @@ $menu = (new CliMenuBuilder)
791791
->build();
792792
```
793793

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+
794820
## Menu Methods
795821

796822
The next set of documentation applies to methods available directly on the `\PhpSchool\CliMenu\CliMenu` instance. Typically

examples/item-extra-toggling.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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();

src/Builder/CliMenuBuilder.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ public function setItemExtra(string $extra) : self
425425
{
426426
$this->style->setItemExtra($extra);
427427

428+
//if we customise item extra, it means we most likely want to display it
429+
$this->displayExtra();
430+
428431
return $this;
429432
}
430433

@@ -505,6 +508,13 @@ public function disableDefaultItems() : self
505508
return $this;
506509
}
507510

511+
public function displayExtra() : self
512+
{
513+
$this->style->setDisplaysExtra(true);
514+
515+
return $this;
516+
}
517+
508518
private function itemsHaveExtra(array $items) : bool
509519
{
510520
return !empty(array_filter($items, function (MenuItemInterface $item) {
@@ -518,7 +528,9 @@ public function build() : CliMenu
518528
$this->menu->addItems($this->getDefaultItems());
519529
}
520530

521-
$this->style->setDisplaysExtra($this->itemsHaveExtra($this->menu->getItems()));
531+
if (!$this->style->getDisplaysExtra()) {
532+
$this->style->setDisplaysExtra($this->itemsHaveExtra($this->menu->getItems()));
533+
}
522534

523535
return $this->menu;
524536
}

test/Builder/CliMenuBuilderTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,35 @@ public function testAddSplitItemWithClosureBinding() : void
820820
$this->checkItems($menu->getItems()[0]->getItems(), $expected);
821821
}
822822

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+
823852
private function checkMenuItems(CliMenu $menu, array $expected) : void
824853
{
825854
$this->checkItems($menu->getItems(), $expected);

0 commit comments

Comments
 (0)