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- Respects the environment automatically. Colour is emitted only when stdout
is a TTY,
NO_COLORisn't set, andTERMisn'tdumb— withFORCE_COLORto override. Pipe your program into a file orgrepand the output is clean text, no manualif 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, norich.
pip install larzcolorfrom 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 stringForce behaviour when you need to (tests, --color=always):
from larzcolor import set_enabled
set_enabled(True) # or False, or None for autopython -m unittest discover -s tests -v # 17 tests incl. detection + no-leak guaranteePure-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
MIT © larz-scripter