Skip to main content

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

Release files for philiprehberger-ansi-style 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for philiprehberger-ansi-style 0.4.0
File Size Uploaded
philiprehberger_ansi_style-0.4.0.tar.gz 187.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-ansi-style 0.4.0
File Interpreter ABI Platform
philiprehberger_ansi_style-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 193.1 kB

Release files / philiprehberger_ansi_style-0.4.0.tar.gz

Download URL philiprehberger_ansi_style-0.4.0.tar.gz
Size 187.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b93b8e39e1fe04dd34742ce67eb4b255affc2178fba2e95ffa69bcd3589aabf0
BLAKE2b-256 checksum
How to use checksums
49917bc946b5b51ef863ff9bb7a3acb3e01ef71b44378130c23f1304e4f5ec1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_ansi_style-0.4.0-py3-none-any.whl

Download URL philiprehberger_ansi_style-0.4.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0b88098da1549e21392a031713db87c85516cece2960cf49e8fd74b84ca594f
BLAKE2b-256 checksum
How to use checksums
252a5038b9c33ab8aa64fee954a021172b49c0868f2fabbf3dbd8e03f9bf244c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page