Created
September 14, 2019 12:08
-
-
Save mtalimanchuk/688e3e81a0ff23917299eef357167e15 to your computer and use it in GitHub Desktop.
Class representing console colors
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
class colors: | |
BLACK = "\u001b[30m" | |
PALE_RED = "\u001b[31m" | |
PALE_GREEN = "\u001b[32m" | |
PALE_YELLOW = "\u001b[33m" | |
PALE_BLUE = "\u001b[34m" | |
PALE_MAGENTA = "\u001b[35m" | |
PALE_CYAN = "\u001b[36m" | |
GRAY = "\u001b[90m" | |
RED = "\u001b[91m" | |
GREEN = "\u001b[92m" | |
YELLOW = "\u001b[93m" | |
BLUE = "\u001b[94m" | |
MAGENTA = "\u001b[95m" | |
CYAN = "\u001b[96m" | |
WHITE = "\u001b[97m" | |
BG_GRAY = "\u001b[100m" | |
BG_RED = "\u001b[41m" | |
BG_GREEN = "\u001b[42m" | |
BG_YELLOW = "\u001b[43m" | |
BG_BLUE = "\u001b[44m" | |
BG_MAGENTA = "\u001b[45m" | |
BG_CYAN = "\u001b[46m" | |
BG_WHITE = "\u001b[47m" | |
BOLD = "\u001b[1m" | |
RESET = "\u001b[0m" |
totekuh
commented
Sep 14, 2019
•
@derstolz nice example, thanks. Although putting colors into an f-string directly looks more flexible:
f"{colors.RED}{colors.BG_GRAY} Red text on gray bg {colors.BG_CYAN} Still red text but on cyan bg {colors.RESET}"
@mtalimanchuk
I don't think so, because it's not portable to older version of Python
@derstolz
"{0}{1} Red text on gray bg {2} Still red text but on cyan bg {3}".format(colors.RED, colors.BG_GRAY, colors.BG_CYAN, colors.RESET)
Anyway static methods are good for wrapping your custom tasks, e.g.:
@staticmethod
def colorize_article(title, text, comments):
title = f"{colors.BG_GRAY}{colors.YELLOW}{title}{colors.RESET}"
text = f"{colors.BOLD}{text}{colors.RESET}"
comments = f"{colors.BG_YELLOW}{comments}{colors.RESET}"
return '\n'.join([title, text, comments])
@mtalimanchuk you don't have to define positional arguments with .format() method:
@derstolz I know, I used them for readability
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment