CLI Progress Reporting
Report progress for a long CLI task with a single-line spinner or a bar, throttle redraws, and clear the line on completion for clean logs.
On this page
Introduction
This guide covers CLI progress reporting, a recurring concern in the cli-tooling scenario. A CLI task that runs for a minute with no feedback looks hung. A spinner for indeterminate work or a bar for measurable progress, throttled so it does not repaint on every tick and cleared on completion, gives the user a sense of progress and a clean log line at the end.
Spinner or bar
Use a bar when you know the total so the percentage is honest, and a spinner when you do not, so an unknown total does not show a fake percentage.
The trade-off worth naming is that use a bar when you know the total so the percentage is honest, and a spinner when you do not, so an unknown total does not show a fake percentage adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
import ora from 'ora';
const s = ora('Building').start();
// or a bar when total is known:
// bar.update(current, total);
Throttle redraws
Repaint at most a few times per second so a fast task does not spend more time drawing than working, which is the cost of unthrottled progress.
The trade-off worth naming is that repaint at most a few times per second so a fast task does not spend more time drawing than working, which is the cost of unthrottled progress adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Clear on completion
On finish, clear the spinner or finalize the bar with a success line, so the log ends with a clean summary instead of a half-drawn progress line.
The trade-off worth naming is that on finish, clear the spinner or finalize the bar with a success line, so the log ends with a clean summary instead of a half-drawn progress line adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
s.succeed('Built 42 files in 1.2s');
// or on error: s.fail('Build failed');
Quiet when not tty
When stdout is not a tty, skip the spinner and write a single summary line at the end, so a log file is not full of spinner frames.
The trade-off worth naming is that when stdout is not a tty, skip the spinner and write a single summary line at the end, so a log file is not full of spinner frames adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
ETA from rate
Track the rate of progress and show a rough ETA, labeled as approximate, so a fluctuating task does not make the number jump erratically.
The trade-off worth naming is that track the rate of progress and show a rough eta, labeled as approximate, so a fluctuating task does not make the number jump erratically adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test quiet
Run with non-tty stdout and assert no spinner frames and a single summary line, so the log-friendly path is real.
The trade-off worth naming is that run with non-tty stdout and assert no spinner frames and a single summary line, so the log-friendly path is real adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: ora, cli-progress.
FAQ
- Spinner or bar?
- Use a bar when you know the total, because a percentage and an ETA are meaningful; use a spinner when you do not, because a bar at an unknown total lies.
- Should progress go to a log file?
- No; progress is for the tty, so disable it when stdout is not a tty and write a single summary line instead, which keeps logs parseable.
Report a problem with this articleOpens a pre-filled issue on GitHub.