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+HandDelete,Ctrl+Ddelete one characterLeft,Ctrl+B,Right,Ctrl+Fmove one characterHome,Ctrl+A,End,Ctrl+Emove to either endCtrl+Left,Alt+Left,Ctrl+Right,Alt+Rightmove one wordCtrl+Wcuts the word before the cursor,Ctrl+Ueverything before it andCtrl+Keverything after itCtrl+Yinserts what was cut last
Summary
Types
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
Functions
The text before the cursor.
@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.
Whether the line is empty.
Insert text at the cursor.
Line breaks and tabs become spaces and other control characters are dropped.
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 word is the run of characters other than spaces before the cursor, which is empty when the cursor follows a space.
The whole line.
@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}