Skip to main content

Learn from complete apps

The repository includes twelve 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:worxbend/glyphora.git
cd glyphora
./mill examples.showcase.run

Pick an example​

ExampleRunBest place to learn
hello-world./mill examples.hello-world.runTuiApp, panels, text, local key handling
counter./mill examples.counter.runsignal update → tracked redraw, concise onKey
todo-list./mill examples.todo-list.runtext entry, list state, focus switching, delete action
dashboard./mill examples.dashboard.runtick-driven gauges, sparklines, chart layout
form-demo./mill examples.form-demo.runcompile-time form derivation and validation
weather./mill examples.weather.runreal HTTP through Async.runCatching, loading/error states
showcase./mill examples.showcase.runscaffold, themes, palette, screens, toasts, splash
procmon./mill examples.procmon.runa sortable table over refreshing rows, selection that survives a refresh — guide
airsensor./mill examples.airsensor.runpolling, threshold bands, trend arrows, loading/ready/error — guide
loadtest./mill examples.loadtest.runconcurrent work off the render thread, histogram and percentiles — guide
doctor./mill examples.doctor.runwhat glyphora detected about your terminal, then every decoded key and mouse event — troubleshooting
gallery./mill examples.gallery.runevery exported factory on its own page, grouped by family — the visual index of the toolkit, and a golden frame per factory; pages whose factory takes simple values (gauge, sparkline, barChart, progressBar, spinner, badge) have a Props panel that edits them live

Every app exits with q or Esc (doctor only with q: it echoes Esc like any other key; gallery with Ctrl+Q, because its pages include text fields that take q); the source comment above each app lists its full keyboard vocabulary.

Which idiom should you copy?​

The examples deliberately show two levels of key handling, and they are not alternatives of equal standing:

LevelLooks likeUse 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.

hello-world: the smallest useful tree​

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.

Read hello-world source

counter: declared bindings and tracked redraws​

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 CounterApp

Two 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.

Read counter source

todo-list: state belongs to the app​

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,
).rounded

TextInputState and ListState are created once outside view. The input and list keep their own editing/selection mechanics while the app owns the todo collection.

Read todo-list source

dashboard: time becomes state​

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.

Read dashboard source

form-demo: reflection-free derivation​

import io.worxbend.tui.dsl.*
import io.worxbend.tui.macros.deriveForm

final case class Signup(username: String, age: Int, subscribe: Boolean)

val spec = deriveForm[Signup]
val formState = FormState.of(
spec,
spec.field(_.username).validate(_.trim.nonEmpty, "required"),
spec.field(_.age).validate(_ >= 18, "must be 18 or older"),
)

The macro retains the original parsers and generates the constructor call at compile time. Submission parses all controls first, constructs a candidate only if parsing succeeds, then runs typed checks. Checks reject without replacing values: the username check above tests trimmed text but does not trim the submitted username. The UI shows errors inline and publishes Some(Signup(...)) only after every check accepts it.

Read form-demo source

weather: real asynchronous I/O​

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.

Read weather source

showcase: the integrated product surface​

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.

Read showcase source

Run tests and native builds​

# 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.nativeImage

Native images use --no-fallback and no reflection configuration. Continue with Testing or Native binaries.