Skip to content

Instantly share code, notes, and snippets.

@sloria sloria/style.py
Created Jan 19, 2019

Embed
What would you like to do?
"""Dead simple ANSI coloring."""
import os
import sys
RED = 31
GREEN = 32
YELLOW = 33
BOLD = 1
RESET_ALL = 0
def style(text, fg, *, bold=False, file=sys.stdout) -> str:
use_color = not os.environ.get("NO_COLOR") and file.isatty()
if use_color:
parts = [
fg and f"\033[{fg}m",
bold and f"\033[{BOLD}m",
text,
f"\033[{RESET_ALL}m",
]
return "".join([e for e in parts if e])
else:
return text
def sprint(text, *args, **kwargs) -> None:
file = kwargs.pop("file", sys.stdout)
return print(style(text, file=file, *args, **kwargs), file=file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.