From 0ff7a37052c1a5cb86145d3dbe698801b2a1f196 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Mon, 14 May 2018 19:03:57 +0200 Subject: [PATCH 1/2] Fix infinite loop when no selectable items --- src/CliMenu.php | 14 ++++++++++++++ test/CliMenuTest.php | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/CliMenu.php b/src/CliMenu.php index e6d434d6..0d6b63fe 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -88,6 +88,11 @@ class CliMenu */ private $currentFrame; + /** + * @var bool + */ + private $hasSelectableItems = false; + public function __construct( ?string $title, array $items, @@ -277,6 +282,10 @@ private function display() : void */ protected function moveSelectionVertically(string $direction) : void { + if (!$this->hasSelectableItems) { + return; + } + $itemKeys = array_keys($this->items); do { @@ -493,6 +502,11 @@ public function open() : void $this->configureTerminal(); $this->open = true; + + $this->hasSelectableItems = array_reduce($this->items, function (bool $carry, MenuItemInterface $item) { + return $carry || $item->canSelect(); + }, false); + $this->display(); } diff --git a/test/CliMenuTest.php b/test/CliMenuTest.php index 1f6df128..2fef1602 100644 --- a/test/CliMenuTest.php +++ b/test/CliMenuTest.php @@ -842,6 +842,22 @@ public function testSelectableCallableReceivesSelectableAndNotSplitItem() : void self::assertSame($expectedSelectedItem, $actualSelectedItem); } + public function testMenuCanOpenAndFunctionWithoutAnySelectableItems() : void + { + $this->terminal->expects($this->exactly(3)) + ->method('read') + ->willReturn("\033[B", "\033[B", 'Q'); + + $menu = new CliMenu('PHP School FTW', [new StaticItem('One')], $this->terminal); + $menu->addCustomControlMapping('Q', function (CliMenu $menu) { + $menu->close(); + }); + + $menu->open(); + + self::assertCount(1, $menu->getItems()); + } + private function getTestFile() : string { return sprintf('%s/res/%s.txt', __DIR__, $this->getName()); From 8158de551696fccf16cee4edb14b2f02f3b09f9a Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Mon, 14 May 2018 19:07:54 +0200 Subject: [PATCH 2/2] Changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53f67bfc..164d0f95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip - Enable cursor when exiting menu (#110) - Fixed (#71) - changed padding calculation when row too long to stop php notices (#112) - Fixed wordwrap helper (#134) + - Fixed infinite loop when no selectable items exist in menu (#144, #148) ### Removed - Dropped PHP 5.x and PHP 7.0 support (#79)