ui

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 33 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 ui holds the interactive components: the small inline prompts (select lists, confirmations, text inputs, a spinner) used wherever the CLI asks the user something, which render inline and collapse to a one-line summary once answered, and the chat's full-screen Screen, which shows the same prompts in a panel.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCancelled is returned when the user dismisses a prompt (Esc, Ctrl+C).
	ErrCancelled = errors.New("cancelled")
	// ErrNotInteractive is returned when stdin or stdout is not a terminal;
	// callers fall back to flags or environment variables.
	ErrNotInteractive = errors.New("not an interactive terminal")
)
View Source
var Colors = Palette{
	Accent:  lipgloss.AdaptiveColor{Dark: "#a798d7", Light: "#7459b4"},
	Muted:   lipgloss.AdaptiveColor{Dark: "#9da5a9", Light: "#677176"},
	Dim:     lipgloss.AdaptiveColor{Dark: "#7e888e", Light: "#879095"},
	Success: lipgloss.AdaptiveColor{Dark: "#68b78d", Light: "#337e58"},
	Error:   lipgloss.AdaptiveColor{Dark: "#ea7f81", Light: "#c8253d"},
	Border:  lipgloss.AdaptiveColor{Dark: "#768186", Light: "#9aa2a7"},
}

Colors is the active palette (pi's dark/light tones by default).

View Source
var FileMarker = regexp.MustCompile(`\[(?:image|file) #(\d+)(?: · [^\[\]\n]*)?\]`)

FileMarker matches the marker of an attached file (attach.File.Marker).

View Source
var PasteMarker = regexp.MustCompile(`\[paste #(\d+) (?:\+\d+ lines|\d+ chars)\]`)

PasteMarker matches the marker a collapsed paste leaves in the editor.

Functions

func Confirm

func Confirm(title string, def bool) (bool, error)

Confirm asks a yes/no question on one line (y/n, ←/→, Enter takes the highlighted answer, def initially).

func ConfirmWith

func ConfirmWith(p Prompter, title string, def bool) (bool, error)

ConfirmWith asks the yes/no question with p.

func Copy

func Copy(text string, term io.Writer) error

Copy puts text on the clipboard: the system's, or the terminal's through an OSC 52 sequence written to term over SSH (where the system clipboard is the server's) and when the system clipboard fails (no xclip, say).

func DiscardInput

func DiscardInput()

DiscardInput drops what was typed ahead, so it cannot answer a prompt the user has not seen yet.

func HoldInput

func HoldInput() (restore func())

HoldInput turns off the terminal's echo while output streams: echoed typing moves the cursor under the in-place redraws. What is typed stays queued for the next prompt, and Ctrl+C still interrupts. restore turns the echo back on.

func Interactive

func Interactive() bool

Interactive reports whether prompts can run: stdin and stdout are terminals.

func NewlineKey

func NewlineKey() string

NewlineKey names the key for a new line in the editor: shift+enter, or ctrl+j where Shift+Enter cannot be told from Enter (Terminal.app, tmux without extended-keys, Windows).

func Unwrap

func Unwrap(lines []string, plain []Plain, a, b int) string

Unwrap returns the text of lines from column a of the first to column b (not included) of the last as it copies: plain, the lines wrapped for the width joined again (see Plain), no spaces at their ends.

Types

type Action

type Action struct{ Key, Name string }

Action is a key of a Select list and what it does, for the hint line.

type Block

type Block interface{ Lines(width int) []string }

Block is one item of a Screen's transcript (a message, an answer, a tool call). It renders itself in width columns, as lines no wider, and may change while shown (a streaming answer): Screen.Changed redraws it.

type Command

type Command struct {
	Name        string // without the slash
	Description string
}

Command is a slash command the editor offers once the input starts with "/".

type Expander

type Expander interface {
	Expand(on bool)
	Expanded() bool
	Folds() bool
}

Expander is a block that shows part of what it holds (a command's output) unless expanded; Ctrl+O expands or collapses them all, a click on one that folds that one alone. Folds reports whether collapsing it hides anything.

type History

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

History is the editor's prompt history, oldest first. With a path it is kept in that file, one JSON string per line, private to the user.

func LoadHistory

func LoadHistory(path string) *History

LoadHistory reads the history kept in path. A missing or unreadable file gives an empty history; "" keeps it in memory only.

func (*History) Add

func (h *History) Add(s string)

Add records s, unless it is empty, repeats the last entry, is too long or looks like it holds a secret.

type Input

type Input struct {
	Title       string
	Placeholder string
	Value       string // initial text
	Mask        bool   // render every character as • (keys, tokens)
	Stderr      bool   // draw on stderr, leaving stdout to the answer
	// Validate runs on Enter behind a spinner; an error is shown under the
	// field and the prompt stays open. ctx is cancelled if the user quits.
	Validate func(ctx context.Context, value string) error
	// Summary is the line left once submitted; it defaults to "Title: value"
	// (bullets when masked) and an empty result leaves nothing.
	Summary func(value string) string
}

Input asks for one line of text.

func (Input) Run

func (in Input) Run() (string, error)

Run shows the prompt and returns the submitted text.

type Item

type Item struct {
	Label       string
	Description string   // dim second column (list) or line under the row (inline)
	Value       string   // returned by Run; Label when empty
	Keys        []string // pick the item directly ("a", "1", "ctrl+y"); the first is shown
}

Item is one choice of a Select.

type Message

type Message struct {
	Text  string        // pastes expanded
	Shown string        // pastes as markers
	Files []attach.File // the files attached, in the order of their markers
}

Message is a message the user sent.

type Palette

type Palette struct {
	Accent  lipgloss.TerminalColor // cursor, selected row, spinner
	Muted   lipgloss.TerminalColor // descriptions, hint actions, scroll info
	Dim     lipgloss.TerminalColor // hint keys, placeholders, separators
	Success lipgloss.TerminalColor
	Error   lipgloss.TerminalColor
	Border  lipgloss.TerminalColor
}

Palette holds the colours every component draws with. Set Colors to restyle them; lipgloss already honours NO_COLOR.

type Plain

type Plain struct {
	Indent int    // columns of decoration it starts with (padding, a quote's bar), left out
	Wrap   bool   // it goes on from the line above, wrapped for the width
	Sep    string // what the wrap took out between the two (a space)
}

Plain tells how a line of a Block copies as text.

type Plainer

type Plainer interface{ Plain(width int) []Plain }

Plainer is a Block whose lines do not copy as they show: Plain returns one Plain per line of Lines(width) (none: as shown).

type Prompt

type Prompt interface{ Prompt() }

Prompt marks the blocks Ctrl+↑ and Ctrl+↓ move between: the user's messages.

type Prompter

type Prompter interface {
	Select(Select) (string, error)
	Input(Input) (string, error)
}

Prompter shows prompts: Inline, or in a Screen's panel.

var Inline Prompter = inline{}

Inline shows prompts inline (Select.Run, Input.Run).

type Screen

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

Screen is the chat on the alternate screen: the transcript, which it scrolls itself, above a status row, a panel for prompts, the editor and its footer, which stay at the bottom while answers stream in. Run it in one goroutine and drive it from another: every method but Run and Transcript is for that one.

func NewScreen

func NewScreen(o ScreenOptions) *Screen

NewScreen sets up a Screen; Run shows it.

func (*Screen) Add

func (s *Screen) Add(b Block)

Add appends a block to the transcript; an Expander as the others are.

func (*Screen) Busy

func (s *Screen) Busy(label string, cancel func())

Busy shows a spinner with label in the status row ("" for none), and makes Esc and Ctrl+C call cancel (nil for nothing to cancel).

func (*Screen) Changed

func (s *Screen) Changed()

Changed redraws the blocks that changed, within a frame's time.

func (*Screen) Clear

func (s *Screen) Clear()

Clear empties the transcript.

func (*Screen) Copy

func (s *Screen) Copy(text string) error

Copy puts text on the clipboard (see Copy), safe to call from any goroutine.

func (*Screen) Footer

func (s *Screen) Footer(left, right string)

Footer sets the line under the editor: left and right parts.

func (*Screen) Input

func (s *Screen) Input(in Input) (string, error)

Input asks for a line of text in the panel above the editor.

func (*Screen) Kill

func (s *Screen) Kill()

Kill ends Run at once.

func (*Screen) Mouse

func (s *Screen) Mouse(on bool)

Mouse turns the wheel and selecting on or off.

func (*Screen) Next

func (s *Screen) Next() (msg Message, ok bool)

Next waits for the next message the user sends. ok is false once the screen is gone. Messages sent while the caller is busy wait in a queue.

func (*Screen) Notify

func (s *Screen) Notify(title, body string)

Notify tells the user something waits for them (an answer, an approval) when the terminal has reported losing focus: a desktop notification where the terminal posts one, else the bell. Without focus reports (focus-events off in tmux, say) it never fires.

func (*Screen) PutBack

func (s *Screen) PutBack(msg Message)

PutBack puts a message that was not sent back into the editor, before what it holds, as Alt+↑ does with queued ones.

func (*Screen) Quit

func (s *Screen) Quit()

Quit ends Run.

func (*Screen) Run

func (s *Screen) Run() error

Run shows the screen until the user quits, Quit is called or a TERM or HUP signal comes (returned as a Signal), then restores the terminal. A Ctrl+C sent as a signal (kill -INT) interrupts like the key.

func (*Screen) Select

func (s *Screen) Select(sel Select) (string, error)

Select shows sel in the panel above the editor and returns the chosen item's value; Esc or Ctrl+C give ErrCancelled.

func (*Screen) SelectAction

func (s *Screen) SelectAction(sel Select) (value, action string, err error)

SelectAction is Select for a list with Actions: it also returns the action key that chose the item, "" for Enter.

func (*Screen) SetInput

func (s *Screen) SetInput(text string, files ...attach.File)

SetInput puts text in the editor in place of what is there, with the files its markers stand for.

func (*Screen) Status

func (s *Screen) Status(label string)

Status changes the spinner's label, "" stopping it.

func (*Screen) Title

func (s *Screen) Title(title string)

Title sets the terminal's (tab) title while the screen runs; the one before comes back when it ends. Control characters are dropped.

func (*Screen) Transcript

func (s *Screen) Transcript(width int) string

Transcript renders the transcript in width columns, for printing once Run has returned.

type ScreenOptions

type ScreenOptions struct {
	Commands []Command // the editor's slash commands
	History  *History
	Mouse    bool // scroll with the wheel and select text to copy it
	// EscEsc is sent as a message (a command) for Esc pressed twice on an
	// empty input while nothing runs; "" for nothing.
	EscEsc string
	// Headless keeps the blocks without a terminal, prompts failing with
	// ErrNotInteractive (for tests).
	Headless bool
}

ScreenOptions configures a Screen.

type Select

type Select struct {
	Title   string
	Items   []Item
	Default int  // initially selected item
	Filter  bool // type to filter; printable item keys are then ignored
	Inline  bool // one row, ←/→ to move (falls back to a list when too wide)
	Height  int  // visible rows of a list (default 8)
	Stderr  bool // draw on stderr, leaving stdout to the answer
	// Summary is the line left once an item is chosen; it defaults to
	// "Title: Label" and an empty result leaves nothing.
	Summary func(Item) string
	// Actions are keys that pick the selected item for something other
	// than Enter does (rename, delete), shown in the hint line; a Screen's
	// SelectAction reports which.
	Actions []Action
}

Select asks the user to pick one item: a vertical list with an optional type-to-filter line, or with Inline a one-line row of choices.

func (Select) Run

func (s Select) Run() (string, error)

Run shows the prompt and returns the chosen item's value.

type Signal

type Signal struct{ os.Signal }

Signal is the error of a prompt that a TERM or HUP signal ended (kill, timeout, a closed terminal): the caller stops too, and exits with 128 + the signal.

func (Signal) Error

func (s Signal) Error() string

type Spinner

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

Spinner animates "⠋ message" on one stderr line until Stop clears it. It is not a bubbletea program, so it leaves stdin alone. The zero value is ready; it does nothing when stderr is not a terminal.

func (*Spinner) Start

func (s *Spinner) Start(msg string)

Start shows the spinner with msg, or just swaps the message if it is already running.

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop clears the spinner line and returns once it is gone, so output that follows lands on a clean line.

Jump to

Keyboard shortcuts

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