# `Drafter.Render.Writer`
[🔗](https://github.com/jaman/drafter/blob/main/lib/drafter/render/writer.ex#L1)

The one process that writes a session's terminal, for `Drafter.Compositor`.

    {:ok, writer} = Drafter.Render.Writer.start_link(:tty)
    Drafter.Render.Writer.write(writer, text_rows)
    Drafter.Render.Writer.write_image(writer, image_bytes)

Every write is one call to the sink, in the order the calls were made, so no sequence
is ever cut by another, and neither call blocks the caller. `write/2` is for text and
control sequences and is never dropped; `behind?/1` tells whether any of it is still
unwritten, for a caller that would rather merge its next frames into one than queue
them. `write_image/2` is for a frame's image bytes: while the terminal is slow to
accept one, a newer image that has arrived by the time an older one's turn comes
replaces it, so the terminal sees the latest picture and no backlog.

The sink is a function of iodata, or `{:tty, fallback}` to write `/dev/tty` — which the
writer opens itself, since a raw file is usable only by the process that opened it —
using the `fallback` function when `/dev/tty` cannot be opened.

# `behind?`

```elixir
@spec behind?(GenServer.server()) :: boolean()
```

Whether text handed over by `write/2` is still waiting to be written.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `flush`

```elixir
@spec flush(GenServer.server()) :: :ok
```

Return once everything handed over so far has been written.

# `start_link`

```elixir
@spec start_link((iodata() -&gt; term()) | {:tty, (iodata() -&gt; term())}) ::
  GenServer.on_start()
```

Start a writer whose `sink` is a function of iodata, or `{:tty, fallback}`.

# `write`

```elixir
@spec write(GenServer.server(), iodata()) :: :ok
```

Write `bytes` after everything already handed over. Asynchronous, never dropped.

# `write_image`

```elixir
@spec write_image(GenServer.server(), iodata()) :: :ok
```

Write a frame's image `bytes` after everything already handed over, unless a newer image has arrived by then. Asynchronous.

---

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