tui

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package tui draws the small interactive menus of `agentguard setup`: a list you move through with the arrow keys and pick from with Enter. It draws in place in the normal terminal (no full-screen mode) and leaves a one-line record of each answer, so the scrollback reads like a log of what was chosen.

The menu logic (Menu, ParseKeys, Run) works on plain readers and writers so tests drive it with scripted key presses; Terminal adds the real terminal: raw mode for the duration of a menu, and colour when the terminal supports it.

Index

Constants

View Source
const Back = -1

Back is the choice Run returns when the user presses Esc or q.

Variables

View Source
var ErrNotTerminal = errors.New("not an interactive terminal")

ErrNotTerminal means stdin or stdout isn't an interactive terminal (a pipe, a file, CI), so no menu can be shown.

View Source
var ErrQuit = errors.New("quit")

ErrQuit is returned when the user presses Ctrl-C (or Ctrl-D) in a menu.

Functions

func ColorWanted

func ColorWanted() bool

ColorWanted reports whether colour output is welcome: NO_COLOR unset (https://no-color.org) and not a dumb terminal.

func Run

func Run(r io.Reader, w io.Writer, s Style, m *Menu, newline string) (int, error)

Run draws m on w, reads key presses from r until one picks an option, and returns its index (Back for Esc or q, ErrQuit for Ctrl-C). The menu is then replaced by one line recording the answer. newline is what ends a line: "\r\n" while the terminal is in raw mode.

Types

type Key

type Key int

Key is a key press that means something to a menu.

const (
	KeyNone Key = iota
	KeyUp
	KeyDown
	KeyEnter
	KeyBack
	KeyQuit
)

The keys a menu understands. Everything else is ignored.

func ParseKeys

func ParseKeys(b []byte) []Key

ParseKeys turns raw terminal input into keys: the arrow keys (as the ESC [ A / ESC O A sequences terminals send), k/j, Enter, Esc or q for back, and Ctrl-C/Ctrl-D to quit. Unknown escape sequences are skipped whole, so a stray one never reads as Esc.

type Menu struct {
	Title   string
	Options []Option
	Cursor  int
}

Menu is one selectable list and where the cursor is in it.

func NewMenu

func NewMenu(title string, opts []Option, def int) *Menu

NewMenu starts the cursor on def: on the next selectable option when def is a separator, on the first one when def is out of range.

func (m *Menu) Move(delta int)

Move steps the cursor by delta (±1), wrapping around the ends and skipping separators.

func (m *Menu) Render(s Style) []string

Render is the menu as lines, without line endings.

type Option

type Option struct {
	Label string
	Hint  string // shown dimmed after the label
	// Separator makes this line a divider: drawn, never selectable.
	Separator bool
}

Option is one line of a menu.

type Style

type Style struct{ Color bool }

Style decides whether output carries ANSI colour.

func (Style) Accent

func (s Style) Accent(t string) string

func (Style) Bold

func (s Style) Bold(t string) string

Bold, Dim, Accent, Good and Warn style text when colour is on.

func (Style) Dim

func (s Style) Dim(t string) string

func (Style) Good

func (s Style) Good(t string) string

func (Style) Warn

func (s Style) Warn(t string) string

type Terminal

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

Terminal is the user's terminal, ready for menus.

func Open

func Open() (*Terminal, error)

Open checks that stdin and stdout are a terminal and prepares it: on Windows that switches the console to understand ANSI escape codes. Close undoes it.

func (*Terminal) Close

func (t *Terminal) Close()

Close puts the terminal back as it was.

func (*Terminal) Printf

func (t *Terminal) Printf(format string, a ...any)

Printf writes to the terminal.

func (*Terminal) Select

func (t *Terminal) Select(title string, opts []Option, def int) (int, error)

Select shows a menu and returns the chosen option's index, Back for Esc, or ErrQuit for Ctrl-C. The terminal is in raw mode only while the menu is up, so everything else prints normally.

func (*Terminal) Style

func (t *Terminal) Style() Style

Style is how this terminal shows colour.

Jump to

Keyboard shortcuts

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