Documentation
¶
Overview ¶
Package commandpalette is a text input with a fuzzy-filtered dropdown of Commands — a "Ctrl+K" style command palette.
It reuses autocomplete.Model's entire filter-dropdown skeleton (embedded tui/textinput, re-filter-per-keystroke, highlight index, Up/Down/Enter, dropdown View), swapping two things: the match function is fuzzy subsequence matching instead of prefix matching, and accepting a result emits a Msg identifying the chosen Command instead of filling the input value — a palette runs a command, it doesn't complete text.
Example ¶
Typing filters the commands; Enter selects the best match.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/commandpalette"
)
func main() {
m := commandpalette.New(
commandpalette.Command{Name: "open file", Description: "open a file"},
commandpalette.Command{Name: "quit", Description: "leave the app"},
)
m.Focus()
for _, r := range "qu" {
m, _ = m.Update(tui.Key{Type: tui.KeyRunes, Text: string([]rune{r})})
}
_, cmd := m.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(cmd().(commandpalette.SelectedMsg).Command.Name)
}
Output: quit
Index ¶
- type Command
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m *Model) Focus() tui.Cmd
- func (m Model) IsOpen() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
- type SelectedMsg
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Command ¶
Command is a single palette entry: a Name to match and display, plus an optional Description shown alongside it.
type KeyMap ¶
type KeyMap struct {
Up keymap.Binding
Down keymap.Binding
// Select runs the highlighted command.
Select keymap.Binding
// Close closes the dropdown without running anything.
Close keymap.Binding
}
KeyMap is the set of navigation keys a palette reacts to while its dropdown is open.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the default keys: up/down, enter to select, esc to close.
type Model ¶
type Model struct {
Input textinput.Model
Commands []Command
Theme theme.Theme
// Raw, when true, draws command names and descriptions unchanged. By
// default they are sanitised (ansi.Sanitize) so untrusted text cannot
// carry terminal escape sequences.
Raw bool
// KeyMap holds the navigation keys Update reacts to while the dropdown
// is open; every other key goes to Input. New sets DefaultKeyMap; a Model
// built as a struct literal with a zero KeyMap uses DefaultKeyMap.
KeyMap KeyMap
// Mouse turns on mouse handling in Update (off by default). Bounds is the
// screen rectangle where the app draws the palette (the input on its
// first row, the dropdown beneath); events outside it, or while the
// dropdown is closed, are ignored. WheelStep is the rows one wheel notch
// moves the highlight (0 means 3). A left press on a dropdown row selects
// that command, exactly as Enter on it would.
Mouse bool
Bounds hittest.Rect
WheelStep int
// contains filtered or unexported fields
}
Model is a textinput.Model plus a fuzzy-filtered dropdown of Commands.
func New ¶
New returns a focused Model (typing works immediately) with an unstarted cursor blink — call Init to start it, the same as textinput.Model.Focus would require.
func (*Model) Focus ¶
Focus and Blur delegate to the embedded Input, so a parent composing several fields can drive focus across all of them the same way.
func (Model) LayoutNode ¶
LayoutNode adapts the palette to a layout.Node. Measure reports its natural size: the input line and, while the dropdown is open, one row per command. Render fits the allotted Size: the input scrolls its value around the cursor within the width (as textinput's node does), rows are cut to the width, and when the dropdown has more rows than fit it shows a window containing the highlighted command beneath the input line. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the palette as plain text for accessible output (see tui.Linearizer): the input's own line, then, while the dropdown is open, "N commands" and one line per command with its description, position and ", selected" on the highlighted one.
func (Model) SetTheme ¶
SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentCommandPalette, ...) and then by WithTokens.
func (Model) Update ¶
Update forwards most keys to Input, intercepting the KeyMap keys (by default Up/Down/Enter/Esc) to drive the dropdown while it's open (has at least one filtered command). If your app also uses Esc to quit, check that globally before forwarding to CommandPalette — otherwise Esc will only ever close the dropdown, never reach your quit handling, since CommandPalette consumes it whenever the dropdown is open.
type SelectedMsg ¶
type SelectedMsg struct {
Command Command
}
SelectedMsg is delivered (via the Cmd Update returns) when a command is selected with Enter. Unlike autocomplete.Model's AcceptedMsg, this does not fill the input value — a chosen command is meant to run, not to be edited further.