Skip to content

Lesson 5 of 9 · 23m

Terminal UI: Prompts, Tables, Color, and Restraint

Use interactive terminal features as progressive enhancement: helpful in a TTY, invisible in automation, and accessible by default.

Terminal UI is not a lesser graphical interface. It is a different medium with its own strengths: fast scanning, composability, and low ceremony. The failure mode is decoration-first design — banners, emoji, spinners, color everywhere — until the tool is unreadable over SSH and useless in CI.

Detect interactivity before promising it

import sys

interactive = sys.stdin.isatty() and sys.stdout.isatty()

Prompt only when stdin is interactive. A script running under cron cannot answer Continue? [y/N], so it should either require --yes or fail with a clear message. Output being piped should disable color and live redraws. Do not couple these checks: input and output are separate streams.

Prompts are confirmation, not workflow archaeology

For destructive actions, a prompt is reasonable:

Remove 14 stale container images? [y/N]

Default to the safe answer. If the user supplied --yes, do not prompt. If the operation is irreversible, require typing the resource name or use an explicit --force in addition. One generic confirmation for every action trains people to type y without reading.

Tables should survive copy, paste, and narrow terminals

NAME      STATE     CPU    MEMORY   AGE
web-01    running   12%    214MiB   6d
web-02    degraded  88%    912MiB   6d

Keep columns aligned and put stable identifiers first. Avoid box-drawing art for primary output; it wraps badly and makes awk/grep extraction painful. For complex data, offer --json rather than squeezing nested structure into text.

Color is emphasis, not information

Use one color for danger, one for success, perhaps one for headings. Always pair color with words or symbols because users may be colorblind, use monochrome themes, or copy output into plain-text tickets. Respect NO_COLOR and common flags:

--color auto|always|never
NO_COLOR=1

rich can produce excellent tables, syntax highlighting, and tracebacks, but use it deliberately. A dependency that renders beautiful output is not a license to make every command noisy.

Emoji and Unicode are optional decoration

A checkmark may render as tofu on minimal terminals. Prefer plain ASCII for critical markers: ok, warn, error. Unicode can be a tasteful enhancement when the terminal encoding supports it, but never make meaning depend on it.

Field notes

  • CI logs are not interactive even when they display escape sequences; use TTY detection plus explicit environment overrides.
  • Do not clear the screen in normal operation. Users expect scrollback to preserve what happened.
  • Long values need truncation with a stable width strategy, or output will jitter badly when refreshed.
  • If prompts are central to the product, test them with a pseudo-terminal; ordinary pipes do not reproduce readline behavior.

Was this lesson helpful?

Sign in to rate