From 0cc3c0c41b1715f057ee7e9a186675146104c2fe Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Wed, 5 Oct 2016 19:49:27 +0100 Subject: [PATCH 1/5] Add getter for items --- src/CliMenu.php | 10 +++++++++- test/CliMenuTest.php | 25 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/CliMenu.php b/src/CliMenu.php index 334d1b76..9529607d 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -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(); } @@ -346,6 +346,14 @@ public function closeThis() $this->open = false; } + /** + * @return MenuItemInterface[] + */ + public function getItems() + { + return $this->items; + } + /** * @return MenuStyle */ diff --git a/test/CliMenuTest.php b/test/CliMenuTest.php index 7f86d076..c8039083 100644 --- a/test/CliMenuTest.php +++ b/test/CliMenuTest.php @@ -4,6 +4,7 @@ use PhpSchool\CliMenu\CliMenu; use PhpSchool\CliMenu\Exception\MenuNotOpenException; +use PhpSchool\CliMenu\MenuItem\LineBreakItem; use PhpSchool\CliMenu\MenuItem\SelectableItem; use PhpSchool\CliMenu\MenuStyle; use PhpSchool\CliMenu\Terminal\TerminalInterface; @@ -30,7 +31,7 @@ public function testReDrawThrowsExceptionIfMenuNotOpen() $this->expectException(MenuNotOpenException::class); - $menu->reDraw(); + $menu->redraw(); } public function testSimpleOpenClose() @@ -91,6 +92,28 @@ public function testReDrawReDrawsImmediately() $menu->open(); } + public function testGetItems() + { + $item1 = new LineBreakItem(); + $item2 = new LineBreakItem(); + + + $terminal = $this->createMock(TerminalInterface::class); + $style = $this->getStyle($terminal); + + $menu = new CliMenu( + 'PHP School FTW', + [ + $item1, + $item2 + ], + $terminal, + $style + ); + + static::assertSame([$item1, $item2], $menu->getItems()); + } + /** * @return string */ From 36b1061dc61b85fe0045c828fbac6eef6b435f63 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Wed, 5 Oct 2016 20:05:00 +0100 Subject: [PATCH 2/5] Ability to toggle item extra --- src/MenuItem/AsciiArtItem.php | 18 +++++++ src/MenuItem/LineBreakItem.php | 18 +++++++ src/MenuItem/MenuItemInterface.php | 12 +++++ src/MenuItem/SelectableTrait.php | 18 +++++++ src/MenuItem/StaticItem.php | 18 +++++++ test/MenuItem/AsciiArtItemTest.php | 31 ++++++------ test/MenuItem/LineBreakItemTest.php | 31 ++++++------ test/MenuItem/MenuMenuItemTest.php | 71 +++++++++++----------------- test/MenuItem/SelectableItemTest.php | 34 ++++++------- test/MenuItem/StaticItemTest.php | 21 +++++--- 10 files changed, 177 insertions(+), 95 deletions(-) diff --git a/src/MenuItem/AsciiArtItem.php b/src/MenuItem/AsciiArtItem.php index f8e24f90..5b3b4193 100644 --- a/src/MenuItem/AsciiArtItem.php +++ b/src/MenuItem/AsciiArtItem.php @@ -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 + } } diff --git a/src/MenuItem/LineBreakItem.php b/src/MenuItem/LineBreakItem.php index 385b7098..c27cf1ff 100644 --- a/src/MenuItem/LineBreakItem.php +++ b/src/MenuItem/LineBreakItem.php @@ -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 + } } diff --git a/src/MenuItem/MenuItemInterface.php b/src/MenuItem/MenuItemInterface.php index a697244a..5b6b33e4 100644 --- a/src/MenuItem/MenuItemInterface.php +++ b/src/MenuItem/MenuItemInterface.php @@ -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(); } diff --git a/src/MenuItem/SelectableTrait.php b/src/MenuItem/SelectableTrait.php index 9d96bb69..a5892fec 100644 --- a/src/MenuItem/SelectableTrait.php +++ b/src/MenuItem/SelectableTrait.php @@ -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; + } } diff --git a/src/MenuItem/StaticItem.php b/src/MenuItem/StaticItem.php index 1f738a22..2e014975 100644 --- a/src/MenuItem/StaticItem.php +++ b/src/MenuItem/StaticItem.php @@ -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 + } } diff --git a/test/MenuItem/AsciiArtItemTest.php b/test/MenuItem/AsciiArtItemTest.php index dabe95f0..7bdb6767 100644 --- a/test/MenuItem/AsciiArtItemTest.php +++ b/test/MenuItem/AsciiArtItemTest.php @@ -16,13 +16,13 @@ class AsciiArtItemTest extends PHPUnit_Framework_TestCase { public function testExceptionIsThrownIfBreakCharNotString() { - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); new AsciiArtItem(new \stdClass); } public function testExceptionIsThrownIfPositionNotValid() { - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); new AsciiArtItem('////\\\\', new \stdClass); } @@ -52,9 +52,7 @@ public function testGetText() public function testGetRowsLeftAligned() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -73,9 +71,7 @@ public function testGetRowsLeftAligned() public function testGetRowsRightAligned() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -94,9 +90,7 @@ public function testGetRowsRightAligned() public function testGetRowsCenterAligned() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -115,9 +109,7 @@ public function testGetRowsCenterAligned() public function testGetRowsCenterAlignedWithOddWidth() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -133,4 +125,15 @@ public function testGetRowsCenterAlignedWithOddWidth() $item->getRows($menuStyle) ); } + + public function testHideAndShowItemExtraHasNoEffect() + { + $item = new AsciiArtItem("//\n//", AsciiArtItem::POSITION_CENTER); + + $this->assertFalse($item->showsItemExtra()); + $item->showItemExtra(); + $this->assertFalse($item->showsItemExtra()); + $item->hideItemExtra(); + $this->assertFalse($item->showsItemExtra()); + } } diff --git a/test/MenuItem/LineBreakItemTest.php b/test/MenuItem/LineBreakItemTest.php index 45e64d99..4855ec68 100644 --- a/test/MenuItem/LineBreakItemTest.php +++ b/test/MenuItem/LineBreakItemTest.php @@ -16,13 +16,13 @@ class LineBreakItemTest extends PHPUnit_Framework_TestCase { public function testExceptionIsThrownIfBreakCharNotString() { - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); new LineBreakItem(new \stdClass); } public function testExceptionIsThrownIfNumLinesNotInt() { - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); new LineBreakItem('*', new \stdClass); } @@ -52,9 +52,7 @@ public function testGetText() public function testGetRowsRepeatsCharForMenuWidth() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -67,9 +65,7 @@ public function testGetRowsRepeatsCharForMenuWidth() public function testGetRowsRepeatsCharForMenuWidthMultiLines() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -82,9 +78,7 @@ public function testGetRowsRepeatsCharForMenuWidthMultiLines() public function testGetRowsWithPhraseThatDoesNotFitInWidthEvenlyIsTrimmed() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -100,9 +94,7 @@ public function testGetRowsWithPhraseThatDoesNotFitInWidthEvenlyIsTrimmed() public function testGetRowsWithMultiByteChars() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -112,4 +104,15 @@ public function testGetRowsWithMultiByteChars() $item = new LineBreakItem('❅', 2); $this->assertEquals(['❅❅❅❅❅', '❅❅❅❅❅'], $item->getRows($menuStyle)); } + + public function testHideAndShowItemExtraHasNoEffect() + { + $item = new LineBreakItem('*'); + + $this->assertFalse($item->showsItemExtra()); + $item->showItemExtra(); + $this->assertFalse($item->showsItemExtra()); + $item->hideItemExtra(); + $this->assertFalse($item->showsItemExtra()); + } } diff --git a/test/MenuItem/MenuMenuItemTest.php b/test/MenuItem/MenuMenuItemTest.php index 3595a478..955f19a1 100644 --- a/test/MenuItem/MenuMenuItemTest.php +++ b/test/MenuItem/MenuMenuItemTest.php @@ -17,19 +17,15 @@ class MenuMenuItemTest extends PHPUnit_Framework_TestCase { public function testExceptionIsThrownIfBreakCharNotString() { - $subMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); + $subMenu = $this->createMock(CliMenu::class); - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); new MenuMenuItem(new \stdClass, $subMenu); } public function testCanSelectIsTrue() { - $subMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); + $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('Item', $subMenu); $this->assertTrue($item->canSelect()); @@ -37,9 +33,7 @@ public function testCanSelectIsTrue() public function testGetSelectAction() { - $subMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); + $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('Item', $subMenu); @@ -52,9 +46,7 @@ public function testGetSelectAction() public function testShowsItemExtra() { - $subMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); + $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('Item', $subMenu); $this->assertFalse($item->showsItemExtra()); @@ -62,9 +54,7 @@ public function testShowsItemExtra() public function testGetText() { - $subMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); + $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('Item', $subMenu); $this->assertEquals('Item', $item->getText()); @@ -72,18 +62,14 @@ public function testGetText() public function testGetRows() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) ->method('getContentWidth') ->will($this->returnValue(10)); - $subMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); + $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('Item', $subMenu); $this->assertEquals([' Item'], $item->getRows($menuStyle)); @@ -91,9 +77,7 @@ public function testGetRows() public function testGetRowsWithUnSelectedMarker() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -106,9 +90,7 @@ public function testGetRowsWithUnSelectedMarker() ->with(false) ->will($this->returnValue('*')); - $subMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); + $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('Item', $subMenu); $this->assertEquals(['* Item'], $item->getRows($menuStyle)); @@ -117,9 +99,7 @@ public function testGetRowsWithUnSelectedMarker() public function testGetRowsWithSelectedMarker() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -143,18 +123,14 @@ public function testGetRowsWithSelectedMarker() public function testGetRowsWithMultipleLines() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) ->method('getContentWidth') ->will($this->returnValue(10)); - $subMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); + $subMenu = $this->createMock(CliMenu::class); $item = new MenuMenuItem('LONG ITEM LINE', $subMenu); $this->assertEquals( @@ -168,13 +144,8 @@ public function testGetRowsWithMultipleLines() public function testShowSubMenu() { - $mainMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); - - $subMenu = $this->getMockBuilder(CliMenu::class) - ->disableOriginalConstructor() - ->getMock(); + $mainMenu = $this->createMock(CliMenu::class); + $subMenu = $this->createMock(CliMenu::class); $mainMenu ->expects($this->once()) @@ -187,4 +158,16 @@ public function testShowSubMenu() $item = new MenuMenuItem('Item', $subMenu); $item->showSubMenu($mainMenu); } + + public function testHideAndShowItemExtra() + { + $subMenu = $this->createMock(CliMenu::class); + $item = new MenuMenuItem('Item', $subMenu); + + $this->assertFalse($item->showsItemExtra()); + $item->showItemExtra(); + $this->assertTrue($item->showsItemExtra()); + $item->hideItemExtra(); + $this->assertFalse($item->showsItemExtra()); + } } diff --git a/test/MenuItem/SelectableItemTest.php b/test/MenuItem/SelectableItemTest.php index 72e39293..c792b96e 100644 --- a/test/MenuItem/SelectableItemTest.php +++ b/test/MenuItem/SelectableItemTest.php @@ -16,7 +16,7 @@ class SelectableItemTest extends PHPUnit_Framework_TestCase { public function testExceptionIsThrownIfBreakCharNotString() { - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); new SelectableItem(new \stdClass, function () { }); } @@ -56,9 +56,7 @@ public function testGetText() public function testGetRows() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -74,9 +72,7 @@ public function testGetRows() public function testGetRowsWithUnSelectedMarker() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -97,9 +93,7 @@ public function testGetRowsWithUnSelectedMarker() public function testGetRowsWithSelectedMarker() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -119,9 +113,7 @@ public function testGetRowsWithSelectedMarker() public function testGetRowsWithItemExtra() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -140,9 +132,7 @@ public function testGetRowsWithItemExtra() public function testGetRowsWithMultipleLinesWithItemExtra() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->any()) @@ -164,4 +154,16 @@ public function testGetRowsWithMultipleLinesWithItemExtra() $item->getRows($menuStyle) ); } + + public function testHideAndShowItemExtra() + { + $item = new SelectableItem('Item', function () { + }); + + $this->assertFalse($item->showsItemExtra()); + $item->showItemExtra(); + $this->assertTrue($item->showsItemExtra()); + $item->hideItemExtra(); + $this->assertFalse($item->showsItemExtra()); + } } diff --git a/test/MenuItem/StaticItemTest.php b/test/MenuItem/StaticItemTest.php index 7bda9366..31f20bbc 100644 --- a/test/MenuItem/StaticItemTest.php +++ b/test/MenuItem/StaticItemTest.php @@ -15,7 +15,7 @@ class StaticItemTest extends PHPUnit_Framework_TestCase { public function testExceptionIsThrownIfArgumentNotString() { - $this->setExpectedException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); new StaticItem(new \stdClass); } @@ -45,9 +45,7 @@ public function testGetText() public function testGetRowsWithContentWhichFitsOnOneLine() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->once()) @@ -64,9 +62,7 @@ public function testGetRowsWithContentWhichFitsOnOneLine() public function testGetRowsWithContentWhichDoesNotFitOnOneLineIsWrapped() { - $menuStyle = $this->getMockBuilder(MenuStyle::class) - ->disableOriginalConstructor() - ->getMock(); + $menuStyle = $this->createMock(MenuStyle::class); $menuStyle ->expects($this->once()) @@ -80,4 +76,15 @@ public function testGetRowsWithContentWhichDoesNotFitOnOneLineIsWrapped() $item->getRows($menuStyle) ); } + + public function testHideAndShowItemExtraHasNoEffect() + { + $item = new StaticItem('CONTENT 1 LINE'); + + $this->assertFalse($item->showsItemExtra()); + $item->showItemExtra(); + $this->assertFalse($item->showsItemExtra()); + $item->hideItemExtra(); + $this->assertFalse($item->showsItemExtra()); + } } From 0a176ca252ab03e2d0df8d2eac28bf265e08c24d Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Wed, 5 Oct 2016 21:27:51 +0100 Subject: [PATCH 3/5] Examples --- examples/crazy-redraw.php | 61 ++++++++++++++++++++++++++++++++++ examples/redraw.php | 21 ++++++++++++ examples/toggle-item-extra.php | 32 ++++++++++++++++++ src/CliMenu.php | 18 ++++++++++ 4 files changed, 132 insertions(+) create mode 100644 examples/crazy-redraw.php create mode 100644 examples/redraw.php create mode 100644 examples/toggle-item-extra.php diff --git a/examples/crazy-redraw.php b/examples/crazy-redraw.php new file mode 100644 index 00000000..ae82d138 --- /dev/null +++ b/examples/crazy-redraw.php @@ -0,0 +1,61 @@ +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(); diff --git a/examples/redraw.php b/examples/redraw.php new file mode 100644 index 00000000..898d64cc --- /dev/null +++ b/examples/redraw.php @@ -0,0 +1,21 @@ +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(); diff --git a/examples/toggle-item-extra.php b/examples/toggle-item-extra.php new file mode 100644 index 00000000..49b2231e --- /dev/null +++ b/examples/toggle-item-extra.php @@ -0,0 +1,32 @@ +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(); diff --git a/src/CliMenu.php b/src/CliMenu.php index 9529607d..fce79909 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -153,6 +153,10 @@ public function isOpen() public function addItem(MenuItemInterface $item) { $this->items[] = $item; + + if (count($this->items) === 1) { + $this->selectFirstItem(); + } } /** @@ -354,6 +358,20 @@ 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 */ From d503ea1767a1537969fbbdbb5910f7abdef9f747 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Wed, 5 Oct 2016 21:44:59 +0100 Subject: [PATCH 4/5] Remove item test --- test/CliMenuTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/CliMenuTest.php b/test/CliMenuTest.php index c8039083..49215d8c 100644 --- a/test/CliMenuTest.php +++ b/test/CliMenuTest.php @@ -114,6 +114,32 @@ public function testGetItems() static::assertSame([$item1, $item2], $menu->getItems()); } + public function testRemoveItem() + { + $item1 = new LineBreakItem(); + $item2 = new LineBreakItem(); + + $terminal = $this->createMock(TerminalInterface::class); + $style = $this->getStyle($terminal); + + $menu = new CliMenu( + 'PHP School FTW', + [ + $item1, + $item2 + ], + $terminal, + $style + ); + + static::assertEquals([$item1, $item2], $menu->getItems()); + + $menu->removeItem($item1); + + static::assertCount(1, $menu->getItems()); + static::assertContains($item2, $menu->getItems()); + } + /** * @return string */ From 6120afd42106fa05b4945edf5eb9210d440cc60e Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Wed, 5 Oct 2016 22:01:04 +0100 Subject: [PATCH 5/5] docs --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/README.md b/README.md index 62606a85..48441871 100644 --- a/README.md +++ b/README.md @@ -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...