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

Style reset after 2nd submenu

jtreminio opened this issue · comments

<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;

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

$menu = (new CliMenuBuilder)
    ->setBackgroundColour('black')
    ->setForegroundColour('green')
    ->setTitle('Color Test #1')
    ->addLineBreak()
    ->addSubMenu('Submenu #1', function (CliMenuBuilder $b) {
        $b->setTitle('Color Test #2')
        ->addLineBreak()
        ->addSubMenu('Submenu #2', function (CliMenuBuilder $b) {
            $b->setTitle('Color Test #3')
            ->addLineBreak()
            ->addItem('Foo #6', function (CliMenu $menu) {})
            ->addItem('Foo #7', function (CliMenu $menu) {})
            ->addLineBreak();
        })
        ->addItem('Foo #4', function (CliMenu $menu) {})
        ->addItem('Foo #5', function (CliMenu $menu) {})
        ->addLineBreak();
    })
    ->addItem('Foo #2', function (CliMenu $menu) {})
    ->addItem('Foo #3', function (CliMenu $menu) {})
    ->addLineBreak()
    ->build();

$menu->open();

Peek 2019-12-18 09-33

It appears CliMenuBuilder::addSubMenu() returns false for

        if (!$menu->getStyle()->hasChangedFromDefaults()) {
            $menu->setStyle($this->menu->getStyle());
        }

I can step through and verify this, but the MenuStyle is only applied to the first submenu, nothing afterward unless I set the values again.

That's odd, definitely a bug

A quick look says it's broken. When building the 3rd menu, the second menu hasn't been built yet, so it's not had the first menu's style set on to it. It has the default style. So the 3rd menu inherits the seconds default style.

Adding menus and items executes their code immediately (other than user-provided callbacks).

If the code is changed so that CliMenuBuilder::addSubMenu() and CliMenuBuilder::addSubMenuFromBuilder() persist anonymous functions vs an instantiated CliMenu object, and then run that code on initial submenu being opened, we can avoid this race condition.

I'm thinking CliMenu could be somewhat like a container, like the old Pimple.

Not sure I’m following but I’d be interested in seeing a prototype 😀

Fixed with #210.