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.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Bash getopts Long-Options Parser Prompt
Build a robust Bash option parser that supports both short flags and GNU-style long options with validation and a generated usage block
-
Python argparse Subcommand CLI Without Dependencies Prompt
Scaffold a multi-command Python CLI using only the standard-library argparse — subparsers, shared global flags, env-var defaults, and clean exit codes — for scripts that must run with zero pip installs.
-
Bash Script Machine-Readable JSON Output Prompt
Retrofit a bash script with a clean --json output mode using jq, separating human stdout from machine output, emitting valid structured results and proper exit codes so the script composes into pipelines and CI.
-
Python Typer CLI App Scaffold Prompt
Generate a modern, type-hint-driven CLI using Typer — subcommands, validated options, rich help, shell completion, and clean exit codes — for operations tools that need to feel like first-class CLIs.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.