# `Drafter.Prompt.Editor`
[🔗](https://github.com/jaman/drafter/blob/v0.6.0/lib/drafter/prompt/editor.ex#L1)

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

# `t`

```elixir
@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.

# `before_cursor`

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

The text before the cursor.

# `edit`

```elixir
@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?`

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

Whether the line is empty.

# `insert`

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

Insert `text` at the cursor.

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

# `masked`

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

The same line with every character replaced by `mask`.

# `new`

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

An editor holding `text` with the cursor at its end.

# `replace_word`

```elixir
@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`

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

The whole line.

# `view`

```elixir
@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}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
