cliui

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 26 Imported by: 1 module, 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

Index

Constants

View Source
const (
	Reset   = "\033[0m"
	Bold    = "\033[1m"
	Dim     = "\033[2m"
	Red     = "\033[31m"
	Green   = "\033[32m"
	Yellow  = "\033[33m"
	Blue    = "\033[34m"
	Magenta = "\033[35m"
	Cyan    = "\033[36m"
)
View Source
const (
	KeyDown      = 258
	KeyUp        = 259
	KeyLeft      = 260
	KeyRight     = 261
	KeyHome      = 262
	KeyBackspace = 263
	KeyF0        = 264
	KeyDC        = 330
	KeyIC        = 331
	KeyNPage     = 338
	KeyPPage     = 339
	KeyEnter     = 343
	KeyEnd       = 360
)

Curses key codes the keypad translation produces

View Source
const (
	ColorGreen  = 2
	ColorYellow = 3
	ColorCyan   = 6
	ColorWhite  = 7
)

Curses foreground color numbers for InitPairs

Variables

View Source
var ErrCursesInit = errCursesInit

ErrCursesInit marks a screen that could not be set up, the condition menus route to their text fallback

View Source
var ErrEOF = errors.New("EOFError")

ErrEOF is the EOFError of a prompt reading a closed input

View Source
var ErrInterrupt = errors.New("KeyboardInterrupt")

ErrInterrupt is the KeyboardInterrupt of a prompt or menu

View Source
var ErrNoEditor = errEditorUnavailable

ErrNoEditor reports a terminal that cannot draw an inline field

Functions

func Color

func Color(text string, codes ...string) string

Color applies codes on the process console

func FilterIndices

func FilterIndices(items []string, query string) []int

FilterIndices returns matching item indices best first with ties in catalog order

func FuzzyScore

func FuzzyScore(label, query string) (float64, bool)

FuzzyScore sums per token scores of a multi token query, false when any token misses

func RadioItemPlain

func RadioItemPlain(item RadioItem) string

RadioItemPlain flattens a radiolist row to its searchable text

func ReconcileCursor

func ReconcileCursor(filtered []int, cursor int) (int, int)

ReconcileCursor keeps the cursor on a visible row, returning it and its position

func ResetMenuNavigationHandler

func ResetMenuNavigationHandler(tok NavigationToken)

ResetMenuNavigationHandler restores the handler active before the token was issued

func RestoreTerminal

func RestoreTerminal()

RestoreTerminal clears the screen modes and raw mode of every terminal a prompt holds

func SetMaskedSecretPrompt

func SetMaskedSecretPrompt(fn func(prompt string) (string, error))

SetMaskedSecretPrompt installs the masked secret reader password prompts use on every console

func SwapDefault

func SwapDefault(c *Console) func()

SwapDefault makes c the process console, the returned func restoring the previous one

Types

type Attr

type Attr struct {
	Bold bool
	Dim  bool
	Pair int
}

Attr is a curses style attribute, Pair naming color pair one green, two yellow, three dim gray, four red

type Console

type Console struct {
	In  io.Reader
	Out io.Writer
	// Process environment lookup for NO_COLOR, TERM and ESCDELAY
	Getenv func(string) (string, bool)
	// TTY checks standing in for isatty on the input and output streams
	StdinTTY  func() bool
	StdoutTTY func() bool
	// Terminal rows and columns of the output stream
	TermSize func() (int, int, error)
	// Masked secret reader used by password prompts
	MaskedSecretPrompt func(prompt string) (string, error)
	// Full screen opener standing in for curses.wrapper, an error routes menus to the numbered fallback
	OpenScreen func() (Screen, func(), error)
	// contains filtered or unexported fields
}

Console is the terminal a prompt or menu reads from and draws on

func Default

func Default() *Console

Default is the process console over standard input and output

func (*Console) Color

func (c *Console) Color(text string, codes ...string) string

Color wraps text in the codes and a reset when color output is appropriate

func (*Console) CursesChecklist

func (c *Console) CursesChecklist(ctx context.Context, title string, items []string, selected, cancelReturns map[int]bool, statusFn func(map[int]bool) string) (map[int]bool, error)

CursesChecklist is a multiple choice list, cancel returning cancelReturns or the original selection

func (*Console) CursesRadiolist

func (c *Console) CursesRadiolist(ctx context.Context, title string, items []RadioItem, selected int, opts RadiolistOptions) (int, error)

CursesRadiolist is a single choice list returning the original index of the chosen row

func (*Console) CursesSingleSelect

func (c *Console) CursesSingleSelect(ctx context.Context, title string, items []string, defaultIndex int, cancelLabel string, searchable bool) (int, bool, error)

CursesSingleSelect picks one item or the cancel row, ok false on cancel

func (*Console) FlushStdin

func (c *Console) FlushStdin()

FlushStdin drops unread terminal input left behind by a full screen surface, a no op off a terminal

func (*Console) FormatRadioItemANSI

func (c *Console) FormatRadioItemANSI(item RadioItem) string

FormatRadioItemANSI applies ANSI colors to a styled radiolist row

func (*Console) Input

func (c *Console) Input(prompt string) (string, error)

Reads one line like Python input

func (*Console) Interactive

func (c *Console) Interactive() bool

Interactive reports whether prompts draw their live form on this console

func (*Console) LineInput

func (c *Console) LineInput(prompt string) (string, error)

LineInput reads one line in an inline field with editing keys on a terminal and as plain input otherwise

func (*Console) LineInputDefault

func (c *Console) LineInputDefault(prompt, title, fallback string) (string, error)

