Current section

Files

Jump to
tuix README.md
Raw

README.md

# Tuix
A terminal UI framework for Elixir, inspired by [OpenTUI](https://opentui.com).
Build declarative, component-based TUIs with a LiveView-style programming
model: state lives in assigns, events arrive in callbacks, and `render/1`
describes the UI as a tree of boxes and text. Tuix resolves the tree with a
flexbox-subset layout engine and writes only the terminal cells that changed.
```elixir
defmodule Counter do
use Tuix.App
@impl true
def mount(_opts, app) do
{:ok, assign(app, count: 0)}
end
@impl true
def handle_event(%Tuix.Event.Key{key: "+"}, app) do
{:noreply, update(app, :count, &(&1 + 1))}
end
def handle_event(%Tuix.Event.Key{key: "q"}, app) do
{:stop, :normal, app}
end
def handle_event(_event, app), do: {:noreply, app}
@impl true
def render(assigns) do
box border: :rounded, title: "Counter", padding: 1, gap: 1 do
text "Count: #{assigns.count}", fg: "#00FF00", attrs: [:bold]
text "Press + to increment, q to quit", fg: :bright_black
end
end
end
Tuix.run(Counter)
```
Try it: `mix run examples/counter.exs`
## Features
- **LiveView-style apps** - `mount/2`, `handle_event/2`, `handle_info/2`, and
a pure `render/1` over assigns. Any Elixir message (timers, `Task` results,
PubSub broadcasts) can drive the UI through `handle_info/2`.
- **Declarative components** - `box` and `text` are plain data constructors;
conditionals and comprehensions compose naturally inside `do` blocks.
- **Keyboard focus** - mark boxes `focusable` and Tab / Shift+Tab traversal,
focus styling, and per-element event targeting come for free.
- **Text input** - a controlled, grapheme-aware single-line input with
cursor, placeholder, masking, and horizontal scrolling; edits arrive as
events and the app owns the value.
- **Select** - a controlled list picker: arrow keys move the selection,
changes arrive as events, and long lists scroll to keep the selection
visible.
- **Flexbox-subset layout** - `flex_direction`, `flex_grow`, `gap`,
`padding`, borders, fixed and percentage sizes.
- **Diffed rendering** - frames are cell buffers; unchanged rows are skipped
with a single term comparison and only changed cells are written, as
position-addressed ANSI runs.
- **Unicode-aware** - grapheme-based cells with correct handling of
wide characters (CJK, emoji), with an ASCII fast path.
- **Pure Elixir** - no NIFs, no ports, no external binaries. Raw terminal
mode comes from OTP itself.
- **Testable without a terminal** - `Tuix.TestRenderer` renders any app or
element tree to a text snapshot or inspectable cell buffer.
## Requirements
- Elixir 1.15+
- Erlang/OTP 29+
OTP 29 provides raw terminal mode (`:shell.start_interactive/1`), the
terminfo-aware `:io_ansi` module used for terminal setup and capability
detection, and `SIGWINCH` delivery for resize events.
## Installation
Add `tuix` to your dependencies in `mix.exs`:
```elixir
def deps do
[
{:tuix, "~> 0.1.0"}
]
end
```
## Core concepts
### Apps
A Tuix app is a module using the `Tuix.App` behaviour. State lives in
`assigns`, updated with `assign/2,3` and `update/3`. Callbacks return
`{:noreply, app}` to continue or `{:stop, reason, app}` to quit.
`Tuix.run/2` takes over the terminal (raw mode + alternate screen), blocks
until the app stops, and restores the terminal on exit - including crashes.
Rendering is event-driven: nothing is written unless state changes, and
`render/1` is treated as a pure function of assigns, so no-op messages cost
nothing.
### Components
```elixir
box border: :single, flex_direction: :row, gap: 2, padding: 1 do
text "left", fg: :cyan
box flex_grow: 1 do
text "stretches to fill the remaining space"
end
text "right", attrs: [:bold, :underline]
end
```
**Box props:** `width` / `height` (cells, `{:percent, n}`, or `"50%"`),
`flex_direction` (`:column` default, `:row`), `flex_grow`, `gap`, `padding`,
`border` (`:single`, `:rounded`, `:double`), `border_color`, `title`, `bg`.
**Text props:** `fg`, `bg`, `attrs` (`:bold`, `:dim`, `:italic`,
`:underline`, `:blink`, `:reverse`, `:strikethrough`). Multi-line strings
render one line per row.
**Colors:** hex strings (`"#FF8800"`), RGB tuples (`{255, 136, 0}`), or
named atoms (`:red`, `:bright_cyan`, ...).
### Events
Keyboard input arrives as `%Tuix.Event.Key{}` with a `key` (a grapheme like
`"a"` or a named atom like `:up`, `:enter`, `:escape`) and `ctrl` / `alt` /
`shift` modifier flags. Terminal resizes arrive as `%Tuix.Event.Resize{}`
and automatically reflow the layout.
### Focus
Boxes with `focusable: true` and a stable `:id` join the focus ring. The
runtime cycles focus with Tab / Shift+Tab (in document order, wrapping),
applies `focus_border_color` / `focus_bg` to the focused element, and stamps
every key event with the focused id as `target`:
```elixir
def render(assigns) do
box flex_direction: :row, gap: 1 do
box id: :left, focusable: true, autofocus: true,
border: :single, focus_border_color: :cyan do
text "left pane"
end
box id: :right, focusable: true, border: :single, focus_border_color: :cyan do
text "right pane"
end
end
end
def handle_event(%Tuix.Event.Key{key: :up, target: :left}, app), do: ...
```
Focus can also be controlled programmatically with `focus/2` and `blur/1`
(and read with `focused/1`). Every focus change — traversal or programmatic —
is delivered to the app as a `%Tuix.Event.Focus{id: new, from: old}` event.
If the focused element disappears from the tree, focus is cleared.
Focus styles also apply to ancestors of the focused element (CSS
`:focus-within`), so a bordered box wrapping a focused input highlights
automatically; use `focus_within_border_color` / `focus_within_bg` to style
ancestors differently from the focused element itself.
Try it: `mix run examples/focus.exs`
### Inputs
`input/1` builds a single-line text input. Inputs are focusable by default
and controlled: the app owns the value, and edits arrive as
`%Tuix.Event.Input{}` events that the app assigns back (LiveView form
style) — or transforms, e.g. to enforce a format:
```elixir
def render(assigns) do
# The box border highlights while the input is focused (focus-within).
box border: :single, title: "Email", focus_border_color: :cyan do
input id: :email, value: assigns.email, placeholder: "you@example.com"
end
end
def handle_event(%Tuix.Event.Input{id: :email, value: value}, app),
do: {:noreply, assign(app, email: value)}
def handle_event(%Tuix.Event.Key{key: :enter, target: :email}, app),
do: submit(app)
```
While an input is focused it consumes printable keys, `:backspace`,
`:delete`, and `:left` / `:right` / `:home` / `:end` (grapheme-aware, with
the cursor managed by the runtime). Everything else — `:enter`, `:escape`,
ctrl combos, Tab traversal — falls through to the app with `target` set.
`mask: "•"` renders password fields; long values scroll horizontally to
keep the cursor visible.
Try it: `mix run examples/login.exs`
### Selects
`select/1` builds a vertical list picker. Like inputs, selects are
focusable by default and controlled — the selection follows the highlight,
so `:up` / `:down` / `:home` / `:end` emit `%Tuix.Event.Select{}` with the
new value immediately:
```elixir
def render(assigns) do
box border: :single, title: "Plan", focus_border_color: :cyan do
select id: :plan, options: [{"Basic", :basic}, {"Pro", :pro}], value: assigns.plan
end
end
def handle_event(%Tuix.Event.Select{id: :plan, value: value}, app),
do: {:noreply, assign(app, plan: value)}
```
Options are `{label, value}` tuples or bare strings. Navigation clamps at
the boundaries, `:enter` falls through with `target` set (keep a draft
value in assigns for commit-on-Enter flows), and lists taller than the
select scroll to keep the selection visible.
Try it: `mix run examples/select.exs`
### Testing
```elixir
test "renders the counter" do
assert Tuix.TestRenderer.render_to_text(Counter, 14, 3, count: 42) ==
"""
┌────────────┐
│Count: 42 │
└────────────┘
"""
|> String.trim_trailing()
end
```
`render/4` returns the underlying `Tuix.Buffer` for structured assertions on
individual cells and their styles.
## Design notes and known trade-offs
- **Full-tree re-render per state change** is the LiveView-style trade-off:
simple mental model, no manual invalidation. The frame diff keeps terminal
writes minimal regardless.
- **Resize detection** uses `SIGWINCH` where the OTP/platform supports it
(`:os.set_signal/2`), falling back to 250ms polling elsewhere.
- **Wide-character edge case:** a continuation cell changing without its
head cell is not repainted. This only occurs when new content partially
overlaps a wide grapheme.
- **No mouse support yet** - see the roadmap.
## Roadmap
- ScrollBox component (built on the focus model), including scrollbar
rendering
- Mouse support
- `justify_content` / `align_items` / wrapping in the layout engine
- Select enhancements: multi-select, typeahead/filtering, wrap-around
navigation, `PageUp` / `PageDown`
- Dropdown/overlay presentation for selects (needs z-order/overlay
machinery)
- Input enhancements: readline-style ctrl bindings, `max_length`, real
terminal cursor (blinking)
- Kitty keyboard protocol
- Animation / frame-loop rendering mode
## Documentation
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc):
```sh
mix docs
```
Once published, the docs can be found at <https://hexdocs.pm/tuix>.
## License
Copyright 2026 Ryan Winchester
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.