Documentation
¶
Overview ¶
Package tui provides a full-screen terminal UI framework for building interactive CLI applications, inspired by modern AI assistants.
Layout ¶
The screen is divided into three vertical regions:
┌─────────────────────────────────────────────────────────┐ │ Scrollable output / conversation history │ │ (auto-scrolls to bottom; Page Up/Down/wheel to scroll) │ ├─────────────────────────────────────────────────────────┤ │ Command palette (visible when / is typed) │ ├─────────────────────────────────────────────────────────┤ │ ┌─────────────────────────────────────────────────┐ │ │ │ > input goes here Ctrl+C to exit │ │ │ └─────────────────────────────────────────────────┘ │ │ myapp │ └─────────────────────────────────────────────────────────┘
Quick Start ¶
t := tui.New(tui.Config{
Theme: tui.ThemeAmber,
Commands: []*tui.Command{
{Name: "clear", Description: "Clear history", Handler: func(_ string) { t.ClearOutput() }},
},
OnSubmit: func(text string) {
t.AddMessage(tui.RoleUser, text)
t.AddMessage(tui.RoleAssistant, "Echo: "+text)
},
})
t.Run(context.Background())
Streaming ¶
For token-by-token responses:
t.StartStreamingAs("GPT-4o")
for chunk := range tokenCh {
t.StreamChunk(chunk)
}
t.StreamComplete()
Themes ¶
Seven built-in themes: ThemeAmber, ThemeBlue, ThemeGreen, ThemePurple, ThemeLight, ThemePlain, ThemeDefault. Look up by name with ThemeByName. Register custom themes with RegisterTheme or via [Config.Themes].
Index ¶
- Variables
- func RegisterTheme(t *Theme)
- func Styled(color Color, text string) string
- func ThemeNames() []string
- type Color
- type Command
- type Config
- type Menu
- type MenuItem
- type MessageRole
- type TUI
- func (t *TUI) AddCommand(cmd *Command)
- func (t *TUI) AddMessage(role MessageRole, content string)
- func (t *TUI) AddMessageAs(role MessageRole, label, content string)
- func (t *TUI) ClearOutput()
- func (t *TUI) ClearProgress()
- func (t *TUI) CloseMenu()
- func (t *TUI) Context() context.Context
- func (t *TUI) Exit()
- func (t *TUI) IsStreaming() bool
- func (t *TUI) OpenMenu(m *Menu)
- func (t *TUI) RemoveCommand(name string)
- func (t *TUI) Run(ctx context.Context) error
- func (t *TUI) SetInputEnabled(enabled bool)
- func (t *TUI) SetLabels(user, assistant, system string)
- func (t *TUI) SetProgress(label string, value float64)
- func (t *TUI) SetStatus(left, right string)
- func (t *TUI) SetStatusLeft(s string)
- func (t *TUI) SetStatusRight(s string)
- func (t *TUI) SetTheme(theme *Theme)
- func (t *TUI) StartSpinner(text string)
- func (t *TUI) StartStreaming()
- func (t *TUI) StartStreamingAs(label string)
- func (t *TUI) StopSpinner()
- func (t *TUI) StopStreaming()
- func (t *TUI) StreamChunk(chunk string)
- func (t *TUI) StreamComplete()
- func (t *TUI) Theme() *Theme
- type Theme
Constants ¶
This section is empty.
Variables ¶
var ( // ThemeDefault is the default theme — dark navy background, cyan primary, crimson secondary. ThemeDefault = &Theme{ Name: "default", Primary: 0x4EB8C8, Secondary: 0xC0395A, Text: 0xE8EAF0, UserText: 0x4EB8C8, Dim: 0x7A8492, CodeBG: 0x111A26, CodeText: 0xE8EAF0, Error: 0xC0395A, } // ThemeAmber — warm dark background, amber primary, teal secondary. ThemeAmber = &Theme{ Name: "amber", Primary: 0xE8A87C, Secondary: 0x7EC8A4, Text: 0xCDD6F4, UserText: 0xE8A87C, Dim: 0x6C6F85, CodeBG: 0x1E1A14, CodeText: 0xCDD6F4, Error: 0xF38BA8, } // ThemeBlue — deep dark background, periwinkle primary, sky secondary. ThemeBlue = &Theme{ Name: "blue", Primary: 0x7BA7E8, Secondary: 0x5BC8D8, Text: 0xD0D8F0, UserText: 0x7BA7E8, Dim: 0x5A6070, CodeBG: 0x0D1117, CodeText: 0xD0D8F0, Error: 0xE06C75, } // ThemeGreen — dark terminal, mint primary, gold secondary. ThemeGreen = &Theme{ Name: "green", Primary: 0x7EC87A, Secondary: 0xD4A843, Text: 0xD8E0D0, UserText: 0x7EC87A, Dim: 0x5A6650, CodeBG: 0x0D1A0F, CodeText: 0xD8E0D0, Error: 0xE05050, } // ThemePurple — dark background, lavender primary, rose secondary. ThemePurple = &Theme{ Name: "purple", Primary: 0xB48EE8, Secondary: 0xE87EB4, Text: 0xE0D8F0, UserText: 0xB48EE8, Dim: 0x6A6080, CodeBG: 0x130D1E, CodeText: 0xE0D8F0, Error: 0xF07070, } // ThemeLight — light background, blue primary, green secondary. ThemeLight = &Theme{ Name: "light", Primary: 0x1A56CC, Secondary: 0x0A7A50, Text: 0x1A1A2E, UserText: 0x1A56CC, Dim: 0x666677, CodeBG: 0xE8EAF0, CodeText: 0x1A1A2E, Error: 0xCC2020, } // ThemePlain uses no colors (monochrome). ThemePlain = &Theme{ Name: "plain", } )
Built-in themes.
Functions ¶
func RegisterTheme ¶
func RegisterTheme(t *Theme)
RegisterTheme adds a custom theme to the global registry, keyed by Theme.Name.
func Styled ¶
Styled returns text wrapped in the given color, reset after. Use the theme color fields directly: t.Theme().Primary, t.Theme().Secondary, etc.
func ThemeNames ¶
func ThemeNames() []string
ThemeNames returns a sorted slice of all registered theme names.
Types ¶
type Color ¶
type Color uint32
Color is a 24-bit RGB color packed as 0xRRGGBB. Zero means "default terminal color".
type Command ¶
type Command struct {
Name string
Description string
Args []string // Optional sub-options shown in palette after a space
Handler func(args string)
}
Command is a slash command that can be registered with the TUI.
type Config ¶
type Config struct {
// Theme controls colors. Defaults to ThemeAmber if nil.
Theme *Theme
// Commands are the slash commands available in the palette.
Commands []*Command
// Themes registers additional themes into the global theme registry,
// making them available via ThemeByName.
Themes []*Theme
// OnSubmit is called when the user presses Enter to submit input.
// The TUI does NOT add a user message automatically; the caller decides.
OnSubmit func(text string)
// OnEscape is called when Escape is pressed and the palette is not active.
OnEscape func()
// UserLabel is the label shown for user messages. Defaults to "You".
UserLabel string
// AssistantLabel is the default label for assistant messages. Defaults to "Assistant".
AssistantLabel string
// SystemLabel is the label shown for system messages. Defaults to "System".
SystemLabel string
// HideHeaders suppresses the role header line between messages.
HideHeaders bool
// StatusLeft is optional text shown in the bottom-left status bar.
StatusLeft string
// StatusRight is optional text shown in the bottom-right status bar.
StatusRight string
// ShowCharCount enables the character counter below the input box. Defaults to false.
ShowCharCount bool
// InputEnabled controls whether the input box is shown. Defaults to true.
// When false, the input box, char count, and palette are hidden and
// keyboard input only handles scrolling and Ctrl+C.
InputEnabled *bool
}
Config holds the configuration for a TUI instance.
type MenuItem ¶
type MenuItem struct {
Label string
Value string // optional value passed to OnSelect
Prompt string // if set, selecting this item opens a text-entry prompt with this label
Children []*MenuItem // non-nil → sub-menu
OnSelect func(item *MenuItem, input string) // input is non-empty only for Prompt items
}
MenuItem is a single entry in a Menu.
type MessageRole ¶
type MessageRole int
MessageRole identifies who sent a message.
const ( RoleAssistant MessageRole = iota RoleUser RoleSystem )
type TUI ¶
type TUI struct {
// contains filtered or unexported fields
}
TUI is the main terminal UI instance.
func (*TUI) AddCommand ¶ added in v0.7.6
AddCommand registers a new slash command at runtime.
func (*TUI) AddMessage ¶
func (t *TUI) AddMessage(role MessageRole, content string)
AddMessage appends a complete message to the output region.
func (*TUI) AddMessageAs ¶
func (t *TUI) AddMessageAs(role MessageRole, label, content string)
AddMessageAs appends a complete message with a custom label.
func (*TUI) ClearOutput ¶
func (t *TUI) ClearOutput()
ClearOutput removes all messages from the output region.
func (*TUI) ClearProgress ¶
func (t *TUI) ClearProgress()
ClearProgress removes the progress bar from the separator.
func (*TUI) CloseMenu ¶
func (t *TUI) CloseMenu()
CloseMenu closes the menu and restores the input box.
func (*TUI) Context ¶
Context returns the context that was passed to Run. Returns nil if Run has not been called yet.
func (*TUI) Exit ¶
func (t *TUI) Exit()
Exit cleanly shuts down the TUI event loop. Useful as a /exit command handler: func(_ string) { t.Exit() }
func (*TUI) IsStreaming ¶
IsStreaming returns true if a streaming message is in progress.
func (*TUI) RemoveCommand ¶ added in v0.7.6
RemoveCommand removes a slash command by name.
func (*TUI) Run ¶
Run enters the event loop. It blocks until the user exits (Ctrl+C, t.Exit(), or ctx cancellation).
func (*TUI) SetInputEnabled ¶ added in v0.7.9
SetInputEnabled toggles the input box at runtime.
func (*TUI) SetLabels ¶ added in v0.7.6
SetLabels updates the default role labels shown in message headers. Empty strings leave the corresponding label unchanged.
func (*TUI) SetProgress ¶
SetProgress shows a labelled progress bar in the separator (0.0–1.0). Stops any active spinner. Call ClearProgress to remove it.
func (*TUI) SetStatusLeft ¶
SetStatusLeft updates the left status bar text.
func (*TUI) SetStatusRight ¶
SetStatusRight updates the right status bar text.
func (*TUI) StartSpinner ¶
StartSpinner shows an animated spinner in the separator with the given text. Calling StartSpinner while one is running replaces the text.
func (*TUI) StartStreaming ¶
func (t *TUI) StartStreaming()
StartStreaming begins a new streaming assistant message.
func (*TUI) StartStreamingAs ¶
StartStreamingAs begins a new streaming assistant message with a custom label.
func (*TUI) StopSpinner ¶
func (t *TUI) StopSpinner()
StopSpinner stops the spinner and clears it from the separator.
func (*TUI) StopStreaming ¶
func (t *TUI) StopStreaming()
StopStreaming finalises any in-progress streaming message.
func (*TUI) StreamChunk ¶
StreamChunk appends a chunk to the current streaming message.
func (*TUI) StreamComplete ¶
func (t *TUI) StreamComplete()
StreamComplete finalises the streaming message.
type Theme ¶
type Theme struct {
Name string
Primary Color // Accents, prompt >
Secondary Color // Muted text, hints
Text Color // Normal content
UserText Color // User message text
Dim Color // Very muted (scrollbar, borders)
CodeBG Color // Code block background
CodeText Color // Code block text
Error Color // Error messages
}
Theme defines the visual identity of the TUI.
func ThemeByName ¶
ThemeByName returns the theme registered under name, or (nil, false).