commandpalette

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Name        string
	Description string
}

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

func New(commands ...Command) Model

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) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the active key bindings, with descriptions, for help text.

func (*Model) Blur

func (m *Model) Blur()

Blur unfocuses the embedded Input; see Focus.

func (*Model) Focus

func (m *Model) Focus() tui.Cmd

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) IsOpen

func (m Model) IsOpen() bool

IsOpen reports whether the command dropdown is currently showing.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

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

func (m Model) SetTheme(t theme.Theme) Model

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

func (m Model) Tokens() theme.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

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

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.

func (Model) View

func (m Model) View() string

View renders the embedded Input, plus a command dropdown beneath it while IsOpen.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL