Crust logoCrust

Progress

Spinners and determinate progress indicators for terminal tasks

@crustjs/progress writes progress indicators to stderr by default. It supports task-scoped spinners, imperative handles, determinate progress, themes, injected output, and static non-TTY output.

Install

npm install @crustjs/progress

Quick example

await spinner({
	message: "Building",
	task: async ({ updateMessage }) => {
		updateMessage("Running checks");
	},
});

spinner() starts the indicator, awaits task, and prints a final line. The task receives updateMessage().

✓ Running checks

API

Call spinner({ task }) for a task-scoped indicator. Call spinner() without task for a SpinnerHandle, or progress() for a ProgressHandle.

spinner(options)

await spinner({ message: "Building", task: async () => build() });
// success: ✓ Building
// failure: ✗ Building
  • task may return a value or a promise; spinner({ task }) always resolves to its result.
  • A failed task prints ✗ <message>, then rejects with the task error.
  • Non-TTY output omits animation and cursor-control sequences.

Frame layout

Interactive spinners and progress indicators draw the animated frame on one row:

  • The frame is cut with … at its first line break or before the terminal's last column. The message is cut first, so progress() keeps its count; the final ✓/✗ line prints the message in full.
  • A line printed through Crust's process writer while the frame is live, such as an action's default stdout() or stderr() line on the terminal, erases the frame first and redraws it below the line.
  • Other writes, such as console.log or process.stdout.write, start on the spinner's row: a complete line stays on screen after the frame text it interrupted, and a write without a trailing newline is erased by the next frame (if it wraps, only its last row is erased).
  • Narrowing a terminal that reflows the cursor's row can leave the start of the previous frame above the spinner.

Prop

Type

Imperative spinner

const handle = spinner({ message: "Deploying" });
handle.start();
handle.updateMessage("Uploading");
handle.stop("success", "Deployed"); // ✓ Deployed

stop() defaults to "success" and the last message shown; later calls do nothing. Without start(), nothing renders until stop() prints the final line.

Cancellation

A spinner or progress indicator follows the signal option, else the ambient withTerminalIO() signal, which inside a Crust command is the invocation's signal. When it aborts while the indicator runs, the indicator stops with ✗ and its current message, unless a stop() in the same turn renders its own final line (a finally that the cancellation reaches, or the failed task of spinner({ task })). An indicator started after the abort, for example to show cleanup, ignores it.

Prop

Type

progress(options)

const bar = progress({ total: 2, message: "Files" });
bar.start();
bar.advance(1);
bar.advance(1);
bar.stop("success", "Files complete");

progress() renders <message> (<current>/<total>). advance() defaults to one unit, changes the count, and may replace the message.

✓ Files complete (2/2)

Prop

Type

Calling advance() before start() changes the count without repainting. The updated count appears after the handle starts or in its final line.

Prop

Type

SIGINT policy

const handle = spinner({ message: "Working", sigint: false });
sigintOn SIGINT while an interactive indicator runs
"exit" (default)Restores the cursor and re-raises SIGINT when no other listener remains (exit 130). When another listener owns SIGINT, as Crust's execute() does, leaves ending the process to that owner and stops with ✗ on the next turn
falseInstalls no handler; call stop() from your own cleanup

Inside a Crust command the default works: Ctrl-C aborts the invocation's signal, which stops the indicator with its final line (see Cancellation). A SIGINT the process inherited as ignored stays ignored. The handler is a real process listener even with an injected sink, so prefer false in tests. Calling process.exit() while an interactive indicator runs restores the cursor under either policy when the sink writes synchronously.

Custom spinner frames

spinner({ message: "Working", spinner: { frames: ["⊶", "⊷"], interval: 150 } });

Built-in names are "dots", "line", "arc", and "bounce". A custom frame array must not be empty.

Themes

import { spinner } from "@crustjs/progress";
import { cyan } from "@crustjs/style";

spinner({ message: "Working", theme: { spinner: cyan } });

A ProgressTheme has spinner, message, success, and error style slots. Per-call slots (a PartialProgressTheme) override the defaults; unset slots keep the default styles, which detect color from the sink (see Non-interactive behavior). The exported defaultTheme holds the same styles but detects color from stdout.

Output sinks

const transcript: string[] = [];
const sink: TerminalOutput = { isTTY: false, write: (text) => transcript.push(text) };
await withTerminalIO({ output: sink }, () =>
	spinner({ message: "Deploying", task: async () => {} }),
);

Output goes to the first of:

  1. the per-call sink option;
  2. the ambient withTerminalIO() output;
  3. standard error.

withTerminalIO(io, fn) takes the same optional { input, output, signal } as @crustjs/prompts and shares its ambient scope.

Standard error uses the same writer as Core's default stderr, so a spinner inside a Command Action stays in order with the action's stderr() lines. Outside Windows it writes synchronously to file descriptor 2, so patching process.stderr.write does not capture it; inject a sink instead.

Non-interactive behavior

✓ Deploying
✗ Deploy failed

A non-TTY sink gets one final line with no animation or cursor-control sequences. updateMessage() still determines the final message.

Default theme slots decide color from sink.isTTY, applying the NO_COLOR and FORCE_COLOR rules from @crustjs/style color control. Theme functions you pass, such as @crustjs/style imports, keep their own detection, which checks stdout.

On this page