Drafter.Prompt.Editor (drafter v0.6.0)

Copy Markdown View Source

One line of text with a cursor in it, and the editing keys that change it.

iex> alias Drafter.Prompt.Editor
iex> editor = Editor.new("hello world")
iex> {:ok, editor} = Editor.edit(editor, {:key, :w, [:ctrl]})
iex> Editor.text(editor)
"hello "
iex> {:ok, editor} = Editor.edit(editor, {:key, :y, [:ctrl]})
iex> Editor.text(editor)
"hello world"

The cursor sits between graphemes, so a character made of several codepoints moves and deletes as one. A word is a run of characters other than spaces.

Keys

edit/2 understands these events and answers :ignored to any other:

  • a printable key, {:char, codepoint} or {:bracketed_paste, text} inserts
  • Backspace, Ctrl+H and Delete, Ctrl+D delete one character
  • Left, Ctrl+B, Right, Ctrl+F move one character
  • Home, Ctrl+A, End, Ctrl+E move to either end
  • Ctrl+Left, Alt+Left, Ctrl+Right, Alt+Right move one word
  • Ctrl+W cuts the word before the cursor, Ctrl+U everything before it and Ctrl+K everything after it
  • Ctrl+Y inserts what was cut last

Summary

Types

t()

before holds the graphemes before the cursor, nearest first. after holds the ones after it in reading order. cut is the text cut last.

Functions

The text before the cursor.

Apply one key or paste event.

Whether the line is empty.

Insert text at the cursor.

The same line with every character replaced by mask.

An editor holding text with the cursor at its end.

Replace the word the cursor is at the end of with replacement.

The whole line.

The part of the line that fits in width columns, and the cursor's column in it.

Types

t()

@type t() :: %Drafter.Prompt.Editor{
  after: [String.t()],
  before: [String.t()],
  cut: String.t()
}

before holds the graphemes before the cursor, nearest first. after holds the ones after it in reading order. cut is the text cut last.

Functions

before_cursor(editor)

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

The text before the cursor.

edit(editor, arg2)

@spec edit(t(), Drafter.Terminal.ANSI.event()) :: {:ok, t()} | :ignored

Apply one key or paste event.

Returns {:ok, editor} for an event the editor understands, changed or not, and :ignored for any other.

empty?(editor)

@spec empty?(t()) :: boolean()

Whether the line is empty.

insert(editor, text)

@spec insert(t(), String.t()) :: t()

Insert text at the cursor.

Line breaks and tabs become spaces and other control characters are dropped.

masked(editor, mask)

@spec masked(t(), String.t()) :: t()

The same line with every character replaced by mask.

new(text \\ "")

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

An editor holding text with the cursor at its end.

replace_word(editor, replacement)

@spec replace_word(t(), String.t()) :: t()

Replace the word the cursor is at the end of with replacement.

The word is the run of characters other than spaces before the cursor, which is empty when the cursor follows a space.

text(editor)

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

The whole line.

view(editor, width)

@spec view(t(), pos_integer()) :: {String.t(), non_neg_integer()}

The part of the line that fits in width columns, and the cursor's column in it.

The line scrolls so the cursor stays in view: text before the cursor is cut from the left until it fits in width - 1 columns, and text after it is cut from the right.

iex> Drafter.Prompt.Editor.view(Drafter.Prompt.Editor.new("abcdefgh"), 5)
{"efgh", 4}