Skip to content

Commit

Permalink
Item extra toggling support
Browse files Browse the repository at this point in the history
  • Loading branch information
AydinHassan committed Dec 16, 2019
1 parent d80c749 commit ead500a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
28 changes: 28 additions & 0 deletions examples/item-extra-toggling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;

require_once(__DIR__ . '/../vendor/autoload.php');

$itemCallable = function (CliMenu $menu) {
if ($menu->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();
14 changes: 13 additions & 1 deletion src/Builder/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit ead500a

Please sign in to comment.