ExRatatui.Widgets.SlashCommands (ExRatatui v0.7.1)

Copy Markdown View Source

Slash command parsing and autocomplete popup rendering.

Provides utilities for detecting /command prefixes in text input, matching against a list of registered commands, and rendering an autocomplete popup with the Popup + List widgets.

Usage

commands = [
  %SlashCommands.Command{name: "help", description: "Show help"},
  %SlashCommands.Command{name: "clear", description: "Clear chat"},
  %SlashCommands.Command{name: "quit", description: "Exit the app"}
]

case SlashCommands.parse(input_text) do
  {:command, prefix} ->
    matched = SlashCommands.match_commands(commands, prefix)
    popup_widgets = SlashCommands.render_autocomplete(matched, area: area)
    # Append popup_widgets to your render list

  :no_command ->
    # No slash command detected
end

Summary

Functions

Filters commands whose name or aliases start with the given prefix.

Parses input text to detect a slash command prefix.

Builds a popup widget list for autocomplete display.

Functions

match_commands(commands, prefix)

Filters commands whose name or aliases start with the given prefix.

Case-insensitive matching. An empty prefix matches all commands.

Examples

iex> cmds = [%ExRatatui.Widgets.SlashCommands.Command{name: "help"}, %ExRatatui.Widgets.SlashCommands.Command{name: "clear"}]
iex> ExRatatui.Widgets.SlashCommands.match_commands(cmds, "hel")
[%ExRatatui.Widgets.SlashCommands.Command{name: "help", description: "", aliases: []}]

iex> cmds = [%ExRatatui.Widgets.SlashCommands.Command{name: "help"}]
iex> ExRatatui.Widgets.SlashCommands.match_commands(cmds, "")
[%ExRatatui.Widgets.SlashCommands.Command{name: "help", description: "", aliases: []}]

parse(text)

@spec parse(String.t()) :: {:command, String.t()} | :no_command

Parses input text to detect a slash command prefix.

Returns {:command, prefix} if the text starts with / (after optional leading whitespace), or :no_command otherwise. The prefix is the text between / and the first space.

Examples

iex> ExRatatui.Widgets.SlashCommands.parse("/help")
{:command, "help"}

iex> ExRatatui.Widgets.SlashCommands.parse("/he")
{:command, "he"}

iex> ExRatatui.Widgets.SlashCommands.parse("/")
{:command, ""}

iex> ExRatatui.Widgets.SlashCommands.parse("hello")
:no_command

iex> ExRatatui.Widgets.SlashCommands.parse("")
:no_command

render_autocomplete(matched_commands, opts)

Builds a popup widget list for autocomplete display.

Returns a list of {widget, rect} tuples that can be appended to your render output. Uses Popup + List widgets.

Options

  • :area (required) — the Rect to position the popup in
  • :selected — zero-based index of the selected command (default 0)
  • :highlight_style — style for the selected item
  • :style — style for non-selected items
  • :percent_width — popup width as percentage (default 50)
  • :percent_height — popup height as percentage (default 40)

Examples

iex> alias ExRatatui.Widgets.SlashCommands
iex> alias ExRatatui.Widgets.SlashCommands.Command
iex> commands = [%Command{name: "help", description: "Show help"}]
iex> area = %ExRatatui.Layout.Rect{x: 0, y: 0, width: 80, height: 24}
iex> [{%ExRatatui.Widgets.Popup{}, %ExRatatui.Layout.Rect{}}] = SlashCommands.render_autocomplete(commands, area: area)
iex> :ok
:ok