Skip to main content

Terminal color and style that auto-disables on non-TTY/NO_COLOR, so escape codes never leak into pipes or logs. Pure Python, zero dependencies.

Project description

larzcolor

Terminal colour that knows when to stay quiet. Pure Python, zero dependencies.

Colour and style for the terminal, with the thing most helpers get wrong handled for you: it auto-disables when the output isn't a terminal (or NO_COLOR is set, or TERM=dumb) — so your logs and piped output never fill up with \x1b[31m escape-code garbage.

from larzcolor import red, green, bold, style

print(red("error"))            # coloured in a terminal, plain when piped to a file
print(bold(green("ok")))       # styles nest correctly
print(style("hi", fg="cyan", bg="white", underline=True))
print(style("warn", fg=(255, 165, 0)))   # truecolor RGB

What makes it different

  • Respects the environment automatically. Colour is emitted only when stdout is a TTY, NO_COLOR isn't set, and TERM isn't dumb — with FORCE_COLOR to override. Pipe your program into a file or grep and the output is clean text, no manual if sys.stdout.isatty() dance.
  • Nesting just works. bold(red(x)) re-applies the outer style after the inner reset, so combinations don't fall apart.
  • All the colour depths. Basic + bright named colours, 256-colour (fg=39), and truecolor (fg=(r, g, b)), foreground and background.
  • Zero dependencies. No colorama, no rich.

Install

pip install larzcolor

Usage

from larzcolor import style, red, green, yellow, bold, underline, strip, cprint

red("err"); green("ok"); yellow("warn")
bold("strong"); underline("link")
style("text", fg="magenta", bg="black", bold=True, italic=True)
style("x", fg=196)              # 256-colour
style("x", fg=(0, 200, 120))    # truecolor

cprint("styled print", fg="cyan", bold=True)
strip("\x1b[31mred\x1b[0m")     # 'red'  — remove ANSI from any string

Force behaviour when you need to (tests, --color=always):

from larzcolor import set_enabled
set_enabled(True)    # or False, or None for auto

Tests

python -m unittest discover -s tests -v   # 17 tests incl. detection + no-leak guarantee

The Larz stack

Pure-Python, zero-dependency building blocks: larz · larzchain · larzmoney · larzcrypt · larzdb · larzagent · larzchart · larzmark · larztask · larzvault · larzvm · larzcache · larzvalidate · larzid · larzrpc · larzstate · larzhttp · larzconf · larzcron · larzlimit · larzlog · larzcli · larzretry · larztime · larzpdf · larzpack · larztemplate · larzcolor

License

MIT © larz-scripter

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

larzcolor-0.1.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.

larzcolor-0.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file larzcolor-0.1.0.tar.gz.

File metadata

  • Download URL: larzcolor-0.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzcolor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9fd6471734d8070aaf0e5a2f780b6d81c56421de22651fa0d46010fbed625083
MD5 e4cc88054f59292d9ac915fb0738adaf
BLAKE2b-256 3df57103fcfbe9ab547f99c5abcd8fe4ed0ab35219a530f4e9cb0f7ecff59ab5

See more details on using hashes here.

File details

Details for the file larzcolor-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: larzcolor-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzcolor-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 daf2b166424922543c3af90510f93d97ef5c6f22e26210ab9cbaf4fe8db56960
MD5 bf770aa67dba8b4d313484897d1642f4
BLAKE2b-256 0e2ca583e612c95a39d3d81042d476e679fdf18e4be4cf23b2fccf97016b9d3e

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