PHP code example of sugarcraft / sugar-readline

1. Go to this page and download the library: Download sugarcraft/sugar-readline library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sugarcraft / sugar-readline example snippets


use SugarCraft\Readline\{Key, TextPrompt};

$p = TextPrompt::new('Enter your name: ')
    ->withDefault('Anonymous')
    ->withCompletions(['Alice', 'Bob', 'Carol']);

$p = $p->handleChar('A')->handleChar('l')->handleKey(Key::Tab)->submit();
echo $p->value();  // 'Alice'

use SugarCraft\Readline\SelectionPrompt;

$p = SelectionPrompt::new('Choose a fruit:', ['Apple', 'Banana', 'Cherry', 'Date'])
    ->withFilter('an');                 // Banana matches
echo $p->selectedValue();              // 'Banana'

use SugarCraft\Readline\{Key, MultiSelectPrompt};

$p = MultiSelectPrompt::new('Pick:', ['A', 'B', 'C'])
    ->withMinSelections(1)
    ->handleKey(Key::Space)              // mark A
    ->handleKey(Key::Down)
    ->handleKey(Key::Space)              // mark B
    ->handleKey(Key::Enter);             // submit (min satisfied)

print_r($p->selectedValues());          // ['A', 'B']

use SugarCraft\Readline\{ConfirmationPrompt, Key};

$p = ConfirmationPrompt::new('Delete file?')
    ->handleKey('n')                     // selects No (does not auto-submit)
    ->handleKey(Key::Left)               // changes mind back to Yes
    ->submit();
echo $p->result() ? 'yes' : 'no';       // 'yes'