Layout Tiers: The Language That Paints Phosphor

November 13, 2026

Inside Phosphor, .phos scripts paint their own screens. This is the post about the language those scripts speak — and about the arc that took it from “a script that can hand-paint rows” to “a screen describes itself as a widget tree” in twelve concrete milestones. The bugs found along the way (an SGR crash only a live wire could produce, and the CP437 wire the end-to-end probe finally taught the harness about) are covered in their own post — this one is the architecture.

The problem: every screen was its own painter

Phosphor’s .phos scripts paint their own UI — the blackjack door, the module screens, the language samples. (The rest of the door fleet — Wordle, Tetris, the dungeon crawler, the arcade ports — is plain Java; blackjack is the one script door in the fleet, and this post is about the tier it renders through.) Before the layout tiers, each one built its display by constructing labels row by row in code: borders were string concatenation, the statusbar was whatever the script happened to append, colors depended on which code path painted them. Every new door re-invented alignment, and every host (terminal door player, headless screens, the script editor) painted slightly differently — which is exactly the divergence that later let a legal-but-empty background color crash one host while tests stayed green.

The language: a layout block

Today a script declares its display declaratively. Inside a screen "…" { … } or door "…" { … }, a layout { … } block is a widget tree:

layout {
    banner id = "banner" {
        text = "BLACKJACK"
        fg = "bright_yellow"
        align = "center"
    }
    region id = "table" {
        border = "rounded"
        title = "Your hand"
        fg = "green"
        padding = 1
    }
    label id = "msg" "" {
        align = "center"
    }
    statusbar id = "statusbar" {
        left = ""
        center = "[D]eal  [H]it  [S]tand  [Q]uit"
    }
}

The vocabulary is deliberately small and every name is a keyword: label, banner (text or .ans reference), region (children + border, title, padding), panel, tabs, statusbar, separator, spacer, table/list/input/textarea/progress/chart, menu_bar — plus extension widgets the runtime registers (below). Shared style properties (border, title, fg, bg, align, padding, width) mean the same thing on every widget that honors them, with per-widget defaults.

Three grammar laws keep the language simple enough to read at a glance and are pinned by committed tests:

  1. layout only exists inside a screen/door wrapper.
  2. Panel children go inside content { … } — an inlined child is a parse error, because an inline widget in a panel block reads as a property.
  3. Label text is positional (label id = "x" "text"); the trailing block holds style, never text =.

And a junk law runs through everything: the parser validates names, not values. Junk align, junk border, junk width never throw — they fall back to defaults at render time. A broken value degrades the visual, never the session.

The expression contract: static shapes, dynamic updates

Layout properties are read without the evaluator. String literals render as typed; interpolated values render their placeholder visibly ("Hi ${user.name}" becomes Hi {user.name}); calls render empty. That is a feature, not a limitation: it means a render pass can walk a layout without ever executing script code — no evaluation, no side effects, no re-entrancy. All dynamism flows through targeted update builtins called from handlers:

function update_message(extra) {
    msg = "Bankroll: " + bankroll + " | Bet: " + bet + " | " + extra
    update("msg", msg)
}

update / append / prepend change content, hide / show collapse whole subtrees, and the next render picks the changes up through a fixed precedence law (accumulated widget lines → update content → declared literal). A test pins that no {placeholder} ever leaks into a rendered row.

Geometry and the width seam

Panels compose: direction = "row"/"column" with child weight/grow allocates fixed claims first, then weighted remainder, leftover to growers — and a panel without direction flattens byte-identically to its children, so older scripts never changed meaning. All width math lives in one seam (BoxPainter): visual-width measurement, truncation, padding and wrapping are SGR-aware — clipping by string length can sever an ESC[0m and bleed color down the screen, so the seam never does — and every repeat count clamps at 256 so a junk layout can’t balloon memory.

One law worth knowing if you script Phosphor: rows justify across the content source’s width, and the door feed uses your live terminal columns — resize and the door re-justifies on its next paint. The statusbar’s center segment exists precisely so a key hint looks centered (putting it in right right-justifies it — yesterday’s bug, now a test).

Interaction: input, focus, timers — on an event stream

Screens aren’t just pictures. The input widget binds keyboard focus to a script-owned buffer (set_focus("id"), Backspace deletes, Esc clears), firing on_change per keystroke and submitting through its on_submit block — all event-stream, with a hard law that no handler ever blocks (there is no pause/await/yield in the evaluator, by contract). select and scroll move table focus and scroll state; regions with max_height become real viewports.

Timers are the part most likely to bite an implementer, so the contract is strict: scheduled handlers never paint on the scheduler thread. A schedule firing just sets a repaint flag on the mounted screen or door and the host consumes it on its own GUI loop; the tick only runs at all when a schedule ... declaration exists. Handler mutations replace collections by reference instead of mutating in place (no ConcurrentModificationException under a lock-free read path), and every schedule handler runs exactly once per fire. None of that is visible to the script author — it exists so a door’s “refresh your dashboard every second” hobby can’t freeze the paint pass.

Scripts can listen, too: bus events

The Phosphor bus (in-process — deliberately no websocket surface) delivers board, mail, chat and session events into script on-handlers, alongside screen-scoped timers. Board events carry the historical names (BOARD_POST fires when a reply posts, BOARD_REPLY when a thread is created) — kept stable rather than renamed, because scripts already listen for them.

Extension widgets: Java power, script syntax

Widgets implemented in Java register into the runtime’s default widget set and join the layout grammar through a static parser whitelist — the gallery door exercises the built-ins:

card id = "card_ace" {
    rank = "A"
    suit = "s"
}
stats id = "system_stats" { … }
ticker id = "tape" { … }

They follow the same contracts as native widgets: static properties only, update() for dynamism, and degrade-always rules (an unregistered widget name renders its accumulated lines; a throwing widget renders nothing — partial output never survives a throw). The script editor gained a live preview pane on the same renderer, refreshing in place as you save — the language’s fast feedback loop is the editor, not a redeploy.

What it’s worth

  • One renderer, five hosts. Script doors, headless screens, the manager, the editor preview and the bus-driven tick all speak the same unit pipeline — a new widget or law lands everywhere at once.
  • Scripts got expressive without getting dangerous. Junk input can’t throw; handlers can’t block; paint can’t run on the scheduler; memory can’t balloon on a typo.
  • The DSL is a real API surface. It’s documented (docs/phosphor-layout.md), exercised by samples/phosphor-layout-gallery.phos, pinned by ~1,300 module tests plus the server’s 9,800-law suite, and every claim in this post is checked against committed code.

The arc ran M0 → M4 across two repos — styled render units, the walk, box painting and geometry in the shared jterm-phosphor module, then input, timers, ext widgets and the editor preview on the server side — and it’s deployed: the blackjack door you connect to today renders through it.