Documentation
¶
Overview ¶
Package tui provides a reusable framework for building panel-based terminal UIs with Bubbletea.
Index ¶
- Variables
- func Box(num int, title, content string, width, height int, active bool, ...) string
- func ClampCursor(cursor, count int) int
- func RenderOption(opt QuickOption) string
- func VisibleRange(selected, count, maxLines, linesPerItem int) (int, int)
- type App
- type AppConfig
- type AppState
- type BoxOpts
- type ConfirmAction
- type DataLoader
- type KeyResult
- type Logger
- type PanelDef
- type PanelID
- type PanelRenderer
- type PanelState
- type Prompt
- func (p *Prompt) Active() bool
- func (p *Prompt) Cancel()
- func (p *Prompt) HandleKey(msg tea.KeyMsg) (*Result, bool, tea.Cmd)
- func (p *Prompt) Render() string
- func (p *Prompt) RenderHelp() string
- func (p *Prompt) Start(mode PromptMode, defaultValue string) tea.Cmd
- func (p *Prompt) StartConfirm(action ConfirmAction, target string)
- func (p *Prompt) StartWithOptions(mode PromptMode, defaultValue string, names []string) tea.Cmd
- func (p *Prompt) Update(msg tea.Msg) tea.Cmd
- func (p *Prompt) Value() string
- type PromptLabeler
- type PromptMode
- type PromptReq
- type QuickOption
- type RefreshFlag
- type Region
- type Result
- type ScrollInfo
Constants ¶
This section is empty.
Variables ¶
var ( ActiveBorderStyle lipgloss.Style InactiveBorderStyle lipgloss.Style TitleStyle lipgloss.Style StatusBarStyle lipgloss.Style )
Styles used by panel rendering. Set these from the parent package.
var ( InputLabelStyle lipgloss.Style ErrorStyle lipgloss.Style HelpStyle lipgloss.Style )
Styles used by prompt rendering. Set these from the parent package.
Functions ¶
func Box ¶
Box renders a bordered panel with a numbered title in the top border. This is the single source of truth for panel rendering constraints. All content is truncated to fit within width×height, preventing overflow.
func ClampCursor ¶
ClampCursor ensures cursor is within [0, count-1]. Returns 0 if count is 0.
func RenderOption ¶
func RenderOption(opt QuickOption) string
RenderOption renders a name with its shortcut key highlighted: [C]hanges
func VisibleRange ¶
VisibleRange returns the start and end indices of items to display, ensuring the selected item is always visible within maxLines.
Types ¶
type App ¶
type App struct {
Config AppConfig
State AppState
Prompt Prompt
Width int
Height int
// PanelRegions stores screen coordinates for mouse hit-testing.
PanelRegions map[PanelID]Region
// OnPromptResult is called when a prompt completes.
// Returns true if a follow-up confirm was started (caller should return early).
OnPromptResult func(result *Result) bool
// contains filtered or unexported fields
}
App is the generic Bubbletea model for a panel-based TUI. It can be used as a standalone tea.Model or as a helper embedded in a consumer model.
func (*App) HandleMouse ¶
func (a *App) HandleMouse(msg tea.MouseMsg) (bool, RefreshFlag)
HandleMouse processes a mouse event for panel focus switching. Returns (handled, refresh).
func (*App) HandleUniversalKey ¶
func (a *App) HandleUniversalKey(key string) (handled bool, quit bool, refresh RefreshFlag)
HandleUniversalKey processes universal keys (quit, tab, panel numbers). Returns (handled, quit, refresh). If handled is true, the key was consumed.
type AppConfig ¶
type AppConfig struct {
Panels []PanelDef
TabFlow func(focus, pivot PanelID, panelStates map[PanelID]PanelState) []PanelID
KeyHandler func(key string, state *AppState) KeyResult // domain key handling
Labeler PromptLabeler
Renderer PanelRenderer
Loader DataLoader
}
AppConfig wires consumer logic into the framework.
type AppState ¶
type AppState struct {
Focus PanelID
Pivot PanelID
Cursors map[PanelID]int // cursor position per panel
Selections map[string]bool // selected items (generic keys)
PanelStates map[PanelID]PanelState // Normal/Maximized/Hidden per panel
Scrolls map[PanelID]int // vertical scroll per panel
HScrolls map[PanelID]int // horizontal scroll per panel
Custom any // consumer-specific state
}
AppState is framework-owned generic state.
type BoxOpts ¶
type BoxOpts struct {
Scroll ScrollInfo
Accent lipgloss.Color // override border/title color (empty = default)
}
BoxOpts holds optional rendering parameters for Box.
type ConfirmAction ¶
type ConfirmAction = int
ConfirmAction identifies what dangerous action is pending confirmation.
type DataLoader ¶
type DataLoader interface {
Refresh(flag RefreshFlag)
}
DataLoader refreshes data based on refresh flags.
type KeyResult ¶
type KeyResult struct {
Refresh RefreshFlag
Prompt *PromptReq
StatusMsg string
ErrorMsg string
Quit bool
}
KeyResult is the output of the consumer's key handler.
type PanelDef ¶
type PanelDef struct {
ID PanelID
Title string
Num int // number key shortcut (1-9)
Pivot bool // pressing number also sets this as pivot
Toggle bool // pressing number cycles Normal → Maximized → Hidden when focused
}
PanelDef defines a panel in the layout.
type PanelRenderer ¶
type PanelRenderer interface {
RenderPanel(id PanelID, width, height int, focused bool) (content string, scroll *ScrollInfo)
RenderHeader(width int) string
RenderHelp(promptActive bool) string
}
PanelRenderer renders panel content and help bar.
type PanelState ¶
type PanelState = int
PanelState represents the display state of a toggleable panel.
const ( PanelNormal PanelState = 0 PanelMaximized PanelState = 1 PanelHidden PanelState = 2 PanelMinimized PanelState = 3 )
func CyclePanelState ¶
func CyclePanelState(current PanelState, focused bool) (PanelState, bool)
CyclePanelState cycles a toggleable panel through: normal → maximized → hidden → normal. Returns the new state and whether focus should move away (when hidden).
func CycleWorktreeState ¶
func CycleWorktreeState(current PanelState, focused bool) (PanelState, bool)
CycleWorktreeState toggles the worktrees panel between normal and minimized. Returns the new state and whether focus should move away (when minimizing).
type Prompt ¶
type Prompt struct {
Mode PromptMode
Input textinput.Model
ConfirmAction ConfirmAction
ConfirmTarget string
Options []QuickOption
Labeler PromptLabeler
// contains filtered or unexported fields
}
Prompt holds the current input/confirmation state.
func NewPrompt ¶
func NewPrompt(labeler PromptLabeler, confirmMode PromptMode) Prompt
NewPrompt creates a Prompt configured with a labeler and the consumer's confirm mode value.
func (*Prompt) HandleKey ¶
HandleKey processes a key event for the active prompt. Returns (result, handled, cmd). If result is non-nil, the prompt completed.
func (*Prompt) RenderHelp ¶
RenderHelp returns help text for the active prompt.
func (*Prompt) Start ¶
func (p *Prompt) Start(mode PromptMode, defaultValue string) tea.Cmd
Start begins a new input prompt with the given mode and default value.
func (*Prompt) StartConfirm ¶
func (p *Prompt) StartConfirm(action ConfirmAction, target string)
StartConfirm begins a confirmation prompt.
func (*Prompt) StartWithOptions ¶
StartWithOptions begins a prompt with quick-select options. Each option gets a unique shortcut key assigned from its name.
type PromptLabeler ¶
type PromptLabeler interface {
PromptLabel(mode PromptMode) string
ConfirmMessage(action ConfirmAction, target string) string
}
PromptLabeler provides display text for prompt modes and confirm actions.
type PromptReq ¶
type PromptReq struct {
Mode PromptMode
DefaultValue string
Confirm ConfirmAction
Target string
Options []string
}
PromptReq describes a prompt the coordinator should start.
type QuickOption ¶
QuickOption is a named option with a shortcut key.
func AssignShortcuts ¶
func AssignShortcuts(names []string) []QuickOption
AssignShortcuts assigns a unique shortcut key to each name. It picks the first unused uppercase letter from each name.
type RefreshFlag ¶
type RefreshFlag int
RefreshFlag tells the coordinator what data to reload.
const ( RefreshNone RefreshFlag = 0 RefreshDiff RefreshFlag = 1 << iota RefreshCLFiles // reload changelist files (implies diff) RefreshShelfFiles // reload shelf files (implies diff) RefreshAll // reload everything RefreshWorktree // debounced worktree switch + reload )
type Region ¶
type Region struct {
X, Y, W, H int
}
Region stores the screen coordinates of a panel for mouse hit-testing.
type Result ¶
type Result struct {
Mode PromptMode
Value string
Confirmed bool // for Confirm mode: true=yes, false=cancelled
ConfirmAction ConfirmAction
ConfirmTarget string
}
Result represents the outcome of a completed prompt.
type ScrollInfo ¶
ScrollInfo provides scroll position data for rendering a scrollbar on the right border.