php-school / cli-menu

🖥 Build beautiful PHP CLI menus. Simple yet Powerful. Expressive DSL.

Home Page:http://www.phpschool.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Infinite loop with no selectable items in menu

Lynesth opened this issue · comments

So if you create a menu without any selectable item and then try to move the selection up/down, we end up in the moveSelectionVertically() loop forever.

Here is a sample to try it:

<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\CliMenuBuilder;
use PhpSchool\CliMenu\Action\ExitAction;

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

$itemCallable = function (CliMenu $menu) {
    echo $menu->getSelectedItem()->getText();
};

$menu = (new CliMenuBuilder)
    ->disableDefaultItems()
    ->addStaticItem('blablabla')
    ->build();

$menu->addCustomControlMapping('Q', new ExitAction);
$menu->open();

Launch the example, then press Q to quit it, it works.
Launch the example, then press up or down arrow. Try to press Q to quit, doesn't work.

I think we should allow static only menus, as we can still do stuff with custom controls, but at some point we need to either remember if the menu contains selectable items or check during the loop if we went back to the start without finding anything to select.