Drafter.Prompt.Completion (drafter v0.6.0)

Copy Markdown View Source

Tab completion sources for text prompts.

A source is a list of words, or a function that takes the text before the cursor and returns the candidates for the word being typed. The word being typed is the run of characters other than whitespace that ends at the cursor, and a candidate replaces all of it.

iex> Drafter.Prompt.Completion.candidates(["start", "status", "stop"], "service sta")
["start", "status"]

iex> Drafter.Prompt.Completion.common_prefix(["start", "status"])
"sta"

paths/1 builds a source that completes file names:

Drafter.Prompt.text("Config file", complete: Drafter.Prompt.Completion.paths())

Summary

Functions

The candidates source offers for the text before the cursor.

A source for a command line: the first word completes to a command name and the words after it complete from that command's own source.

The longest text every candidate starts with.

A source that completes the word being typed as a file path.

The word being typed: what follows the last whitespace in before.

Types

source()

@type source() :: [String.t()] | (String.t() -> [String.t()])

Functions

candidates(words, before)

@spec candidates(source(), String.t()) :: [String.t()]

The candidates source offers for the text before the cursor.

A list offers its words that start with the word being typed.

commands(commands)

@spec commands(%{required(String.t()) => source()}) :: (String.t() -> [String.t()])

A source for a command line: the first word completes to a command name and the words after it complete from that command's own source.

commands maps each command name to the source for its arguments; use [] for a command that takes none.

iex> source = Drafter.Prompt.Completion.commands(%{"open" => ["inbox", "outbox"], "quit" => []})
iex> source.("q")
["quit"]
iex> source.("open in")
["inbox"]

common_prefix(list)

@spec common_prefix([String.t()]) :: String.t()

The longest text every candidate starts with.

iex> Drafter.Prompt.Completion.common_prefix(["lib/a.ex", "lib/b.ex"])
"lib/"

iex> Drafter.Prompt.Completion.common_prefix([])
""

paths(opts \\ [])

@spec paths(keyword()) :: (String.t() -> [String.t()])

A source that completes the word being typed as a file path.

A candidate keeps the directory part as it was typed, so ~/Doc completes to ~/Documents/. Directories end in /. Names starting with . are offered only when the name being typed starts with ..

Options

  • :only - :directories offers directories alone. Default :all.

word(before)

@spec word(String.t()) :: String.t()

The word being typed: what follows the last whitespace in before.

iex> Drafter.Prompt.Completion.word("git che")
"che"

iex> Drafter.Prompt.Completion.word("git ")
""