Skip to content

Toggle extra #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,56 @@ The third param on the `->addItem` call is what disables an item while the `->di

The outcome is a full menu with dimmed rows to denote them being disabled. When a user navigates these items are jumped over to the next available selectable item.

#### Redrawing the menu

You can modify the menu and its style when executing an action and then you can redraw it! In this example we will toggle the background
colour in an action.

```php
$itemCallable = function (CliMenu $menu) {
$menu->getStyle()->setBg($menu->getStyle()->getBg() === 'red' ? 'blue' : 'red');
$menu->redraw();
};

$menu = (new CliMenuBuilder)
->setTitle('Basic CLI Menu')
->addItem('First Item', $itemCallable)
->addItem('Second Item', $itemCallable)
->addItem('Third Item', $itemCallable)
->addLineBreak('-')
->build();

$menu->open();
```

#### Getting, Removing and Adding items

You can also interact with the menu items in an action:

```php
use PhpSchool\CliMenu\MenuItem\LineBreakItem;

$itemCallable = function (CliMenu $menu) {
foreach ($menu->getItems() as $item) {
$menu->removeItem($item);
}

$menu->addItem(new LineBreakItem('-'));

$menu->redraw();
};

$menu = (new CliMenuBuilder)
->setTitle('Basic CLI Menu')
->addItem('First Item', $itemCallable)
->addItem('Second Item', $itemCallable)
->addItem('Third Item', $itemCallable)
->addLineBreak('-')
->build();

$menu->open();
```

---

Once you get going you might just end up with something that looks a little like this...
Expand Down
61 changes: 61 additions & 0 deletions examples/crazy-redraw.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\CliMenuBuilder;
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;

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

$itemCallable = function (CliMenu $menu) {
$colour = function () {
return array_rand(array_flip(['blue', 'green', 'red', 'yellow']));
};
$int = function () {
return rand(1, 20);
};

$bg = $colour();
while (($fg = $colour()) === $bg) {
}

$menu->getStyle()->setBg($bg);
$menu->getStyle()->setFg($fg);
$menu->getStyle()->setPadding($int());
$menu->getStyle()->setMargin($int());

$items = $menu->getItems();

array_walk($items, function (MenuItemInterface $item) use ($menu) {
$menu->removeItem($item);
});

$items = array_filter($items, function (MenuItemInterface $item) {
return !$item instanceof LineBreakItem;
});

foreach (range(0, rand(1, 5)) as $i) {
$items[] = new LineBreakItem(array_rand(array_flip(['♥', '★', '😂'])), rand(1, 3));
}
shuffle($items);

array_walk(
$items,
function (MenuItemInterface $item) use ($menu) {
$menu->addItem($item);
}
);

$menu->redraw();
};

$menu = (new CliMenuBuilder)
->setTitle('Basic CLI Menu')
->setWidth(80)
->addItem('First Item', $itemCallable)
->addItem('Second Item', $itemCallable)
->addItem('Third Item', $itemCallable)
->addLineBreak('-')
->build();

$menu->open();
21 changes: 21 additions & 0 deletions examples/redraw.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

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

$itemCallable = function (CliMenu $menu) {
$menu->getStyle()->setBg($menu->getStyle()->getBg() === 'red' ? 'blue' : 'red');
$menu->redraw();
};

$menu = (new CliMenuBuilder)
->setTitle('Basic CLI Menu')
->addItem('First Item', $itemCallable)
->addItem('Second Item', $itemCallable)
->addItem('Third Item', $itemCallable)
->addLineBreak('-')
->build();

$menu->open();
32 changes: 32 additions & 0 deletions examples/toggle-item-extra.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\CliMenuBuilder;
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;

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

$itemCallable = function (CliMenu $menu) {
static $i = 1;

foreach ($menu->getItems() as $item) {
$i % 2 === 0
? $item->showItemExtra()
: $item->hideItemExtra();

$menu->redraw();
}

$i++;
};

$menu = (new CliMenuBuilder)
->setTitle('Basic CLI Menu Custom Item Extra')
->addItem('First Item', $itemCallable, true)
->addItem('Second Item', $itemCallable, true)
->addItem('Third Item', $itemCallable, true)
->addLineBreak('-')
->setItemExtra('**')
->build();

$menu->open();
28 changes: 27 additions & 1 deletion src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __construct(
$this->title = $title;
$this->items = $items;
$this->terminal = $terminal ?: TerminalFactory::fromSystem();
$this->style = $style ?: new MenuStyle();
$this->style = $style ?: new MenuStyle($this->terminal);

$this->selectFirstItem();
}
Expand Down Expand Up @@ -153,6 +153,10 @@ public function isOpen()
public function addItem(MenuItemInterface $item)
{
$this->items[] = $item;

if (count($this->items) === 1) {
$this->selectFirstItem();
}
}

/**
Expand Down Expand Up @@ -346,6 +350,28 @@ public function closeThis()
$this->open = false;
}

/**
* @return MenuItemInterface[]
*/
public function getItems()
{
return $this->items;
}

/**
* @param MenuItemInterface $item
*/
public function removeItem(MenuItemInterface $item)
{
$key = array_search($item, $this->items);

if (false === $key) {
throw new \InvalidArgumentException('Item does not exist in menu');
}

unset($this->items[$key]);
}

/**
* @return MenuStyle
*/
Expand Down
18 changes: 18 additions & 0 deletions src/MenuItem/AsciiArtItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,22 @@ public function showsItemExtra()
{
return false;
}

/**
* Enable showing item extra
*
*/
public function showItemExtra()
{
//noop
}

/**
* Disable showing item extra
*
*/
public function hideItemExtra()
{
//noop
}
}
18 changes: 18 additions & 0 deletions src/MenuItem/LineBreakItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,22 @@ public function showsItemExtra()
{
return false;
}

/**
* Enable showing item extra
*
*/
public function showItemExtra()
{
//noop
}

/**
* Disable showing item extra
*
*/
public function hideItemExtra()
{
//noop
}
}
12 changes: 12 additions & 0 deletions src/MenuItem/MenuItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ public function getSelectAction();
* @return bool
*/
public function showsItemExtra();

/**
* Enable showing item extra
*
*/
public function showItemExtra();

/**
* Disable showing item extra
*
*/
public function hideItemExtra();
}
18 changes: 18 additions & 0 deletions src/MenuItem/SelectableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,22 @@ public function showsItemExtra()
{
return $this->showItemExtra;
}

/**
* Enable showing item extra
*
*/
public function showItemExtra()
{
$this->showItemExtra = true;
}

/**
* Disable showing item extra
*
*/
public function hideItemExtra()
{
$this->showItemExtra = false;
}
}
18 changes: 18 additions & 0 deletions src/MenuItem/StaticItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,22 @@ public function showsItemExtra()
{
return false;
}

/**
* Enable showing item extra
*
*/
public function showItemExtra()
{
//noop
}

/**
* Disable showing item extra
*
*/
public function hideItemExtra()
{
//noop
}
}
Loading