Documentation
¶
Overview ¶
Package commandpalette provides a fuzzy-search command palette overlay for the Bubble Tea framework.
The palette renders as a self-contained bordered box: a text input at the top, a live-filtered list of actions below, and a keyboard hint footer. Actions are matched by label and optional keywords using fuzzy search.
The caller is responsible for opening/closing the palette and handling esc and ctrl+c before forwarding key messages to Update.
Typical usage:
palette := commandpalette.New(commandpalette.Config{
Actions: []commandpalette.Action{
{Label: "Edit Book", Run: doEditBook},
{Label: "Delete Book", Keywords: []string{"remove"}, Run: doDelete},
{Label: "Sync", Run: doSync},
},
})
palette.SetSize(width, height)
In Update:
case tea.KeyMsg:
if msg.String() == "ctrl+p" {
paletteOpen = true
return m, palette.Focus()
}
if paletteOpen {
switch msg.String() {
case "esc", "ctrl+c":
paletteOpen = false
return m, nil
}
palette, cmd = palette.Update(msg)
return m, cmd
}
case commandpalette.ActionSelectedMsg:
paletteOpen = false
return m, msg.Action.Run
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action struct {
// Label is the displayed name of the action and the primary search target.
Label string
// Keywords are additional searchable terms that are not displayed.
// For example, an "Edit Book" action might have Keywords: []string{"modify", "update"}.
Keywords []string
// Run is the command executed when this action is selected.
// It is passed directly to the parent model via ActionSelectedMsg and may be nil.
Run tea.Cmd
}
Action represents a single executable command in the palette.
type ActionSelectedMsg ¶
type ActionSelectedMsg struct {
Action Action
}
ActionSelectedMsg is emitted when the user confirms a selection by pressing enter. The parent model should close the palette and call msg.Action.Run.
type Config ¶
type Config struct {
// Actions is the initial set of actions. May be replaced later via SetActions.
Actions []Action
// Placeholder is shown in the text input when it is empty.
// Defaults to "Search actions…".
Placeholder string
// MaxResults is the maximum number of filtered results shown at once.
// Defaults to 10.
MaxResults int
// MaxWidth is the maximum column width of the palette overlay, including
// the border. Defaults to 60.
MaxWidth int
// ActiveColor is the highlight color used for the prompt, selected result
// row, and border. Defaults to lipgloss.Color("#fb6820") (orange).
ActiveColor lipgloss.Color
}
Config holds the configuration for constructing a new Model.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is the command palette state. Create it with New and pass it through Update and View using value semantics, matching the Bubble Tea convention.
func New ¶
New creates a new Model from cfg. Missing fields receive sensible defaults. Call SetSize before the first View call.
func (*Model) Focus ¶
Focus focuses the text input and returns textinput.Blink so the cursor appears immediately. Call this each time the palette is opened.
func (*Model) Reset ¶
func (m *Model) Reset()
Reset clears the query, restores the full action list, and resets the cursor to 0. Call this before Focus when re-opening the palette so it starts fresh.
func (*Model) SetActions ¶
SetActions replaces the full action list and re-applies the current filter. The cursor is clamped to the new result count.
func (*Model) SetSize ¶
SetSize sets the available terminal dimensions used for width clamping. Call on init and on every tea.WindowSizeMsg received by the parent model.
func (Model) Update ¶
Update processes a key message and returns an updated Model and optional Cmd.
Keys handled by the component:
- Any printable character / backspace — update input, re-filter results
- up / k — move cursor up through visible results
- down / j — move cursor down through visible results
- enter — emit ActionSelectedMsg for the highlighted action
The caller is responsible for handling esc and ctrl+c before forwarding key messages here.