Skip to content

Commit df89aac

Browse files
authored
Merge pull request #46 from php-school/toggle-extra
Toggle extra
2 parents 721717c + 6120afd commit df89aac

16 files changed

+418
-97
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,56 @@ The third param on the `->addItem` call is what disables an item while the `->di
362362

363363
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.
364364

365+
#### Redrawing the menu
366+
367+
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
368+
colour in an action.
369+
370+
```php
371+
$itemCallable = function (CliMenu $menu) {
372+
$menu->getStyle()->setBg($menu->getStyle()->getBg() === 'red' ? 'blue' : 'red');
373+
$menu->redraw();
374+
};
375+
376+
$menu = (new CliMenuBuilder)
377+
->setTitle('Basic CLI Menu')
378+
->addItem('First Item', $itemCallable)
379+
->addItem('Second Item', $itemCallable)
380+
->addItem('Third Item', $itemCallable)
381+
->addLineBreak('-')
382+
->build();
383+
384+
$menu->open();
385+
```
386+
387+
#### Getting, Removing and Adding items
388+
389+
You can also interact with the menu items in an action:
390+
391+
```php
392+
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
393+
394+
$itemCallable = function (CliMenu $menu) {
395+
foreach ($menu->getItems() as $item) {
396+
$menu->removeItem($item);
397+
}
398+
399+
$menu->addItem(new LineBreakItem('-'));
400+
401+
$menu->redraw();
402+
};
403+
404+
$menu = (new CliMenuBuilder)
405+
->setTitle('Basic CLI Menu')
406+
->addItem('First Item', $itemCallable)
407+
->addItem('Second Item', $itemCallable)
408+
->addItem('Third Item', $itemCallable)
409+
->addLineBreak('-')
410+
->build();
411+
412+
$menu->open();
413+
```
414+
365415
---
366416

367417
Once you get going you might just end up with something that looks a little like this...

examples/crazy-redraw.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
use PhpSchool\CliMenu\CliMenu;
4+
use PhpSchool\CliMenu\CliMenuBuilder;
5+
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
6+
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;
7+
8+
require_once(__DIR__ . '/../vendor/autoload.php');
9+
10+
$itemCallable = function (CliMenu $menu) {
11+
$colour = function () {
12+
return array_rand(array_flip(['blue', 'green', 'red', 'yellow']));
13+
};
14+
$int = function () {
15+
return rand(1, 20);
16+
};
17+
18+
$bg = $colour();
19+
while (($fg = $colour()) === $bg) {
20+
}
21+
22+
$menu->getStyle()->setBg($bg);
23+
$menu->getStyle()->setFg($fg);
24+
$menu->getStyle()->setPadding($int());
25+
$menu->getStyle()->setMargin($int());
26+
27+
$items = $menu->getItems();
28+
29+
array_walk($items, function (MenuItemInterface $item) use ($menu) {
30+
$menu->removeItem($item);
31+
});
32+
33+
$items = array_filter($items, function (MenuItemInterface $item) {
34+
return !$item instanceof LineBreakItem;
35+
});
36+
37+
foreach (range(0, rand(1, 5)) as $i) {
38+
$items[] = new LineBreakItem(array_rand(array_flip(['', '', '😂'])), rand(1, 3));
39+
}
40+
shuffle($items);
41+
42+
array_walk(
43+
$items,
44+
function (MenuItemInterface $item) use ($menu) {
45+
$menu->addItem($item);
46+
}
47+
);
48+
49+
$menu->redraw();
50+
};
51+
52+
$menu = (new CliMenuBuilder)
53+
->setTitle('Basic CLI Menu')
54+
->setWidth(80)
55+
->addItem('First Item', $itemCallable)
56+
->addItem('Second Item', $itemCallable)
57+
->addItem('Third Item', $itemCallable)
58+
->addLineBreak('-')
59+
->build();
60+
61+
$menu->open();

examples/redraw.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use PhpSchool\CliMenu\CliMenu;
4+
use PhpSchool\CliMenu\CliMenuBuilder;
5+
6+
require_once(__DIR__ . '/../vendor/autoload.php');
7+
8+
$itemCallable = function (CliMenu $menu) {
9+
$menu->getStyle()->setBg($menu->getStyle()->getBg() === 'red' ? 'blue' : 'red');
10+
$menu->redraw();
11+
};
12+
13+
$menu = (new CliMenuBuilder)
14+
->setTitle('Basic CLI Menu')
15+
->addItem('First Item', $itemCallable)
16+
->addItem('Second Item', $itemCallable)
17+
->addItem('Third Item', $itemCallable)
18+
->addLineBreak('-')
19+
->build();
20+
21+
$menu->open();

examples/toggle-item-extra.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use PhpSchool\CliMenu\CliMenu;
4+
use PhpSchool\CliMenu\CliMenuBuilder;
5+
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;
6+
7+
require_once(__DIR__ . '/../vendor/autoload.php');
8+
9+
$itemCallable = function (CliMenu $menu) {
10+
static $i = 1;
11+
12+
foreach ($menu->getItems() as $item) {
13+
$i % 2 === 0
14+
? $item->showItemExtra()
15+
: $item->hideItemExtra();
16+
17+
$menu->redraw();
18+
}
19+
20+
$i++;
21+
};
22+
23+
$menu = (new CliMenuBuilder)
24+
->setTitle('Basic CLI Menu Custom Item Extra')
25+
->addItem('First Item', $itemCallable, true)
26+
->addItem('Second Item', $itemCallable, true)
27+
->addItem('Third Item', $itemCallable, true)
28+
->addLineBreak('-')
29+
->setItemExtra('**')
30+
->build();
31+
32+
$menu->open();

src/CliMenu.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function __construct(
7272
$this->title = $title;
7373
$this->items = $items;
7474
$this->terminal = $terminal ?: TerminalFactory::fromSystem();
75-
$this->style = $style ?: new MenuStyle();
75+
$this->style = $style ?: new MenuStyle($this->terminal);
7676

7777
$this->selectFirstItem();
7878
}
@@ -153,6 +153,10 @@ public function isOpen()
153153
public function addItem(MenuItemInterface $item)
154154
{
155155
$this->items[] = $item;
156+
157+
if (count($this->items) === 1) {
158+
$this->selectFirstItem();
159+
}
156160
}
157161

158162
/**
@@ -346,6 +350,28 @@ public function closeThis()
346350
$this->open = false;
347351
}
348352

353+
/**
354+
* @return MenuItemInterface[]
355+
*/
356+
public function getItems()
357+
{
358+
return $this->items;
359+
}
360+
361+
/**
362+
* @param MenuItemInterface $item
363+
*/
364+
public function removeItem(MenuItemInterface $item)
365+
{
366+
$key = array_search($item, $this->items);
367+
368+
if (false === $key) {
369+
throw new \InvalidArgumentException('Item does not exist in menu');
370+
}
371+
372+
unset($this->items[$key]);
373+
}
374+
349375
/**
350376
* @return MenuStyle
351377
*/

src/MenuItem/AsciiArtItem.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,22 @@ public function showsItemExtra()
125125
{
126126
return false;
127127
}
128+
129+
/**
130+
* Enable showing item extra
131+
*
132+
*/
133+
public function showItemExtra()
134+
{
135+
//noop
136+
}
137+
138+
/**
139+
* Disable showing item extra
140+
*
141+
*/
142+
public function hideItemExtra()
143+
{
144+
//noop
145+
}
128146
}

src/MenuItem/LineBreakItem.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,22 @@ public function showsItemExtra()
9595
{
9696
return false;
9797
}
98+
99+
/**
100+
* Enable showing item extra
101+
*
102+
*/
103+
public function showItemExtra()
104+
{
105+
//noop
106+
}
107+
108+
/**
109+
* Disable showing item extra
110+
*
111+
*/
112+
public function hideItemExtra()
113+
{
114+
//noop
115+
}
98116
}

src/MenuItem/MenuItemInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,16 @@ public function getSelectAction();
4848
* @return bool
4949
*/
5050
public function showsItemExtra();
51+
52+
/**
53+
* Enable showing item extra
54+
*
55+
*/
56+
public function showItemExtra();
57+
58+
/**
59+
* Disable showing item extra
60+
*
61+
*/
62+
public function hideItemExtra();
5163
}

src/MenuItem/SelectableTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,22 @@ public function showsItemExtra()
8282
{
8383
return $this->showItemExtra;
8484
}
85+
86+
/**
87+
* Enable showing item extra
88+
*
89+
*/
90+
public function showItemExtra()
91+
{
92+
$this->showItemExtra = true;
93+
}
94+
95+
/**
96+
* Disable showing item extra
97+
*
98+
*/
99+
public function hideItemExtra()
100+
{
101+
$this->showItemExtra = false;
102+
}
85103
}

src/MenuItem/StaticItem.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,22 @@ public function showsItemExtra()
8080
{
8181
return false;
8282
}
83+
84+
/**
85+
* Enable showing item extra
86+
*
87+
*/
88+
public function showItemExtra()
89+
{
90+
//noop
91+
}
92+
93+
/**
94+
* Disable showing item extra
95+
*
96+
*/
97+
public function hideItemExtra()
98+
{
99+
//noop
100+
}
83101
}

0 commit comments

Comments
 (0)