AgentSkillsCN

python-output-styler

借助Rich库,为Python脚本赋予精美的终端样式,并在纯文本模式下实现优雅回退。 当您需要创建或修改面向用户的终端输出Python脚本时,请使用此工具。 当用户提出“美化Python输出”“让Python脚本更美观”“为Python脚本添加颜色”或“提升Python终端用户体验”时,此工具同样能派上用场。

SKILL.md
--- frontmatter
name: python-output-styler
description: |
  Applies gorgeous terminal styling to Python scripts using Rich with plain-text fallback.
  Use when creating or modifying Python scripts that produce user-facing terminal output.
  Use when asked to "style python output", "make python pretty", "add colors to python script", or "improve python terminal UX".
license: MIT
user-invocable: true
argument-hint: "[script-path]"
metadata:
  author: tsilva
  version: "1.0.0"

Python Output Styling

Style all user-facing Python script output using the bundled style.py module.

Workflow

  1. Read references/style.py to understand available functions
  2. If style.py doesn't exist alongside the target script, copy from references/style.py into the same directory
  3. Import functions in the target script: from style import header, success, error, info, section
  4. Replace raw print() statements with styled output function calls
  5. Replace input() prompts with styled_input, confirm, or choose as appropriate
  6. Wrap long-running iterables with progress for animated feedback
  7. Wrap long-running functions with spin for animated spinner feedback
  8. Verify script still works when Rich is not installed (graceful degradation to plain print)

Dependency

Rich is the preferred rendering backend. Install via:

bash
uv run --with rich script.py

When Rich is unavailable, all functions fall back to plain print() output automatically.

Color Palette

NameHexRich MarkupUse
Brand#AF87FF[bold #AF87FF]Headers, emphasis
Success#87D787[#87D787]Checkmarks, completion
Error#FF5F5F[#FF5F5F]Errors, failures
Warning#FFD75F[#FFD75F]Warnings, caution
Info#87CEEB[#87CEEB]Info messages, tips
Muted#808080[#808080]Paths, secondary text

Output Functions

FunctionSymbolColorPurpose
header(title, subtitle="")boxBrandBordered header panel
section(text)━━BrandHorizontal rule divider
success(text)SuccessCompleted actions
error(text)ErrorError messages
warn(text)WarningWarnings
info(text)InfoInformational
step(text)MutedAction log (suppressed when quiet)
note(text)Muted"Note:" prefixed
banner(text)boxSuccessCompletion banner
error_block(*lines)ErrorMulti-line error box
list_item(label, value)Brand+MutedKey-value display
dim(text)MutedDe-emphasized text (suppressed when quiet)

Utility Functions

FunctionPurpose
table(headers, *rows)Formatted table with headers. headers is a list of strings, each row is a list of strings
pretty(obj)Rich inspect for any Python object (falls back to pprint)

Progress Functions

FunctionReturnsPurpose
progress(iterable, label="")Wrapped iterableRich progress bar wrapping any iterable
spin(title, func, *args, **kwargs)Function resultAnimated spinner wrapping a function call

Interactive Functions

FunctionReturnsPurpose
confirm(prompt, default=True)booly/n prompt
choose(header, *options)strNumbered selection menu
styled_input(prompt, password=False)strStyled text input (avoids shadowing built-in input)

Pattern Mapping

Old PatternNew Pattern
print("Title") + print("=====")header("Title", "Subtitle")
print("✓ done")success("done")
print("Error: ...")error("...")
print("Warning: ...")warn("...")
print(" - Label: value")list_item("Label", "value")
input("Continue? (y/n) ")confirm("Continue?")
print("Section...") between blockssection("Section")
print("Note: ...")note("...")
Multi-line error blockerror_block("line1", "line2")
Numbered menu with input()choose("Pick one:", "A", "B", "C")
input("Name: ")styled_input("Name:")
getpass.getpass("Password: ")styled_input("Password:", password=True)
for item in items: ... (slow)for item in progress(items, "Processing"): ...
Long function call with no feedbackresult = spin("Installing...", install_pkg, "name")
pprint(obj)pretty(obj)

Environment Variables

VariableDefaultEffect
NO_COLORunsetDisables all color output when set to any value
STYLE_VERBOSE10=quiet (suppresses step/dim), 1=default, 2=verbose

Rules

  • ALWAYS import from style.py — never use Rich directly in scripts
  • NEVER hard-code ANSI codes in scripts — use style.py functions exclusively
  • Use styled_input instead of input for styled prompts to avoid shadowing the built-in
  • confirm returns a bool — use directly in if confirm("Continue?"): ...
  • spin returns the wrapped function's return value — use: result = spin("Building...", build_fn)
  • choose returns the selected option string — use: choice = choose("Pick:", "A", "B")
  • progress wraps an iterable — use: for item in progress(items, "Processing"): ...
  • Do NOT wrap instant operations with spin — only use for functions that take noticeable time
  • Preserve all existing logic — only change output formatting, never behavior
  • Scripts must work identically when Rich is not installed (plain print fallback)