Interactive prompts — text, confirm, select, multi-select, textarea
Text, Confirm, Selection, MultiSelect, and Textarea prompts with full keyboard handling, filter-as-you-type, and pagination. State-machine model — no external readline dependency.
composer require sugarcraft/sugar-readline
use SugarCraft\Readline\{Key, MultiSelectPrompt, SelectionPrompt, TextPrompt};
// Prompts are immutable state machines: feed input, read the result.
$name = TextPrompt::new('Your name:')
->handleChar('A')->handleChar('d')->handleChar('a')
->submit();
$name->value(); // 'Ada'
$lang = SelectionPrompt::new('Pick a language:', ['PHP', 'Go', 'Rust', 'Python'])
->handleKey(Key::Down)
->handleKey(Key::Enter);
$lang->selectedValue(); // 'Go'
// Multi-select with constraints: Space toggles, Enter submits only when allowed
$tools = MultiSelectPrompt::new('Pick tools:', ['vim', 'git', 'docker', 'php'])
->withMinSelections(1)
->withMaxSelections(3)
->handleKey(Key::Space)->handleKey(Key::Down)->handleKey(Key::Space)
->handleKey(Key::Enter);
$tools->selectedValues(); // ['vim', 'git']
| Class | Method | Description |
|---|---|---|
| TextPrompt | static new(label) / withDefault() / withHidden() / withCompletions() / withValidator() / withCharLimit() / withHistory() | Single-line input |
| TextPrompt | handleChar(char) / handleKey(key) / submit() / abort() / value() / isSubmitted() | Drive the prompt and read the result |
| ConfirmationPrompt | static new(label, defaultValue = false) / withConfirmLabel() / withCancelLabel() | Yes / no prompt |
| SelectionPrompt | static new(label, choices) / withPageSize() / withFilter() / handleKey() / selectedValue() | Single choice with filter and pagination |
| MultiSelectPrompt | static new(label, choices) / withMinSelections(n) / withMaxSelections(n) / handleKey() / selectedValues() / canSubmit() | Several choices with min/max enforcement |
| TextareaPrompt | static new(label) / withDefault() / withMaxLines() / handleChar() / handleKey() / value() | Multi-line editor |
| Key | Up, Down, Enter, Space, Escape, CtrlC, … | Key-name constants for handleKey() |
VHS-recorded GIFs of every example shipped with the library. Regenerated automatically on every push that touches the source.