Skip to content

Error with toggling item extra #66

@polothy

Description

@polothy

Use case: I was writing up a menu where I want the user to select multiple menu items. I was using the ExtraItem to indicate which menu items were selected.

Here is an example to reproduce the problem:

<?php

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

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

$itemCallable = function (CliMenu $menu) use ($selected) {
    if ($menu->getSelectedItem()->showsItemExtra()) {
        $menu->getSelectedItem()->hideItemExtra();
    } else {
        $menu->getSelectedItem()->showItemExtra();
    }
    $menu->redraw();
};

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

$menu->open();

The primary take aways in the above example are:

  • Initially, every item has ItemExtra false
  • In item callback, we call showItemExtra and do a redraw.

The result is a PHP warning: Warning: str_repeat(): Second argument has to be greater than or equal to 0 in /path/to/cli-menu/src/CliMenu.php on line 319 and 319 is the following line of code in CliMenu::drawMenuItem:

str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))),

In addition, the menu renders incorrectly, the ItemExtra renders outside of the menu box.

Hope that's enough to go on, cheers! Great menu system, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions