Skip to main content

Print once, and run without a terminal

Most command-line tools are not full-screen apps. They want a table, a tree, a panel or a chart written to the terminal once, styled, and then they want the shell prompt back. glyphora's widgets can do that without a runner, an event loop or the alternate screen: Console renders a view to text and writes it to standard output.

The same machinery answers a second question — what a TuiApp should do when it is started without a terminal, in a pipe or on CI — through NonInteractivePolicy.

import io.worxbend.tui.dsl.*

@main def report(): Unit =
Console.print(
panel("Build")(
text("compiled 412 files").fg(Color.Green),
text("2 warnings").fg(Color.Yellow),
)
)

Console.print(view) lays the view out at the terminal's width, asks the view how many rows it needs at that width (the same intrinsicHeight measurement a column uses to size its children), composes one frame through Snapshot and writes it as newline-separated rows. There is no cursor movement in the output, so it lands in the scrollback like any other line a program prints, and it can be redirected into a file.

Every parameter defaults to what the process environment says, and each can be given explicitly:

Console.print(markdown(readme), width = 80, colors = ColorDepth.NoColor)
Console.print(table(rows, header), height = Some(12)) // decide the height yourself
Console.print(view, theme = Theme.Light)
ParameterDefault
widththe terminal's width when standard output is one; else COLUMNS; else 80
colorswhat ColorDepth.detect() resolves on a terminal; no colour when standard output is not a terminal, unless forced
heightthe view's measured height at width; else LINES; else 24
themeTheme.Dark

Colour and the environment​

Print mode follows the conventions every modern CLI does:

  • NO_COLOR set to any non-empty value turns colour off. Text attributes — bold, dim, underline — are still written, as no-color.org asks.
  • FORCE_COLOR forces colour on, even into a pipe: 1, 2 and 3 pick sixteen colours, the 256 palette and true colour; any other value forces the terminal's own capability; FORCE_COLOR=0 turns colour off. CLICOLOR_FORCE is honoured the same way.
  • TERM=dumb means no escape sequences at all.
  • Output that is not a terminal — a file, a pipe into less or grep — gets no colour unless one of the force variables says otherwise. TERM is inherited by a piped process, so trusting it there would fill the file with escape sequences.

With colour off and only colour-styled content, the output contains no escape sequence at all: NO_COLOR=1 mytool > report.txt leaves plain text behind.

Raw widgets and styled text​

A widget that is not wrapped in an element prints at the size you give it, and a Text prints one row per line:

Console.print(statusWidget, Size(40, 3)) // any `Widget` value
Console.printLines(markup"[bold]done[/] in [green]1.2s[/]")

To get the text without writing it — for a log line, a message, a test — Console.render(view, width, colors) returns the same string print writes, minus the trailing newline. Console.renderWidget(widget, size, colors) and Console.renderLines(text, colors) are the counterparts of the other two.

Console is callable from any thread: it registers no render loop and writes no signal. It is not interactive or animated — focus is drawn on the first focusable element, an animated widget renders at whatever position it computes for the time it is handed, and reading a Signal subscribes nothing.

Console shadows scala.Console

Under import io.worxbend.tui.dsl.* the name Console is glyphora's. Code that wrote Console.err.println(...) with that import writes scala.Console.err or System.err instead.

Run a TuiApp without a terminal​

By default a TuiApp started without a terminal refuses to run: it prints glyphora: terminal not supported: … on standard error and exits 1. That stays the default — it is what a user who piped a full-screen app by mistake needs to see, and what CI's smoke test of every example asserts.

An app that has something useful to say without a terminal opts in through its configuration:

object Main extends TuiApp:
override def config: RunnerConfig =
RunnerConfig().withNonInteractive(NonInteractivePolicy.PrintFinalFrame)

def view(using ReactiveScope, Theme): Element = dashboard()

The policy applies when standard output is not a terminal, or when CI is set (to anything but empty, 0 or false). The app then runs once without a terminal: onStart runs, the first frame is composed at COLUMNS×LINES (80×24 when unset), and the run ends the first time the loop would wait for a key — none is ever going to arrive — so an app that only exits on q still exits, and exits 0. Background work and ticks are not waited for: what is printed is the app's initial state plus whatever onStart set synchronously — unless the policy says awaitIdle.

NonInteractivePolicyWhat a run without a terminal does
FailToday's behaviour, and the default: terminal not supported, exit 1.
PrintFinalFrameWrites the last frame the run composed, styled per the colour rules above.
PrintEveryFrameWrites every frame the run composed, in order.
PlainWrites the text rows of each frame with no styling, identical consecutive frames once — for a screen reader, a log or grep.

Waiting for the app to settle​

An app that loads its content in the background — an Async.run started from onStart, a timer, a few ticks of an animation — would print its "loading…" state. awaitIdle(timeout) makes any printing policy wait for it:

override def config: RunnerConfig =
RunnerConfig().withNonInteractive(NonInteractivePolicy.PrintFinalFrame.awaitIdle(5.seconds))

The run then keeps going — ticks fire, Async results land and repaint — until the loop is idle or timeout has passed since the run started, whichever is first. Idle means no queued render-thread work, no Async work, delayed or repeating timer still outstanding, and no frame composed since the loop last waited. An app that is never idle (an Async.every, an animation repainting on every tick) prints the frame it had reached at the timeout; the timeout is the ceiling, not the usual cost. awaitIdle on Fail is still Fail, and policy.output / policy.idleTimeout read back what was asked for.

While a no-TTY run is printing, the app's own println goes straight to standard output instead of being captured above the UI, because there is no UI for it to land above.

Deciding for yourself​

TuiApp.printTarget is the seam the policy consults. Override it to force the interactive path regardless of the environment (None), or to capture what a no-TTY run prints in a test:

val out = StringBuilder()
val app = new MyApp:
override protected def printTarget: Option[PrintTarget] =
Some(PrintTarget(Size(60, 10), ColorDepth.NoColor, text => out.append(text)))
assert(app.run() == Right(()))
assert(out.result().contains("Ready"))

PrintTarget.detect() is what the default answers when the process is non-interactive; Console.isNonInteractive() is the check it makes.

Testing printed output​

Console.render is a pure function from a view to a string, so a test asserts on it directly — no Pilot needed. For whole-app behaviour without a terminal, override printTarget as above. Both inject everything they read: Console.detectedColors(env, terminal), Console.detectedWidth(env, terminalWidth) and Console.isNonInteractive(env, terminal) take the environment and the "is standard output a terminal" answer as parameters, so a test never depends on how its JVM was started.

Next: ask questions before or after a print with Prompts, drive whole apps headlessly in Testing, and ship the result as one binary with Native image.