Skip to content
amterpPublic

About

A flexible, testable framework for CLI interactivity in Go.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

radish

A small, open, testable terminal-interactivity library for Go.

Most TUI prompt libraries are built on a general-purpose, timing-based renderer that reads a real terminal in raw mode. That makes them lovely to use and nearly impossible to test: you end up mocking the whole prompt, leaving the real rendering and selection logic uncovered.

radish takes a different shape. Each prompt is a pure Model - all state, logic, and rendering, with no I/O - sitting behind a thin, swappable I/O edge (EventSource + FrameSink). In production the edge is a real terminal; in tests it's a scripted list of keystrokes and a recording sink. The same Model and View() run in both, so your tests exercise the real prompt end-to-end - deterministically - instead of mocking it away.

It's also deliberately open where comparable libraries are closed: the matcher, theme, key bindings, event source, and render target are all injectable, each with a sane default so the simple case stays a one-liner.

Install

go get github.com/amterp/radish

Quick start

model := radish.NewSelect().
    Title("Pick a fruit (type to filter)").
    Options("Apple", "Banana", "Cherry", "Date")

choice, ok, err := radish.RunSelect(model, os.Stdin, os.Stderr)
if errors.Is(err, radish.ErrNotInteractive) {
    // stdin isn't a TTY - fall back however you like
}
if !ok {
    return // canceled
}
fmt.Println(choice)

Arrow keys move, typing filters, Enter selects, Esc/Ctrl-C cancels. The menu draws to the writer you pass (conventionally stderr), keeping stdout clean for your program's actual result. When stdin isn't a terminal, RunTerminal returns ErrNotInteractive without touching the terminal, so you control the fallback.

Try it: go run ./examples/pick.

Testing - the point

Drive the real prompt with scripted keystrokes and inspect every rendered frame. No terminal, no mocking:

model := radish.NewSelect().Title("Pick").Options("Apple", "Banana", "Cherry")

driver := radish.NewScriptDriver([]radish.Event{
    radish.KeyEvent(radish.KeyDown),
    radish.KeyEvent(radish.KeyEnter),
})
driver.Run(model)

got, _ := model.Selected() // "Banana"
frames := driver.Frames()  // every frame, in order

Frames() returns the initial render, the frame after each keystroke, and the final collapsed summary - ideal for snapshot tests. Disable color (color.NoColor = true) and frames come out as clean plain text.

A program that prints output between prompts can route those writes through driver.Print(s) in tests. Transcript() then returns the printed text and the frames in the order they happened, which is the run as it would read in scrollback.

Customize

Everything below is optional; the defaults are sensible.

radish.NewSelect().
    Title("Pick").
    Options(opts...).
    Matcher(myMatcher).   // how typing filters; default is case-insensitive fuzzy
    Theme(myTheme).       // amterp/color styles; default is tasteful
    KeyMap(myKeyMap).     // rebind navigation; default is arrows + enter + esc
    Height(8).            // visible rows before the list scrolls
    Width(cols)           // truncate long labels to terminal width
  • Matcher func(filter, label string) (matched bool, rank int) - inject your own matching/ranking; DefaultMatcher is a fuzzy subsequence match.
  • Theme - a flat struct of *color.Color styles (via amterp/color); nil fields render plain.
  • KeyMap - []KeyType slices per action; trivially remappable.

The prompt family

All of them share the Model/Run seam and the same testability story:

  • Select - single-select with live type-to-filter. RunSelect.
  • MultiSelect - multi-select; Tab/Space toggles, with optional Min/Max bounds (Max blocks extra toggles, Min gates submit). Reuses Select's filter/viewport core. RunMultiSelect.
  • Input - single-line text with an inline Prompt, optional Title heading and Placeholder, and an Echo mode: EchoNormal, EchoMasked (•), or EchoNone (sudo-style no-echo for secrets - nothing is rendered as you type, so the value never appears in any frame). RunInput.
  • Choice - one line of options, each a key and a label; a single keystroke picks one, with no Enter. Options can be Hidden (they act but are not drawn), Dim, or disabled with a reason shown when their key is pressed. When the row is wider than Width, it drops the labels and draws only the keys. RunChoice.

There is intentionally no Confirm widget: a yes/no prompt is a Choice with two options, or an Input whose result the caller interprets, so radish stays minimal and the policy lives with the caller.

// MultiSelect
picks, ok, err := radish.RunMultiSelect(
    radish.NewMultiSelect().Title("Pick toppings").Options(opts...).Max(3),
    os.Stdin, os.Stderr)

// Input (secret)
pw, ok, err := radish.RunInput(
    radish.NewInput().Prompt("Password > ").Echo(radish.EchoNone),
    os.Stdin, os.Stderr)

// Choice
chosen, ok, err := radish.RunChoice(
    radish.NewChoice().Prompt("file.txt › ").Options(
        radish.Option{Key: radish.RuneEvent('o'), Label: "open"},
        radish.Option{Key: radish.RuneEvent('d'), Label: "delete"},
    ).Disable(radish.RuneEvent('d'), "read-only"),
    os.Stdin, os.Stderr)

Try them: go run ./examples/multiselect, go run ./examples/input, go run ./examples/choice.

To run several prompts in a row - an action, then its confirmation - open one Terminal and run each prompt on it. One read from the terminal can deliver several keys, and a Terminal hands the ones a prompt did not consume to the next prompt; the one-shot RunX functions drop them. The terminal leaves raw mode between prompts, so printing in between works as usual.

term, err := radish.NewTerminal(os.Stdin, os.Stderr)
// ...
action, ok, err := term.Choice(actionPrompt)
confirmed, ok, err := term.Choice(confirmPrompt)
name, ok, err := term.Input(radish.NewInput().Prompt("Reason: "))

Known limitations: the prompt reads terminal width once at construction, so a mid-prompt resize (SIGWINCH) may misalign one redraw; the seam can carry a resize event later without changing the Model contract. Input truncates an over-width line at the edges rather than horizontally scrolling - the full value is always returned regardless of display.

License

MIT - see LICENSE.

About

A flexible, testable framework for CLI interactivity in Go.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages