vxm/yii2-console-menu

Pretty console menu for Yii2

1.0.0 2019-04-10 14:52 UTC

This package is auto-updated.

Last update: 2024-12-17 06:29:42 UTC


README

Latest Stable Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality Yii2

About it

An extension support build pretty menu base on nunomaduro/laravel-console-menu, and is a php-school/cli-menu wrapper for Yii2 console controller.

Requirements

Installation

Require Yii2 console menu using Composer:

composer require vxm/yii2-console-menu

Usage

Quick Setup

use yii\console\Controller;

/**
 * @method \vxm\consoleMenu\Menu menu(string $title = '', array $options = [])
*/
class TestController extends Controller
{
    /**
     * Execute the action.
     *
     * @return void
     */
    public function actionTest()
    {
        $option = $this->menu('Pizza menu', [
            'Freshly baked muffins',
            'Freshly baked croissants',
            'Turnovers, crumb cake, cinnamon buns, scones',
        ])->open();

        $this->stdout("You have chosen the option number #$option");
    }
}

Setup with a question

use yii\console\Controller;

/**
 * @method \vxm\consoleMenu\Menu menu(string $title = '', array $options = [])
*/
class TestController extends Controller
{
    /**
     * Execute the action.
     *
     * @return void
     */
    public function actionTest()
    {
        $option = $this->menu('Pizza menu')
                    ->addOption('mozzarella', 'Mozzarella')
                    ->addOption('chicken_parm', 'Chicken Parm')
                    ->addOption('sausage', 'Sausage')
                    ->addQuestion('Make your own', 'Describe your pizza...')
                    ->addOption('burger', 'Prefer burgers')
                    ->setWidth(80)
                    ->open();
        
        $this->stdout("You have chosen the text option: $option");
    }
}

Setup with advanced option, in this case, a password

use yii\console\Controller;

/**
 * @method \vxm\consoleMenu\Menu menu(string $title = '', array $options = [])
*/
class TestController extends Controller
{
    /**
     * Execute the action.
     *
     * @return void
     */
    public function actionTest()
    {
        $menu = $this->menu('Pizza menu')
                    ->addOption('mozzarella', 'Mozzarella')
                    ->addOption('chicken_parm', 'Chicken Parm')
                    ->addOption('sausage', 'Sausage')
                    ->addQuestion('Make your own', 'Describe your pizza...');
        
        $itemCallable = function (\PhpSchool\CliMenu\CliMenu $cliMenu) use ($menu) {
            $cliMenu->askPassword()
                ->setValidator(function ($password) {
                    return $password === 'secret';
                })
                ->setPromptText('Secret password?')
                ->ask();

            $menu->setResult('Free spice!');

            $cliMenu->close();
        };
        $menu->addItem('Add extra spice for free (password needed)', $itemCallable);


        $option = $menu->addOption('burger', 'Prefer burgers')
            ->setWidth(80)
            ->open();

        $this->stdout("You have chosen the text option: $option");
    }
}

Appearance

Available colors: black, red, green, yellow, blue, magenta, cyan, white.

  $this->menu($title, $options)
      ->setForegroundColour('green')
      ->setBackgroundColour('black')
      ->setWidth(200)
      ->setPadding(10)
      ->setMargin(5)
      ->setExitButtonText("Abort") // remove exit button with ->disableDefaultItems()
      ->setUnselectedMarker('')
      ->setSelectedMarker('')
      ->setTitleSeparator('*-')
      ->addLineBreak('<3', 2)
      ->addStaticItem('AREA 2')
      ->open();

Check out the full documentation here.