diff --git a/examples/item-extra-toggling.php b/examples/item-extra-toggling.php new file mode 100644 index 00000000..87a503ed --- /dev/null +++ b/examples/item-extra-toggling.php @@ -0,0 +1,28 @@ +getSelectedItem()->showsItemExtra()) { + $menu->getSelectedItem()->hideItemExtra(); + } else { + $menu->getSelectedItem()->showItemExtra(); + } + $menu->redraw(); + echo $menu->getSelectedItem()->getText(); +}; + +$menu = (new CliMenuBuilder) + ->setTitle('Basic CLI Menu Custom Item Extra') + ->addItem('First Item', $itemCallable) + ->addItem('Second Item', $itemCallable) + ->addItem('Third Item', $itemCallable) + ->setItemExtra('[COMPLETE!]') + ->displayExtra() + ->addLineBreak('-') + ->build(); + +$menu->open(); diff --git a/src/Builder/CliMenuBuilder.php b/src/Builder/CliMenuBuilder.php index af9668a6..91fd67c2 100644 --- a/src/Builder/CliMenuBuilder.php +++ b/src/Builder/CliMenuBuilder.php @@ -399,6 +399,9 @@ public function setItemExtra(string $extra) : self { $this->style->setItemExtra($extra); + //if we customise item extra, it means we most likely want to display it + $this->displayExtra(); + return $this; } @@ -479,6 +482,13 @@ public function disableDefaultItems() : self return $this; } + public function displayExtra() : self + { + $this->style->setDisplaysExtra(true); + + return $this; + } + private function itemsHaveExtra(array $items) : bool { return !empty(array_filter($items, function (MenuItemInterface $item) { @@ -492,7 +502,9 @@ public function build() : CliMenu $this->menu->addItems($this->getDefaultItems()); } - $this->style->setDisplaysExtra($this->itemsHaveExtra($this->menu->getItems())); + if (!$this->style->getDisplaysExtra()) { + $this->style->setDisplaysExtra($this->itemsHaveExtra($this->menu->getItems())); + } return $this->menu; }