← All 41 libraries

SugarReadline

SugarReadline

Interactive prompts — text, confirm, select, multi-select, textarea

port of erikgeiser/promptkit promptinputreadlineinteractive

SugarReadline code coverage

Text, Confirm, Selection, MultiSelect, and Textarea prompts with full keyboard handling, filter-as-you-type, and pagination. State-machine model — no external readline dependency.

Install

composer require sugarcraft/sugar-readline

Quickstart

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']

What's in the box

TextPromptSingle-line input with default value, password masking, completions, validation, history and reverse search.
ConfirmationPromptYes / No with customizable labels and default.
SelectionPromptArrow-key selection with filter-as-you-type and pagination.
MultiSelectPromptMulti-item selection with min/max enforcement, marker (◉/○), pagination.
TextareaPromptMulti-line editor with cursor movement, line count display.

Source & demos

Try the quickstart →

API

ClassMethodDescription
TextPromptstatic new(label) / withDefault() / withHidden() / withCompletions() / withValidator() / withCharLimit() / withHistory()Single-line input
TextPrompthandleChar(char) / handleKey(key) / submit() / abort() / value() / isSubmitted()Drive the prompt and read the result
ConfirmationPromptstatic new(label, defaultValue = false) / withConfirmLabel() / withCancelLabel()Yes / no prompt
SelectionPromptstatic new(label, choices) / withPageSize() / withFilter() / handleKey() / selectedValue()Single choice with filter and pagination
MultiSelectPromptstatic new(label, choices) / withMinSelections(n) / withMaxSelections(n) / handleKey() / selectedValues() / canSubmit()Several choices with min/max enforcement
TextareaPromptstatic new(label) / withDefault() / withMaxLines() / handleChar() / handleKey() / value()Multi-line editor
KeyUp, Down, Enter, Space, Escape, CtrlC, …Key-name constants for handleKey()

Demos.

VHS-recorded GIFs of every example shipped with the library. Regenerated automatically on every push that touches the source.

Text prompt

Text prompt

Single-line text prompt with autocomplete.
Multi-select

Multi-select

Multi-select list with toggle + submit.