Skip to main content

Terminal text styling with zero dependencies and TTY detection.

Project description

philiprehberger-ansi-style

Tests PyPI version Last updated

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

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 (TTY + NO_COLOR unset)
terminal_link(text, url) Wrap text as an OSC 8 hyperlink for capable terminals

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.3.0.tar.gz (6.7 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.3.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_ansi_style-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4446f6fd07739b3446d1e03c8f4d55f778e87f77435c6ff7369636bb6aa29d0e
MD5 609a3c52313e94bb235c489fe300bfb8
BLAKE2b-256 74a48817c8c61c19b803b9501cd4bdd40af2fe31ba5e6cf70a1a486fc6f2d24f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_ansi_style-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 af0fe528a1e1680dae61162e4e67894c9beef7dbfbca5c467bf723d376774383
MD5 7ef391f594c9a19d45f48e62f6551b30
BLAKE2b-256 db917c01fc26ba63f438615aa39388375a664a545cfe96b2f8fbcb435ee5acd0

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