php-school / cli-menu

🖥 Build beautiful PHP CLI menus. Simple yet Powerful. Expressive DSL.
http://www.phpschool.io
MIT License
1.94k stars 107 forks source link

preserving selection index as you move up/down #279

Open rbro opened 12 months ago

rbro commented 12 months ago

I'm trying to see if I can use cli-menu to build a simple table/grid with rows and columns, similar to a spreadsheet.

I realize this was not the intent of cli-menu, but I can get pretty close with SplitItem's. I feel this could be an interesting use of cli-menu.

With the below script, the only usability item I noticed is if I go to cell B2 and press up, it brings me to cell A1, rather than B1 as I was hoping.

Is there a way an option could be set so that when you move up or down, cli-menu can set the index of the selected item to be the index of the item you had last selected?

Thanks for your help.

<?php
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\Builder\SplitItemBuilder;
use PhpSchool\CliMenu\CliMenu;

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

$menu = (new CliMenuBuilder)
    ->setWidth(150)
    ->addStaticItem('Below is a SplitItem')
    ->addSplitItem(function (SplitItemBuilder $b) use ($itemCallable) {
        $b->setGutter(5)
            ->addItem('A1', $itemCallable)
            ->addItem('B1', $itemCallable)
            ->addItem('C1', $itemCallable);
    })
    ->addSplitItem(function (SplitItemBuilder $b) use ($itemCallable) {
        $b->setGutter(5)
            ->addItem('A2', $itemCallable)
            ->addItem('B2', $itemCallable)
            ->addItem('C2', $itemCallable);
    })
    ->addSplitItem(function (SplitItemBuilder $b) use ($itemCallable) {
        $b->setGutter(5)
            ->addItem('A3', $itemCallable)
            ->addItem('B3', $itemCallable)
            ->addItem('C3', $itemCallable);
    })
    ->addSplitItem(function (SplitItemBuilder $b) use ($itemCallable) {
        $b->setGutter(5)
            ->addItem('A4', $itemCallable)
            ->addItem('B4', $itemCallable)
            ->addItem('C4', $itemCallable);
    })
    ->build();

$menu->open();