Documentation
¶
Overview ¶
Package tui is the CLI's interactive session: the prompt with its history and completion, the slash commands, streamed Markdown output, approvals and questions answered at the terminal, and Ctrl+C to stop a turn. It drives an api.Backend, so it works the same on a local workspace and on one held by the service (spec_tui_019).
Index ¶
- Constants
- Variables
- func ConfirmExit(ctx context.Context, in Input, pm api.Processes, interrupts <-chan os.Signal, ...) bool
- func FormatDiff(diffText string) string
- func FormatTasks(tasks []api.Task) string
- func FormatToolCall(toolName string, args map[string]any) string
- func FormatToolResult(toolName string, success bool, summary string) string
- func HandleCommand(ctx context.Context, input string, app *App) (bool, error)
- func LicenseText(which, command string) (string, error)
- func NewApprover(in Input, diffLines int) api.Approver
- func NewUserPrompter(in Input) api.UserPromptFunc
- func Page(out io.Writer, text string) error
- func PrintBanner(version, agent, model string)
- func PrintModelText(text string)
- func PrintRecap(msgs []api.Message, n int)
- func PrintToolCall(toolName string, args map[string]any)
- func PrintToolResult(toolName string, success bool, summary string)
- func RenderDiff(diff string, maxLines int) (string, bool)
- func RunREPL(ctx context.Context, app *App) error
- func StdinIsTerminal() bool
- func SummarizeToolResponse(resp map[string]any) (summary string, success bool)
- func UsageLine(before, after api.Usage) string
- type App
- type Completer
- type ExitPrompt
- type Input
- type LineReader
- type PickItem
- type Picker
- type Printer
- type PrinterOptions
- type PromptKeys
- type Spinner
- type TerminalInput
- func (t *TerminalInput) Ask(ctx context.Context, text string) (string, error)
- func (t *TerminalInput) AskSteer(ctx context.Context, prompt, prefill string) (string, error)
- func (t *TerminalInput) Close() error
- func (t *TerminalInput) Pick(ctx context.Context, title string, items []PickItem, current int) (int, error)
- func (t *TerminalInput) ReadInput(ctx context.Context, prompt string) (string, error)
- func (t *TerminalInput) SetInterruptHandler(f func())
- func (t *TerminalInput) SetNextInput(text string)
- func (t *TerminalInput) SetPromptKeys(k PromptKeys)
- func (t *TerminalInput) WatchKeys(onKey func(prefill string)) (stop func())
- type TerminalOptions
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" White = "\033[37m" BgBlack = "\033[40m" BgBlue = "\033[44m" )
ANSI color codes
const Product = "Blitz"
PrintBanner renders the Blitz ASCII splash banner. Product is the product's name, which isn't translated.
Variables ¶
var ErrExit = errors.New("exit")
ErrExit is returned by HandleCommand when the user asks to quit.
var ErrPickCancelled = errors.New("cancelled")
ErrPickCancelled is returned when the user leaves a picker without choosing (Esc, or an empty answer).
var ErrRewindKey = errors.New("rewind requested")
ErrRewindKey is what ReadInput returns when Esc Esc was pressed at an empty prompt: the REPL opens /rewind.
Functions ¶
func ConfirmExit ¶
func ConfirmExit(ctx context.Context, in Input, pm api.Processes, interrupts <-chan os.Signal, opts ExitPrompt) bool
ConfirmExit decides whether Blitz may exit while background processes are running. Nothing is left running either way: the user chooses to kill them now or wait for them to finish, and a further Ctrl+C (or EOF) at the prompt or while waiting force-quits, killing them. Returns false only when the user cancels the exit.
func FormatDiff ¶
FormatDiff highlights diff additions in green and deletions in red.
func FormatTasks ¶
FormatTasks renders the agent's task list as a checklist.
func FormatToolCall ¶
FormatToolCall renders an invocation badge for a tool.
func FormatToolResult ¶
FormatToolResult renders a completed tool badge.
func HandleCommand ¶
HandleCommand processes slash commands. Returns true if input was a slash command.
func LicenseText ¶
LicenseText is what `license` shows for which: "" (the NOTICE and where to find the rest), "full" (the Apache License) or "third-party" (the notices of the software Blitz includes). command is how the caller is invoked, for the pointers in the summary.
func NewApprover ¶
NewApprover returns an api.Approver that asks on the terminal, showing the proposed diff (truncated to diffLines; "d" shows it all). Anything other than an explicit yes is a denial.
func NewUserPrompter ¶
func NewUserPrompter(in Input) api.UserPromptFunc
NewUserPrompter returns an api.UserPromptFunc backed by the terminal. A numeric answer selects the matching option when options are offered.
func Page ¶
Page writes text to out, through $PAGER (else less) when out is a terminal and the text is taller than it.
func PrintBanner ¶
func PrintBanner(version, agent, model string)
PrintBanner prints the session's first lines: Blitz's name and version, the agent and model in use, and how to get help or stop.
func PrintModelText ¶
func PrintModelText(text string)
PrintModelText writes streamed model text with control sequences removed.
func PrintRecap ¶
PrintRecap shows the last n messages of a resumed session.
func PrintToolCall ¶
PrintToolCall prints FormatToolCall.
func PrintToolResult ¶
PrintToolResult prints FormatToolResult.
func RenderDiff ¶
RenderDiff colourises a unified diff for the terminal, keeping at most maxLines lines (0 = all). It reports whether lines were cut.
func StdinIsTerminal ¶
func StdinIsTerminal() bool
StdinIsTerminal reports whether stdin is interactive.
func SummarizeToolResponse ¶
SummarizeToolResponse picks a short summary from a function response and reports whether it represents success.
Types ¶
type App ¶
type App struct {
// Workspace is the program behind the REPL: every command and turn goes
// through it. It runs here or in the Blitz service.
Workspace api.Backend
Version string
Input Input
// Printer configures output rendering.
Printer PrinterOptions
// Locales are the interface's translation catalogs (nil: the built-in
// ones). The interface language is the front end's own.
Locales *i18n.Bundle
// Attachments are images to send with the next prompt (/attach, /paste,
// --image).
Attachments []*images.Image
// TerminalTitle shows the session's name in the terminal window title.
TerminalTitle bool
// Interrupts delivers Ctrl+C. If nil, RunREPL subscribes to os.Interrupt
// itself. At the prompt an interrupt starts exit (confirming if background
// processes run); during a turn it cancels only that turn.
Interrupts <-chan os.Signal
}
App is the REPL's state: the workspace it drives and the terminal.
type Completer ¶
type Completer struct {
// contains filtered or unexported fields
}
Completer completes slash commands, their arguments, and @path references to files in the workspace.
func NewCompleter ¶
NewCompleter creates a completer rooted at workspace.
func (*Completer) Command ¶
Command registers a slash command (without "/") and static argument choices.
type ExitPrompt ¶
type ExitPrompt struct {
// CanPrompt is false when stdin can't answer (EOF, not a terminal); running
// processes are then killed without asking.
CanPrompt bool
// AllowCancel offers "cancel" to return to the REPL instead of exiting.
AllowCancel bool
}
ExitPrompt configures ConfirmExit.
type Input ¶
type Input interface {
// Ask reads a one-off answer (approvals, questions); not saved to history.
Ask(ctx context.Context, text string) (string, error)
// ReadInput reads a REPL entry, which may span lines: end a line with
// "\\" to continue it, or enclose a block between lines of """.
ReadInput(ctx context.Context, prompt string) (string, error)
}
Input reads user input. LineReader serves pipes and tests; TerminalInput adds line editing, history and completion on a TTY.
type LineReader ¶
type LineReader struct {
// contains filtered or unexported fields
}
LineReader is the single buffered reader over stdin. The REPL, approval prompts and ask_user_question must all share it: separate bufio readers on the same fd each buffer ahead and steal each other's input.
Reads are cancellable. A read abandoned by cancellation stays in flight and its line is delivered to the next caller, so no input is lost.
func NewLineReader ¶
func NewLineReader(in io.Reader, out io.Writer) *LineReader
NewLineReader wraps in, echoing prompts to out.
func (*LineReader) Ask ¶
Ask prints text and reads one line, returning early with ctx.Err() if ctx is cancelled. Concurrent callers are serialised so their output and input don't interleave.
type PickItem ¶
type PickItem struct {
Label string // shown, and matched by typing
Detail string // shown dim after the label, and matched too
// Key selects the item at once when typed before any filter text (the
// approval answers y, s, a, n). 0 for none.
Key rune
}
PickItem is one choice in a picker.
type Picker ¶
type Picker interface {
// Pick shows items under title with current selected, and returns the
// index chosen.
Pick(ctx context.Context, title string, items []PickItem, current int) (int, error)
}
Picker is an Input that can show a menu: an arrow-key, type-to-filter list inside the terminal's normal flow (decision §12.4 of the parity spec: no full-screen views).
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer renders agent events: model text (optionally as Markdown), tool activity, and a spinner while waiting.
func NewPrinter ¶
func NewPrinter(o PrinterOptions) *Printer
NewPrinter creates a Printer; Markdown falls back to plain text on error.
type PrinterOptions ¶
type PrinterOptions struct {
Out io.Writer
Markdown bool // render Markdown (requires a terminal)
Theme string
Width int
Spinner bool
}
PrinterOptions configure a Printer.
type PromptKeys ¶
type PromptKeys struct {
// CycleMode switches to the next permission mode (Shift+Tab) and
// returns the new prompt, or "" to leave it.
CycleMode func() string
}
PromptKeys are what REPL-only keys do at the main prompt.
type Spinner ¶
type Spinner struct {
// contains filtered or unexported fields
}
Spinner shows an animated status line while waiting. It is a no-op when disabled (e.g. output is not a terminal). Start and Stop may be called in any order and repeatedly.
func NewSpinner ¶
NewSpinner returns a spinner writing to out.
type TerminalInput ¶
type TerminalInput struct {
// contains filtered or unexported fields
}
TerminalInput is an Input backed by a line editor: arrow-key editing, persistent history, reverse search (Ctrl+R) and tab completion. Every terminal read goes through it so prompts never compete for stdin.
func NewTerminalInput ¶
func NewTerminalInput(o TerminalOptions) (*TerminalInput, error)
NewTerminalInput creates the line editor on stdin/stdout.
func (*TerminalInput) Ask ¶
Ask prints text (which may span lines) and reads one answer without recording it in history.
func (*TerminalInput) AskSteer ¶
AskSteer reads a steer message pre-filled with what the user already typed. It is called from the key watcher, which has handed over the terminal, so it doesn't pause the watcher itself.
func (*TerminalInput) Pick ¶
func (t *TerminalInput) Pick(ctx context.Context, title string, items []PickItem, current int) (int, error)
Pick implements Picker on the terminal: arrow keys (or Ctrl+P/N) move, typing filters, Enter chooses, Esc clears the filter or cancels, and Ctrl+C cancels (and interrupts a running turn). Without a terminal it asks for a number instead.
func (*TerminalInput) ReadInput ¶
ReadInput reads a REPL entry (multi-line aware) and saves it to history.
func (*TerminalInput) SetInterruptHandler ¶
func (t *TerminalInput) SetInterruptHandler(f func())
SetInterruptHandler sets what Ctrl+C does while a prompt is shown during a turn (e.g. an approval). The terminal is in raw mode then, so Ctrl+C is a keypress rather than SIGINT; the REPL uses this to cancel the turn.
func (*TerminalInput) SetNextInput ¶
func (t *TerminalInput) SetNextInput(text string)
SetNextInput puts text in the next REPL prompt's line, to edit or send (a rewound prompt).
func (*TerminalInput) SetPromptKeys ¶
func (t *TerminalInput) SetPromptKeys(k PromptKeys)
SetPromptKeys sets what the REPL's own keys do at the main prompt.
func (*TerminalInput) WatchKeys ¶
func (t *TerminalInput) WatchKeys(onKey func(prefill string)) (stop func())
WatchKeys lets the user steer the running turn: typing (or Ctrl+T) calls onKey with the typed text, from a watcher goroutine. Prompts during the turn pause the watcher automatically. Call stop when the turn ends; it waits for an open steer prompt to be finished.
type TerminalOptions ¶
type TerminalOptions struct {
HistoryFile string
HistorySize int
Completer readline.AutoCompleter
}
TerminalOptions configure a TerminalInput.