Documentation
¶
Index ¶
- Constants
- Variables
- func Color(text string, codes ...string) string
- func FilterIndices(items []string, query string) []int
- func FuzzyScore(label, query string) (float64, bool)
- func RadioItemPlain(item RadioItem) string
- func ReconcileCursor(filtered []int, cursor int) (int, int)
- func ResetMenuNavigationHandler(tok NavigationToken)
- func RestoreTerminal()
- func SetMaskedSecretPrompt(fn func(prompt string) (string, error))
- func SwapDefault(c *Console) func()
- type Attr
- type Console
- func (c *Console) Color(text string, codes ...string) string
- func (c *Console) CursesChecklist(ctx context.Context, title string, items []string, ...) (map[int]bool, error)
- func (c *Console) CursesRadiolist(ctx context.Context, title string, items []RadioItem, selected int, ...) (int, error)
- func (c *Console) CursesSingleSelect(ctx context.Context, title string, items []string, defaultIndex int, ...) (int, bool, error)
- func (c *Console) FlushStdin()
- func (c *Console) FormatRadioItemANSI(item RadioItem) string
- func (c *Console) Input(prompt string) (string, error)
- func (c *Console) Interactive() bool
- func (c *Console) LineInput(prompt string) (string, error)
- func (c *Console) LineInputDefault(prompt, title, fallback string) (string, error)
- func (c *Console) MaskedSecret(prompt string) (string, error)
- func (c *Console) OpenFullScreen() (Screen, func(), error)
- func (c *Console) PrintError(text string)
- func (c *Console) PrintHeader(text string)
- func (c *Console) PrintInfo(text string)
- func (c *Console) PrintSuccess(text string)
- func (c *Console) PrintTruncated(ctx context.Context, more *int, hint string)
- func (c *Console) PrintWarning(text string)
- func (c *Console) Prompt(question, def string, password bool) string
- func (c *Console) PromptYesNo(ctx context.Context, question string, def bool) bool
- func (c *Console) ReadLine() (string, error)
- func (c *Console) SecretLine(prompt, mask string) (string, error)
- func (c *Console) ShouldUseColor() bool
- func (c *Console) Size() (int, int, error)
- func (c *Console) StdinIsTTY() bool
- func (c *Console) StdoutIsTTY() bool
- type LineAsker
- type MenuNavigationEvent
- type MenuNavigationHandler
- type MenuNavigationStart
- type NavigationToken
- type RadioItem
- type RadiolistOptions
- type Screen
- type SearchState
- type Segment
- type Theme
Constants ¶
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" )
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
const ( ColorGreen = 2 ColorYellow = 3 ColorCyan = 6 ColorWhite = 7 )
Curses foreground color numbers for InitPairs
Variables ¶
var ErrCursesInit = errCursesInit
ErrCursesInit marks a screen that could not be set up, the condition menus route to their text fallback
var ErrEOF = errors.New("EOFError")
ErrEOF is the EOFError of a prompt reading a closed input
var ErrInterrupt = errors.New("KeyboardInterrupt")
ErrInterrupt is the KeyboardInterrupt of a prompt or menu
var ErrNoEditor = errEditorUnavailable
ErrNoEditor reports a terminal that cannot draw an inline field
Functions ¶
func FilterIndices ¶
FilterIndices returns matching item indices best first with ties in catalog order
func FuzzyScore ¶
FuzzyScore sums per token scores of a multi token query, false when any token misses
func RadioItemPlain ¶
RadioItemPlain flattens a radiolist row to its searchable text
func ReconcileCursor ¶
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 ¶
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 ¶
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) 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 ¶
FormatRadioItemANSI applies ANSI colors to a styled radiolist row
func (*Console) Interactive ¶
Interactive reports whether prompts draw their live form on this console
func (*Console) LineInput ¶
LineInput reads one line in an inline field with editing keys on a terminal and as plain input otherwise
func (*Console) LineInputDefault ¶
LineInputDefault reads one line whose field shows the value a bare Enter keeps
func (*Console) MaskedSecret ¶
MaskedSecret reads one hidden value through the masked secret prompt
func (*Console) OpenFullScreen ¶
Full screen surface standing in for curses.wrapper, the injected opener first
func (*Console) PrintError ¶
PrintError prints a red cross line
func (*Console) PrintHeader ¶
PrintHeader prints a yellow header after a blank line
func (*Console) PrintSuccess ¶
PrintSuccess prints a green check line
func (*Console) PrintTruncated ¶
PrintTruncated prints the footer of a capped listing, nil more meaning at least one hidden row
func (*Console) PrintWarning ¶
PrintWarning prints a yellow warning line
func (*Console) Prompt ¶
Prompt reads a stripped answer or the default on plain Enter, empty after printing a newline on Ctrl-C or EOF
func (*Console) PromptYesNo ¶
Asks a localized yes or no question, a bare answer keeping the default
func (*Console) ReadLine ¶
ReadLine reads one line of input without printing a prompt, interrupt and end of input as errors
func (*Console) SecretLine ¶
SecretLine reads one hidden line in an inline masked field, errEditorUnavailable off a capable terminal
func (*Console) ShouldUseColor ¶
ShouldUseColor is false under NO_COLOR or a dumb TERM and otherwise follows whether output is a terminal
func (*Console) StdinIsTTY ¶
Reports whether the input stream is a terminal, the isatty of sys.stdin
func (*Console) StdoutIsTTY ¶
Reports whether the output stream is a terminal, the isatty of sys.stdout
type LineAsker ¶
Input stream that answers each line a console reads as the question its prompt asks
type MenuNavigationEvent ¶
type MenuNavigationEvent string
MenuNavigationEvent names a scoped navigation notification
const ( )
type MenuNavigationHandler ¶
type MenuNavigationHandler func(event MenuNavigationEvent, value ...any) (*MenuNavigationStart, error)
MenuNavigationHandler receives begin, resolve, cancel and back events, an error aborting the menu
type MenuNavigationStart ¶
type MenuNavigationStart struct {
}
MenuNavigationStart is what a scoped handler answers when a menu begins
type NavigationToken ¶
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 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 ¶
SearchState is the slash search prompt state of a menu
type Segment ¶
Segment is one styled run of a radiolist row, Style empty, yellow, dim, green or red
func GaugeBarSegments ¶
GaugeBarSegments splits text so each ten cell usage gauge is its own styled segment