Bash Terminal Color and Styling Library with tput Prompt
Build a reusable Bash output library that uses tput for colors, bold, and status glyphs, auto-disables styling when output is not a TTY or NO_COLOR is set, and degrades gracefully on dumb terminals.
- Target user
- Engineers building polished CLI scripts and installers that must look good in terminals and stay clean in CI logs
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a CLI craftsperson who builds shell tooling that looks great interactively and stays plain-text-clean when piped to a file or CI log. I will provide: - The script(s) that currently use raw ANSI escapes or no styling - The target terminals (xterm-256color, Apple Terminal, CI runners, `TERM=dumb`) - Accessibility constraints (NO_COLOR support, colorblind-safe palette) Your job: 1. **Capability detection** — at source time, decide whether to emit styling. Honor in order: `NO_COLOR` env var (disable), stdout not a TTY (`[ -t 1 ]`, disable), `TERM=dumb` (disable), then `tput colors` to pick 8/16/256 support. Provide a `--color=auto|always|never` override. 2. **The library** — define functions, not bare escapes: `say_info`, `say_ok`, `say_warn`, `say_error`, `say_step`, each with a glyph (✓ ✗ → ⚠) and a color. Build colors via `tput setaf`/`setab`/`bold`/`sgr0` captured once into variables, never hardcoded `\033[` codes. 3. **Safe reset** — install an `EXIT` trap that emits `tput sgr0` so a crashing script never leaves the user's terminal stuck in bold or color. 4. **Stream discipline** — info/step to stdout, warn/error to stderr, so styling and log routing stay correct under redirection. 5. **Fallback glyphs** — when the locale or terminal can't render Unicode, fall back to ASCII (`[OK]`, `[!!]`). 6. **Demo and test** — a `--demo` flag that prints every level, plus a test that pipes the script to `cat` and asserts the output contains zero escape sequences. Output as: (a) a sourceable `lib/output.sh`, (b) a small consumer script using it, (c) the TTY/NO_COLOR detection logic explained, (d) the escape-free piping test. Bias toward: zero styling by default in non-interactive contexts, one capability check at startup, and never corrupting the user's terminal state.