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 ¶
- Variables
- func Confirm(title string, def bool) (bool, error)
- func ConfirmWith(p Prompter, title string, def bool) (bool, error)
- func Copy(text string, term io.Writer) error
- func DiscardInput()
- func HoldInput() (restore func())
- func Interactive() bool
- func NewlineKey() string
- func Unwrap(lines []string, plain []Plain, a, b int) string
- type Action
- type Block
- type Command
- type Expander
- type History
- type Input
- type Item
- type Message
- type Palette
- type Plain
- type Plainer
- type Prompt
- type Prompter
- type Screen
- func (s *Screen) Add(b Block)
- func (s *Screen) Busy(label string, cancel func())
- func (s *Screen) Changed()
- func (s *Screen) Clear()
- func (s *Screen) Copy(text string) error
- func (s *Screen) Footer(left, right string)
- func (s *Screen) Input(in Input) (string, error)
- func (s *Screen) Kill()
- func (s *Screen) Mouse(on bool)
- func (s *Screen) Next() (msg Message, ok bool)
- func (s *Screen) Notify(title, body string)
- func (s *Screen) PutBack(msg Message)
- func (s *Screen) Quit()
- func (s *Screen) Run() error
- func (s *Screen) Select(sel Select) (string, error)
- func (s *Screen) SelectAction(sel Select) (value, action string, err error)
- func (s *Screen) SetInput(text string, files ...attach.File)
- func (s *Screen) Status(label string)
- func (s *Screen) Title(title string)
- func (s *Screen) Transcript(width int) string
- type ScreenOptions
- type Select
- type Signal
- type Spinner
Constants ¶
This section is empty.
Variables ¶
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") )
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).
var FileMarker = regexp.MustCompile(`\[(?:image|file) #(\d+)(?: · [^\[\]\n]*)?\]`)
FileMarker matches the marker of an attached file (attach.File.Marker).
var PasteMarker = regexp.MustCompile(`\[paste #(\d+) (?:\+\d+ lines|\d+ chars)\]`)
PasteMarker matches the marker a collapsed paste leaves in the editor.
Functions ¶
func Confirm ¶
Confirm asks a yes/no question on one line (y/n, ←/→, Enter takes the highlighted answer, def initially).
func ConfirmWith ¶
ConfirmWith asks the yes/no question with p.
func Copy ¶
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).
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 ¶
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 Expander ¶
Expander is a block that shows part of what it holds (a command's output) unless expanded; Ctrl+O expands or collapses them all. 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 ¶
LoadHistory reads the history kept in path. A missing or unreadable file gives an empty history; "" keeps it in memory only.
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.
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 ¶
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 ¶
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 (*Screen) Busy ¶
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) Next ¶
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 ¶
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 ¶
PutBack puts a message that was not sent back into the editor, before what it holds, as Alt+↑ does with queued ones.
func (*Screen) Run ¶
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 ¶
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 ¶
SelectAction is Select for a list with Actions: it also returns the action key that chose the item, "" for Enter.
func (*Screen) SetInput ¶
SetInput puts text in the editor in place of what is there, with the files its markers stand for.
func (*Screen) Title ¶
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 ¶
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.
type 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.
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.