LineInputDefault reads one line whose field shows the value a bare Enter keeps

func (*Console) MaskedSecret

func (c *Console) MaskedSecret(prompt string) (string, error)

MaskedSecret reads one hidden value through the masked secret prompt

func (*Console) OpenFullScreen

func (c *Console) OpenFullScreen() (Screen, func(), error)

Full screen surface standing in for curses.wrapper, the injected opener first

func (*Console) PrintError

func (c *Console) PrintError(text string)

PrintError prints a red cross line

func (*Console) PrintHeader

func (c *Console) PrintHeader(text string)

PrintHeader prints a yellow header after a blank line

func (*Console) PrintInfo

func (c *Console) PrintInfo(text string)

PrintInfo prints a dim indented line

func (*Console) PrintSuccess

func (c *Console) PrintSuccess(text string)

PrintSuccess prints a green check line

func (*Console) PrintTruncated

func (c *Console) PrintTruncated(ctx context.Context, more *int, hint string)

PrintTruncated prints the footer of a capped listing, nil more meaning at least one hidden row

func (*Console) PrintWarning

func (c *Console) PrintWarning(text string)

PrintWarning prints a yellow warning line

func (*Console) Prompt

func (c *Console) Prompt(question, def string, password bool) string

Prompt reads a stripped answer or the default on plain Enter, empty after printing a newline on Ctrl-C or EOF

func (*Console) PromptYesNo

func (c *Console) PromptYesNo(ctx context.Context, question string, def bool) bool

Asks a localized yes or no question, a bare answer keeping the default

func (*Console) ReadLine

func (c *Console) ReadLine() (string, error)

ReadLine reads one line of input without printing a prompt, interrupt and end of input as errors

func (*Console) SecretLine

func (c *Console) SecretLine(prompt, mask string) (string, error)

SecretLine reads one hidden line in an inline masked field, errEditorUnavailable off a capable terminal

func (*Console) ShouldUseColor

func (c *Console) ShouldUseColor() bool

ShouldUseColor is false under NO_COLOR or a dumb TERM and otherwise follows whether output is a terminal

func (*Console) Size

func (c *Console) Size() (int, int, error)

Size reports the terminal rows and columns of the output

func (*Console) StdinIsTTY

func (c *Console) StdinIsTTY() bool

Reports whether the input stream is a terminal, the isatty of sys.stdin

func (*Console) StdoutIsTTY

func (c *Console) StdoutIsTTY() bool

Reports whether the output stream is a terminal, the isatty of sys.stdout

type LineAsker

type LineAsker interface {
	AskLine(prompt string) (string, error)
}

Input stream that answers each line a console reads as the question its prompt asks

type MenuNavigationEvent string

MenuNavigationEvent names a scoped navigation notification

const (
	NavEventBegin   MenuNavigationEvent = "begin"
	NavEventResolve MenuNavigationEvent = "resolve"
	NavEventCancel  MenuNavigationEvent = "cancel"
	NavEventBack    MenuNavigationEvent = "back"
)
type MenuNavigationHandler func(event MenuNavigationEvent, value ...any) (*MenuNavigationStart, error)

MenuNavigationHandler receives begin, resolve, cancel and back events, an error aborting the menu

type MenuNavigationStart struct {
	AllowBack   bool
	GuardCancel bool
	Replay      bool
	ReplayValue any
}

MenuNavigationStart is what a scoped handler answers when a menu begins

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

NavigationToken restores the process handler active before a set

func SetMenuNavigationHandler

func SetMenuNavigationHandler(h MenuNavigationHandler) NavigationToken

SetMenuNavigationHandler scopes cancel and back behavior to the current CLI invocation for callers without a ctx

type RadioItem

type RadioItem []Segment

RadioItem is a radiolist row as styled segments

func PlainItem

func PlainItem(text string) RadioItem

PlainItem is a radiolist row with no styling

type RadiolistOptions

type RadiolistOptions struct {
	CancelReturns *int
	Description   string
	Searchable    bool
	SearchLabels  []string
	Headings      map[int]string
}

RadiolistOptions are the optional arguments of a radiolist, nil CancelReturns meaning the selection

type Screen

type Screen interface {
	// Next key code, keypad sequences translated, minus one when the timeout passes
	Getch() (int, error)
	// Read timeout in milliseconds, negative blocking
	Timeout(ms int)
	Clear()
	GetMaxYX() (int, int)
	// Writes at most n characters at a cell, an error meaning the write fell off the screen
	AddNStr(y, x int, text string, n int, attr Attr) error
	Refresh() error
	HasColors() bool
	// Hides the cursor and sets up the color pairs, failing like curs_set or start_color on a limited terminal
	InitColors(extraPairs bool) error
	// Hides the cursor, then with colors maps each pair to a curses foreground number over the default background
	InitPairs(pairs map[int]int) error
	// Number of terminal colors, the curses COLORS value
	Colors() int
}

Screen is the curses window surface menus draw on and read keys from

type SearchState

type SearchState struct {
	Active bool
	Query  string
}

SearchState is the slash search prompt state of a menu

type Segment

type Segment struct {
	Text  string
	Style string
}

Segment is one styled run of a radiolist row, Style empty, yellow, dim, green or red

func GaugeBarSegments

func GaugeBarSegments(text string) []Segment

GaugeBarSegments splits text so each ten cell usage gauge is its own styled segment

type Theme

type Theme struct {
	Accent string
	Frost  string
	Muted  string
	Faint  string
	OK     string
	Warn   string
	Err    string
}

Theme is the palette prompts draw with, every tone a hex color

Jump to

Keyboard shortcuts

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