Repository navigation
Examples
The repository includes ten runnable applications. Each uses the same public APIs described in this guide and has a headless end-to-end test. Start with the behavior closest to your app and read its source beside the running terminal.
The last three are larger, and each has a step-by-step guide that builds it from nothing — see Build a real app.
git clone git@github.com:oleksandr-balyshyn/glyphora.git
cd glyphora
./mill examples.showcase.run| Example | Run | Best place to learn |
|---|---|---|
hello-world |
./mill examples.hello-world.run |
TuiApp, panels, text, local key handling |
counter |
./mill examples.counter.run |
signal update → tracked redraw, concise onKey
|
todo-list |
./mill examples.todo-list.run |
text entry, list state, focus switching, delete action |
dashboard |
./mill examples.dashboard.run |
tick-driven gauges, sparklines, chart layout |
form-demo |
./mill examples.form-demo.run |
compile-time form derivation and validation |
weather |
./mill examples.weather.run |
real HTTP through Async.runCatching, loading/error states |
showcase |
./mill examples.showcase.run |
scaffold, themes, palette, screens, toasts, splash |
procmon |
./mill examples.procmon.run |
a sortable table over refreshing rows, selection that survives a refresh — guide |
airsensor |
./mill examples.airsensor.run |
polling, threshold bands, trend arrows, loading/ready/error — guide |
loadtest |
./mill examples.loadtest.run |
concurrent work off the render thread, histogram and percentiles — guide |
Every app exits with q or Esc; the source comment above each app lists its full
keyboard vocabulary.
The examples deliberately show two levels of key handling, and they are not alternatives of equal standing:
| Level | Looks like | Use it for |
|---|---|---|
App bindings (counter and every larger example) |
override def bindings = KeyBindings(binding("q", "quit")(quit())) |
anything global. One declaration also produces the status-bar hint, the help-overlay row, and the command-palette entry, and an undeliverable spec is rejected where you wrote it. |
Local element handlers (hello-world) |
panel(...).onKey(Key.char('q')) { quit() } / .onKeyEvent { … }
|
a key that belongs to one element and appears nowhere in the app's vocabulary — Enter inside a text field, d on the focused list row. |
hello-world is the smallest possible program, so it uses the raw onKeyEvent
escape hatch to keep the file to one idea. Start from counter for anything real.
object HelloWorld extends TuiApp:
def view(using ReactiveScope, Theme): Element =
panel("Hello")(
text("Welcome to glyphora!").bold.fg(Color.Cyan),
spacer,
text("Press 'q' to quit").dim,
).rounded.onKeyEvent {
case KeyEvent(KeyCode.Char('q'), _) =>
quit()
true
case _ => false
}This is the render model in miniature: describe an element tree, decorate it, and handle an event near the UI it controls.
class CounterApp extends TuiApp:
val count: Signal[Int] = Signal(0)
override def bindings: KeyBindings = KeyBindings(
binding("+", "increment")(count.update(_ + 1)),
binding("-", "decrement")(count.update(_ - 1)),
binding("r", "reset")(count.set(0)),
binding("q", "quit")(quit()),
)
def view(using ReactiveScope, Theme): Element =
scaffold(statusBar = Some(statusBar(bindings))) {
centered(34, 7) {
panel("Counter")(
text(s"Count: ${count.get}").bold.fg(Color.Green),
spacer,
text("Change state; the view follows.").dim,
).rounded
}
}
object Main extends CounterAppTwo things to take from this file. First, the key list exists once: statusBar(bindings)
renders the hints from the same values that dispatch the keys, and Ctrl+P opens a
fuzzy palette over them, so the on-screen help cannot drift away from the behaviour.
Second, reading count.get inside view is what makes the redraw automatic — nothing
in this app calls "refresh".
It is a class with a one-line object Main extends CounterApp on the end, because
TuiApp keeps its state on the instance and never resets it between runs; the test
builds a fresh CounterApp() per scenario. See Testing.
val items = Signal(Vector.empty[String])
val inputState = TextInputState()
val listState = ListState()
def view(using ReactiveScope, Theme): Element =
panel("Todo")(
input(inputState, placeholder = "what needs doing?").onKeyEvent {
case KeyEvent(KeyCode.Enter, _) => addItem(); true
case _ => false
},
spacer(1),
list(items.get.map(item => s"· $item"), listState).onKey(Key.char('d')) {
deleteSelected()
},
text("Enter add · Tab switch · ↑/↓ select · d delete").dim,
).roundedTextInputState and ListState are created once outside view. The input and list
keep their own editing/selection mechanics while the app owns the todo collection.
override def config = RunnerConfig(tickRate = Some(100.millis))
val tick = Signal(0)
override def onTick(): Unit = tick.update(_ + 1)
def view(using ReactiveScope, Theme): Element =
val t = tick.get
val load = (math.sin(t * 0.1) + 1) / 2
val samples = Vector.tabulate(60)(i =>
(math.sin((t + i) * 0.25) * 40 + 50).toLong
)
row(
panel("Load")(gauge(load)).percent(50),
panel("Throughput")(sparkline(samples)).percent(50),
)Ticks update a signal on the render thread; charts remain ordinary pure renderers.
final case class Signup(username: String, age: Int, subscribe: Boolean)
val formState = FormState.of(
deriveForm[Signup],
Field.text("username").mapValidated { name =>
if name.trim.nonEmpty then Right(name.trim) else Left("required")
},
Field.int("age").mapValidated { age =>
if age >= 18 then Right(age) else Left("must be 18 or older")
},
)The macro generates metadata and the final constructor call at compile time. The UI
shows parser/validator errors inline and publishes Some(Signup(...)) only after a
valid submit.
private def search(): Unit =
val city = cityInput.value.trim
if city.nonEmpty then
status.set(Status.Loading(city))
Async.runCatching(client.fetch(city)) {
case Right(Right(report)) => status.set(Status.Loaded(report))
case Right(Left(failure)) => status.set(Status.Failed(city, WeatherError.describe(failure)))
case Left(thrown) => status.set(Status.Failed(city, thrown.getMessage))
}Async.runCatching does both halves of the job: it runs the two live Open-Meteo
requests on a worker thread, and it delivers the answer back on the render thread, so
status.set in the callback is an ordinary signal write with no explicit
RenderThread.runOnRenderThread hop. The three cases are the three real outcomes — a
report, a Left the client produced for a bad response, and an exception the client
threw. Handling that last one is what keeps a hard failure from leaving the UI on
Status.Loading for ever. The client is injected, so the headless test uses a
deterministic fake.
The showcase combines:
- launch splash plus frame effects;
- live theme switching;
- top bar, sidebar, tabbed content, and status hints;
- command palette generated from
KeyBindings; - modal screen and focus isolation;
- tick-aged toasts;
- input, gauge, sparkline, log, and Markdown widgets;
- clipboard output through the active terminal backend.
Use it as the manual PTY test bed and the fastest tour of app-level features.
# One example's end-to-end tests
./mill examples.todo-list.test
# Every example test
./mill examples.__.test
# A self-contained executable (GraalVM required)
./mill show examples.showcase.nativeImageNative images use --no-fallback and no reflection configuration. Continue with
Testing or Native binaries.
Documentation is maintained in website/docs. Read the styled guide · API reference · MIT license