Skip to main content

Prompts and progress

Not every program that talks to a terminal is a full-screen app. An installer asks a few questions, a deploy tool wants a yes or no, a build shows a few bars and then gives the prompt back. Prompt is that layer: each call asks one question in a small strip at the bottom of the terminal, returns the answer, and leaves one summary line behind in the scrollback, the way git leaves its output.

import io.worxbend.tui.dsl.*

enum Tier:
case Free, Pro, Team

@main def setup(): Unit =
val name = Prompt.text("Project name", validate = Prompt.required)
val tier = Prompt.select("Tier", Tier.values.toSeq)
val ok = Prompt.confirm("Create it?", default = true)
if ok.contains(true) then
Prompt.task("Scaffolding") { log =>
log("writing build.mill")
log("writing src/Main.scala")
}

The terminal is left like this:

? Project name glyphora
? Tier Pro
? Create it? yes
✓ Scaffolding

Every prompt returns an Option: Some(answer), or None when the user pressed Ctrl-C (or the input ended, when the answers come from a pipe). Nothing throws on a cancel, so for-comprehensions over several prompts stop at the first None.

The prompts​

CallAnswersKeys
Prompt.text(label, default, validate)Option[String]typing and the editing chords; Enter
Prompt.password(label, validate, mask)Option[String]as text, input hidden; mask = Some("•") shows a bullet per character
Prompt.confirm(label, default)Option[Boolean]y/n answer at once; Enter takes the default
Prompt.select(label, options, render)Option[A]Up/Down or k/j; Enter
Prompt.multiSelect(label, options, render, preselected, min)Option[Seq[A]]Up/Down; Space ticks; Enter
Prompt.number(label, default, min, max)Option[Long]as text
Prompt.path(label, default, base, mustExist)Option[Path]as text; Tab completes
Prompt.form[A]Option[A]one field per step; Shift+Tab goes back
Prompt.task(label)(log => work)Option[A]Ctrl-C cancels
Prompt.loader(label, items)(f)Option[Seq[B]]Ctrl-C cancels

validate returns Left(message) to reject what was typed; the message is shown under the field and the prompt stays open. Prompt.required is the validator for "not blank". An empty text answer is its default.

select and multiSelect take any values and a render to show them, so an enum or a case class is chosen directly rather than through its label. path completes against the directory being typed: one match fills in, several fill in their common prefix and are listed under the field.

The summary for a password is a fixed ********, never the secret's length. While it is typed the field is invisible by default; Prompt.password("Token", mask = Some("•")) draws one bullet per character instead, so the user can see how much they typed. The default writes no secret characters into the frame or terminal output, and keeps the painted caret at the start of the field without moving the hardware cursor. Rejected passwords use a generic translated diagnostic instead of the validator's text, which might itself quote the secret. Scripted errors redact the answer too.

A form, one field per step​

Prompt.form[A] asks for every field of a case class in declaration order, using the same derivation as deriveForm: a Boolean field is a confirm, an enum a select, everything else typed text checked by the field type's own parser. A field that does not parse keeps the form on that field.

final case class Service(name: String, port: Int, tls: Boolean)

val service: Option[Service] = Prompt.form[Service]

Shift+Tab (the focus-previous framework key) returns to the previous field with its answer still in it; Ctrl-U kills to the start of the line, as in any input. To check the assembled value as well, pass the spec and its validators to Prompt.formOf; a failing check sends the user back to its field:

import io.worxbend.tui.macros.deriveForm

val spec = deriveForm[Service]
val port = spec.field(_.port).validate(p => p > 0 && p < 65536, "not a port")
val service = Prompt.formOf(spec, port)

Work under a spinner​

Prompt.task runs its body on a thread of its own, under a spinner, with the last few lines it logs scrolling underneath. When the work ends the strip collapses to one line: ✓ label, or ✗ label <message> when the work threw, in which case the throwable is rethrown after the line is drawn. Anything the work prints to standard output lands in the log rather than over the strip.

Ctrl-C draws the ✗ label (cancelled) line at once, interrupts the work's thread and then waits for it to finish — at most two seconds (CancelJoinBound) — before answering None. Work that honours the interrupt (a sleep, a blocking read, an awaited future) has therefore cleaned up by the time the caller continues; work that ignores it is left running on its daemon thread after the two seconds, and ends with the process.

Prompt.loader is the same with a progress row instead of the log: it runs f over every item and draws the bar, the percentage and the ETA as it goes. Reports coalesce into a single pending UI update instead of queueing one callback per item. Cancellation stops the framework's loop before the next item; an already-running f must still honour the interrupt to stop immediately.

val sizes: Option[Seq[Long]] = Prompt.loader("Hashing", files)(file => hash(file))

