Skip to main content

Terminal text styling with zero dependencies and TTY detection.

Project description

philiprehberger-ansi-style

Tests PyPI version Last updated

philiprehberger-ansi-style

Terminal text styling with zero dependencies and TTY detection.

Installation

pip install philiprehberger-ansi-style

Usage

from philiprehberger_ansi_style import red, green, blue, bold, underline

print(red("Error: something went wrong"))
print(green("Success!"))
print(bold(blue("Important message")))

Color functions

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"))

Style functions

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"))

Custom styling

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))

Strip ANSI codes

from philiprehberger_ansi_style import red, strip_ansi

styled = red("hello")
plain = strip_ansi(styled)  # "hello"

Detect color support

from philiprehberger_ansi_style import supports_color, red

text = red("error") if supports_color() else "error"

Clickable terminal links

from philiprehberger_ansi_style import terminal_link

print(terminal_link("Open docs", "https://example.com"))

Force or disable color

from philiprehberger_ansi_style import color_mode, red, set_color_mode

# Force color even when stdout isn't a TTY (e.g. capturing output in tests)
with color_mode("always"):
    print(red("Always red"))

# Disable color for an entire process
set_color_mode("never")

The auto mode also honors the FORCE_COLOR env var to enable styling in CI logs where stdout is not a TTY. NO_COLOR continues to take precedence when both are set.

API

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 (honors mode + TTY + NO_COLOR / FORCE_COLOR)
terminal_link(text, url) Wrap text as an OSC 8 hyperlink for capable terminals
set_color_mode(mode) Force "always", "never", or "auto" color emission globally
color_mode(mode) Context manager that temporarily overrides the color mode

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_ansi_style-0.4.0.tar.gz (187.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_ansi_style-0.4.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_ansi_style-0.4.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_ansi_style-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b93b8e39e1fe04dd34742ce67eb4b255affc2178fba2e95ffa69bcd3589aabf0
MD5 c67a345d8fbd0d2fc22713da92eb5977
BLAKE2b-256 49917bc946b5b51ef863ff9bb7a3acb3e01ef71b44378130c23f1304e4f5ec1b

See more details on using hashes here.

File details

Details for the file philiprehberger_ansi_style-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_ansi_style-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0b88098da1549e21392a031713db87c85516cece2960cf49e8fd74b84ca594f
MD5 5b851b5d173e52c3ceccf6346c3a4b47
BLAKE2b-256 252a5038b9c33ab8aa64fee954a021172b49c0868f2fabbf3dbd8e03f9bf244c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page