TermUI.Widgets.CommandPalette (TermUI v0.2.0)

View Source

Simple command dropdown for filtering and selecting commands.

Shows a list of commands filtered by prefix as the user types. Similar to typing / in Claude Code to see available slash commands.

Usage

# Define commands
commands = [
  %{id: :help, label: "/help", action: fn -> :ok end},
  %{id: :save, label: "/save", action: fn -> :ok end},
  %{id: :quit, label: "/quit", action: fn -> :ok end}
]

# Create and show palette
props = CommandPalette.new(commands: commands)
{:ok, palette} = CommandPalette.init(props)

# Render dropdown when visible
if CommandPalette.visible?(palette) do
  CommandPalette.render(palette, area)
end

Keyboard Navigation

  • Type to filter by prefix
  • Up/Down: Navigate through results
  • Enter: Execute selected command
  • Escape: Close dropdown
  • Backspace: Delete character

Summary

Functions

Gets the current query.

Gets the currently selected command.

Hides the command palette.

Creates new CommandPalette widget props.

Shows the command palette.

Toggles the command palette visibility.

Checks if the palette is visible.

Functions

get_query(state)

@spec get_query(map()) :: String.t()

Gets the current query.

get_selected(state)

@spec get_selected(map()) :: map() | nil

Gets the currently selected command.

hide(state)

@spec hide(map()) :: map()

Hides the command palette.

new(opts)

@spec new(keyword()) :: map()

Creates new CommandPalette widget props.

Options

  • :commands - List of command maps (required). Each command has:
    • :id - Unique identifier (atom)
    • :label - Display text (string)
    • :action - Function to execute (fn -> ... end)
  • :max_visible - Maximum visible results (default: 8)

show(state)

@spec show(map()) :: map()

Shows the command palette.

toggle(state)

@spec toggle(map()) :: map()

Toggles the command palette visibility.

visible?(state)

@spec visible?(map()) :: boolean()

Checks if the palette is visible.