Answers for tests and CI​

A PromptSession decides where answers come from. Every prompt takes one as a using parameter and finds the terminal one when the caller names none. To pre-supply the answers, bring your own:

given PromptSession = Prompt.answers(Seq("glyphora", "Pro", "y"))

Answers are consumed in order — one per prompt, one per field of a form — and parsed exactly as a typed answer would be: an option by its label or its 1-based number, y or n, a comma-separated list for multiSelect, an empty string for the default. Each summary line is printed as if the user had answered. An answer the prompt rejects, or a prompt with no answer left, throws IllegalArgumentException: a script that no longer matches the program is a bug, and a CI job that hangs on a question is worse.

session.transcript holds every summary line the session produced, which is what a test asserts on:

val session = Prompt.answers(Seq("alpha"), echo = _ => ())
assert(Prompt.text("Name")(using session) == Some("alpha"))
assert(session.transcript == Seq("? Name alpha"))

To drive the interactive prompt itself, run it on a HeadlessBackend under Pilot with PromptSession.on(backend) and send it keys.

Each terminal prompt is a one-shot app on a backend of its own, but the terminal is asked about its capabilities only once per process: the first prompt probes, and every later prompt opens its backend with that answer (JLine3Backend.create(knownCapabilities = …)), so a run of ten questions costs one probe round trip, not ten.

Without a terminal​

When there is nothing to draw on, prompts degrade to plain line-based questions instead of failing: ? Name: is written to standard output and a line is read from standard input. That happens when standard output or standard input is not a terminal, when CI is set, or when the terminal cannot be opened (TERM=dumb). A terminal that fails while a prompt is running falls back the same way: the questions are asked again as lines, and a Prompt.task already under way is waited for rather than run twice. Only a bug in the app's own code — a validator or a view that throws — escapes, as the exception it threw. PromptSession.terminal takes a fallbackWhen to decide differently — the same idea as Laravel Prompts' fallbackWhen — and PromptSession.plain(read, write) builds the line shape over any reader and writer. A standard-input password read from a terminal uses System.console().readPassword with echo disabled, even if output is redirected. If no secure console reader is available it refuses the read rather than echoing a secret; piped answers still use the shared line reader. A custom plain reader owns its own terminal echo policy.

printf 'glyphora\n2\ny\n' | my-installer

reads its three answers from the pipe. See the FAQ for why a piped standard input is never bypassed in favour of /dev/tty.

Multi-task progress​

multiProgress(tasks, columns) is the element behind loader, for an app that tracks several pieces of work at once. Each row is one caller-owned ProgressTask, each column one ProgressColumn:

val fetch = ProgressTask("fetch sources", Some(120L))
val build = ProgressTask("build", Some(8L))

def view(using ReactiveScope, Theme): Element =
multiProgress(Seq(fetch, build), ProgressColumn.Default :+ ProgressColumn.Elapsed)
fetch sources █████░░░░░░░░░░░░ 30% 00:28 00:12
build ████████████░░░░░ 75% 00:04 00:12
ColumnShows
ProgressColumn.Descriptionthe task's label
ProgressColumn.Bar(width)the bar: width cells, or a share of what the text columns leave
ProgressColumn.Percentage 40%
ProgressColumn.Etatime left at the current rate, mm:ss
ProgressColumn.Rate(bytes)12.5 it/s, or 1.2 MiB/s
ProgressColumn.Bytes1.0 KiB/4.0 KiB
ProgressColumn.Elapsedtime since the start, frozen at the finish
ProgressColumn.Remaining60 left
ProgressColumn.SpinnerColumn(preset, done)a spinner, then ✓

ProgressColumn.Default is description, bar, percentage and ETA. Text columns are as wide as their widest value, measured in terminal columns, so a CJK label lines up with an ASCII one. A value that cannot be known yet prints - or --:--, never a zero.

ProgressTask holds the label, the optional total, the completed count and its start and finish stamps; advance(n), update(n) and finish() move it. Like every widget state it is render-thread-only, and a write to it repaints the element on its own, so a worker reports progress by posting task.advance() back to the render thread from an Async continuation. Times come from the task's own clock, the monotonic system clock by default; a test passes () => manualClock.elapsed and the ETA becomes exact.

The rate — and the ETA computed from it — is measured over a sliding window, the last ten seconds by default, so a transfer that speeds up or stalls shows it within one window instead of averaging it away; a stall longer than the window prints -/--:--. ProgressTask(label, total, rateWindow = Some(30.seconds)) widens it, and rateWindow = None keeps the average since the start. Reports within one sixty-fourth of a window coalesce, so estimates have that time resolution and history stays bounded even for millions of reports.