Terminal text styling with zero dependencies and TTY detection.
pip install philiprehberger-ansi-styleTerminal text styling with zero dependencies and TTY detection.
pip install philiprehberger-ansi-style
from philiprehberger_ansi_style import red, green, blue, bold, underline
print(red("Error: something went wrong"))
print(green("Success!"))
print(bold(blue("Important message")))
from philiprehberger_ansi_style import (
red, green, blue, yellow, cyan, magenta, white, gray,
)
print(red("Red text"))
print(green("Green text"))
print(blue("Blue text"))
print(yellow("Yellow text"))
print(cyan("Cyan text"))
print(magenta("Magenta text"))
print(white("White text"))
print(gray("Gray text"))
from philiprehberger_ansi_style import bold, dim, underline, italic
print(bold("Bold text"))
print(dim("Dim text"))
print(underline("Underlined text"))
print(italic("Italic text"))
from philiprehberger_ansi_style import style
print(style("Alert", fg="red", bold=True))
print(style("Note", fg="cyan", underline=True))
print(style("Highlight", fg="white", bg="blue", bold=True))
from philiprehberger_ansi_style import red, strip_ansi
styled = red("hello")
plain = strip_ansi(styled) # "hello"
from philiprehberger_ansi_style import supports_color, red
text = red("error") if supports_color() else "error"
from philiprehberger_ansi_style import terminal_link
print(terminal_link("Open docs", "https://example.com"))
| Function | Description |
|---|---|
red(text) | Apply red foreground color |
green(text) | Apply green foreground color |
blue(text) | Apply blue foreground color |
yellow(text) | Apply yellow foreground color |
cyan(text) | Apply cyan foreground color |
magenta(text) | Apply magenta foreground color |
white(text) | Apply white foreground color |
gray(text) | Apply gray foreground color |
bold(text) | Apply bold weight |
dim(text) | Apply dim intensity |
underline(text) | Apply underline decoration |
italic(text) | Apply italic style |
style(text, *, fg, bg, bold, dim, underline) | Apply custom combination of colors and styles |
strip_ansi(text) | Remove all ANSI escape codes from text |
supports_color() | Return True if styling will be emitted (TTY + NO_COLOR unset) |
terminal_link(text, url) | Wrap text as an OSC 8 hyperlink for capable terminals |
pip install -e .
python -m pytest tests/ -v
If you find this project useful: