interactive

package
v0.18.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package interactive is komando's interactive mode: the screens that help a person build a command line (C162). It returns the line as argv; internal/cli runs it as if it was typed, so every check of a typed line still applies. It reads keys and draws, nothing more: no command runs here (TestInteractiveImports).

Index

Constants

View Source
const MaxRows = 30

MaxRows is how many rows a list shows at once, as the line prompts do (promptMaxRows); typing narrows it.

Variables

View Source
var (
	// ErrQuit: Esc at the top of the menu. Nothing runs, nothing is said.
	ErrQuit = errors.New("left the menu")
	// ErrStop: Ctrl-C. Nothing runs.
	ErrStop = errors.New("stopped at the menu")
)

Functions

func Build added in v0.18.2

func Build(t Terminal, leaves []capability.Leaf, o Options) ([]string, error)

Build shows the menu of the command tree and returns the words of the command a person took, as they would have typed them. It only builds a line: it runs nothing, and a value is never part of it (a command's arguments are asked for by the line prompts that follow, as for a typed line). ErrQuit is Esc at the top, ErrStop Ctrl-C; any other error is the terminal's (term.ErrIdle, term.ErrTooSmall, io.EOF), which has put the terminal back already. On every way out the rows drawn are cleared.

func Draw

func Draw(l *List, s Style) []string

Draw is the list as lines, top to bottom, in an order that never changes: the question with what was typed, the help when asked for, the rows of the page, a line under them when there is something to say, and the key line. It only reads l.

func DrawForm

func DrawForm(f *Form, s Style) []string

DrawForm is the form as lines: the command's words, then the field being filled (a list as Draw shows it, or the question with what is typed), then its key line. It only reads f.

func DrawReview

func DrawReview(r *Review, s Style) []string

DrawReview is the review as lines: the question, the whole line with a $ first, then its keys. A line wider than the window wraps; no part of it is cut, so what runs is what was shown.

Types

type Answers

type Answers = map[string]string

Answers are a form's answers by field key.

type Field

type Field struct {
	Key      string // the answer's key: arg0, arg1 ... or flag:NAME
	Ask      string // the question
	Help     string
	Required bool
	Free     bool // with Rows: other text may be typed (tag:prod)
	Advanced bool // asked only after "more options"

	Default func(Answers) string // what an empty Enter takes; nil: nothing
	Rows    func(Answers) []Row  // the list it is picked from; nil: typed
	Show    func(Answers) bool   // asked only while true; nil: always

	// Check is why an answer is refused; "" takes it.
	Check func(a Answers, v string) string
	// Clean takes a password out of a pasted address (user:password@);
	// found says one was there. The screen never draws it.
	Clean func(v string) (clean string, found bool)
}

Field is one question of a form, as internal/cli describes it from the command and its form hint. A secret or a value komando makes is never a field: internal/cli leaves those out. The functions read the answers given so far and nothing else may change through them.

type Form

type Form struct {
	Title string
	// contains filtered or unexported fields
}

Form asks a command's fields one after another: the required ones first, then the other main ones; the advanced ones only when the person asks for more options. Its answers go back to internal/cli, which builds the line.

func NewForm

func NewForm(title string, fields []Field) *Form

NewForm is a form on its first field.

func (*Form) AtMore

func (f *Form) AtMore() bool

AtMore says whether the form asks whether to see more options.

func (*Form) Field

func (f *Form) Field() Field

Field is the field being filled; the zero Field on the more-options step.

func (*Form) Key

func (f *Form) Key(k term.Key) (Result, Answers)

Key applies one key. Take comes with the answers: only shown fields that have a value.

func (*Form) Text

func (f *Form) Text() string

Text is what is typed into the field being filled.

type List

type List struct {
	Question string
	Help     string
	Rows     []Row
	Free     bool
	// Banner is drawn above the question: the first screen's name and what
	// the tool does.
	Banner []string
	// Numbered draws the number of each row, and a digit typed on an empty
	// search jumps to that row (more digits make a longer number). Text a
	// person types in a list of names is search, so only the menu is numbered.
	Numbered bool
	// EscQuits says Esc leaves the screen instead of going back: the top
	// level of the menu.
	EscQuits bool
	// contains filtered or unexported fields
}

List is a list with type-to-search. Free: when no row matches, Enter takes the typed text as it is (a selector such as @web or tag:prod).

func NewList

func NewList(question, help string, rows []Row, free bool) *List

NewList is a list of rows with nothing typed, the first row highlighted.

func (*List) Current

func (l *List) Current() Row

Current is the highlighted row; the zero Row when nothing is shown.

func (*List) Fit

func (l *List) Fit(height int)

Fit sets the page to a window of height rows: at most MaxRows, at least one.

func (*List) Key

func (l *List) Key(k term.Key) (Result, string)

Key applies one key. Take comes with the value taken.

func (*List) Shown

func (l *List) Shown() []Row

Shown is the rows that match the search: on name and detail, ignoring case, the same match as the line prompts.

type Menu struct {
	// contains filtered or unexported fields
}

Menu is the first screen: the command tree of the capability table as nested lists, one level per word a person types. Enter on a word opens the words after it; Enter on a whole command takes it.

func NewMenu

func NewMenu(leaves []capability.Leaf) *Menu

NewMenu is the tree of the built leaves, at its top level.

func (m *Menu) Fit(height int)

Fit fits the level shown, and every level after it, to a window of height rows.

func (m *Menu) Key(k term.Key) (Result, capability.Leaf)

Key applies one key. Take comes with the leaf taken.

func (m *Menu) List() *List

List is the level shown, for Draw.

func (m *Menu) SetBanner(lines ...string)

SetBanner puts the name and what komando does above the top level, in the order given.

type Options added in v0.18.2

type Options struct {
	Color  bool
	Banner []string
}

Options are how Build draws the first screen: Color shows the highlighted row in reverse video too (never by colour alone: it starts with > either way), and Banner is the name and what komando does, above the question.

type Result

type Result int

Result is what a key did to a screen.

const (
	Stay Result = iota // nothing ends: draw again
	Take               // a value was taken
	Back               // Esc on an empty search: one level back
	Quit               // Ctrl-C: nothing runs
)

type Review

type Review struct {
	// contains filtered or unexported fields
}

Review is the last screen: the line built from the answers, as internal/cli prints it (quoted, secrets masked), before it runs.

func NewReview

func NewReview(line string) *Review

NewReview shows line, the words after komando.

func (*Review) Key

func (r *Review) Key(k term.Key) Result

Key applies one key: Enter runs the line, Esc goes back to the form, Ctrl-C quits.

type Row

type Row struct {
	Value, Label, Detail string
}

Row is one entry of a list: the value it stands for, the name shown and a detail beside it.

type Style

type Style struct {
	Width int
	Color bool
}

Style is how a screen is drawn: the window's width, and whether the highlighted row is also shown in reverse video (never by colour alone: it starts with > either way).

type Terminal added in v0.18.2

type Terminal interface {
	ReadKey() (term.Key, error)
	Write(p []byte) (int, error)
	Size() (w, h int)
	Suspend() error
}

Terminal is the screen session Build drives. *term.Screen is one; tests use a fake with no terminal behind it. Build never opens or restores the terminal: whoever opened it restores it, on every way out.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL