Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sloria
Created January 19, 2019 20:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloria/316d21dba4bf8002407ec8baae51d1b0 to your computer and use it in GitHub Desktop.
Save sloria/316d21dba4bf8002407ec8baae51d1b0 to your computer and use it in GitHub Desktop.
"""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