Created
January 19, 2019 20:10
-
-
Save sloria/316d21dba4bf8002407ec8baae51d1b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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