The behaviour for Tuix applications, modeled after Phoenix LiveView.
An app holds its state in assigns, reacts to events and messages, and
declaratively describes its UI in render/1. Tuix re-renders after every
callback and writes only the changed terminal cells.
Example
defmodule Counter do
use Tuix.App
@impl true
def mount(_opts, app), do: {:ok, assign(app, count: 0)}
@impl true
def handle_event(%Tuix.Event.Key{key: :up}, app),
do: {:noreply, update(app, :count, &(&1 + 1))}
def handle_event(%Tuix.Event.Key{key: :down}, app),
do: {:noreply, update(app, :count, &(&1 - 1))}
def handle_event(%Tuix.Event.Key{key: "q"}, app),
do: {:stop, :normal, app}
def handle_event(_event, app), do: {:noreply, app}
@impl true
def render(assigns) do
box border: :rounded, padding: 1 do
text("Count: #{assigns.count}", fg: :green)
end
end
end
Tuix.run(Counter)Because the runtime is a regular process, any Elixir message — timer ticks,
Task results, PubSub broadcasts — can drive the UI through
handle_info/2.
Summary
Callbacks
Handles keyboard and resize events.
Handles arbitrary Elixir messages sent to the runtime process.
Initializes state. Receives the options passed to Tuix.run/2.
Returns the element tree for the current assigns.
Functions
Assigns a key/value pair (or many, from a keyword list or map) into the app.
Clears focus.
Focuses the element with the given id.
Returns the id of the currently focused element, or nil.
Updates an existing assign with a function.
Types
Callbacks
@callback handle_event(event :: Tuix.Event.t(), app :: t()) :: {:noreply, t()} | {:stop, reason :: term(), t()}
Handles keyboard and resize events.
@callback handle_info(message :: term(), app :: t()) :: {:noreply, t()} | {:stop, reason :: term(), t()}
Handles arbitrary Elixir messages sent to the runtime process.
Initializes state. Receives the options passed to Tuix.run/2.
@callback render(assigns :: map()) :: Tuix.Element.t()
Returns the element tree for the current assigns.
Functions
Assigns a key/value pair (or many, from a keyword list or map) into the app.
Clears focus.
Focuses the element with the given id.
The element should be rendered with focusable: true and a matching
:id prop; if it is absent from the next rendered tree, focus is
cleared. Focusing nil is equivalent to blur/1.
Returns the id of the currently focused element, or nil.
Updates an existing assign with a function.