'Stylish ANSI terminal colors'
Project description
snazzy
Stylish ANSI terminal colors.
Usage
from snazzy import enable_colors, green, wrap
enable_colors()
print("That looks " + green("good") + ", right?")
That looks good, right?
Note two things:
- snazzy is inactive by default, so we have to call
enable_colors()
first. - The function
green(<text>)
wraps the text in ANSI escape sequences to apply green foreground color and reset to the default color afterwards.
The function green(<text>)
is only a convenience shortcut for wrap(<text>, ...)
:
assert green("good") == wrap("good", fg="green")
However wrap()
is more powerful and flexible, since it also allows to set background
color and attributes (bold, italic, underline):
print(wrap("white on blue", "white", bg="blue"))
print(wrap("ERROR:", "yellow", bg="red", bold=True) + " that went wrong.")
Alternative pattern 'context manager':
with Snazzy(fg="green", bg="black"))
print("This is so eighties...")
Alternative pattern 'explicit':
print(ansi("green", bg="black"), end="")
print("This is so eighties...")
print(ansi_reset(), end="")
Enable Colors
Snazzy is disabled by default, because not all terminals and platforms support
ANSI codes, resulting in ugly text.
Also, when output is redirected to log files, we want to suppress those escape
sequences.
Finally, a command line tool that uses snazzy
might want to offer a
command line argument --no-color
to disable colors:
if not args.no_color:
snazzy.enable_colors()
Until explicitly enabled, no escape sequencrs are generated, so the the wrappers behave transparently:
from snazzy import red, enable_colors
assert red("error") == "error"
enable_colors(True, True)
assert red("error") == "\x1b[91merror\x1b[39m"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.