Skip to content

larz-scripter/larzcolor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages