Skip to main content

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

Metadata

Release files for larzcolor 0.1.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 larzcolor 0.1.0
File Size Uploaded
larzcolor-0.1.0.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for larzcolor 0.1.0
File Interpreter ABI Platform
larzcolor-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / larzcolor-0.1.0.tar.gz

Download URL larzcolor-0.1.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9fd6471734d8070aaf0e5a2f780b6d81c56421de22651fa0d46010fbed625083
BLAKE2b-256 checksum
How to use checksums
3df57103fcfbe9ab547f99c5abcd8fe4ed0ab35219a530f4e9cb0f7ecff59ab5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / larzcolor-0.1.0-py3-none-any.whl

Download URL larzcolor-0.1.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
daf2b166424922543c3af90510f93d97ef5c6f22e26210ab9cbaf4fe8db56960
BLAKE2b-256 checksum
How to use checksums
0e2ca583e612c95a39d3d81042d476e679fdf18e4be4cf23b2fccf97016b9d3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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