I really think there's a better way to do this, but I can't seem to find it. Can you please point me in the right direction?
import colorama
import os
from enum import Enum
def clearscreen():
# Clear the screen
os.system('cls' if os.name=='nt' else 'clear')
class Color(Enum):
"""
An Enum for representing escape sequences, that color the terminal, even in
Windows, with colorama.
"""
NATIVE = "033円[m"
BLACK = "033円[0;30m"
BLUE = "033円[0;34m"
GREEN = "033円[0;32m"
CYAN = "033円[0;36m"
RED = "033円[0;31m"
PURPLE = "033円[0;35m"
BROWN = "033円[0;33m"
GRAY = "033円[0;37m"
DARK_GRAY = "033円[1;30m"
LIGHT_BLUE = "033円[1;34m"
LIGHT_GREEN = "033円[1;32m"
LIGHT_CYAN = "033円[1;36m"
LIGHT_RED = "033円[1;31m"
LIGHT_PURPLE = "033円[1;35m"
YELLOW = "033円[1;33m"
WHITE = "033円[1;37m"
class Dialoge():
"""
Basic Dialoge class, with various methods.
"""
def __init__(self):
colorama.init()
def important(self, wrap)
# Print an "I---I"-like line based on the length of the text
print("I", end="")
for dash in range(wrap):
print("-", end="")
print("I", end="")
print()
def tprint(self, message, color, char, rank):
"""
Print Dialoge to the terminal, with a lot more options
"""
total = ""
if rank == 0:
if char == "NO_CHAR":
if color != "DEFAULT":
print(f"{color}{message}{Color.NATIVE.value}")
else:
print(f"{message}")
else:
if color != "DEFAULT":
print(f"{color}[{char}]:{Color.NATIVE.value} {message}")
else:
print(f"[{char}]: {message}")
else:
if char == "NO_CHAR":
if color != "DEFAULT":
if rank == 1:
print(f"{color}! - {message} - !{Color.NATIVE.value}")
elif rank == 2:
total = f"!! -- {message} -- !!"
print(color, end="")
self.important(len(f"!! -- {message} -- !!") - 2)
# When evaluating the length of a string, rest -2 for the "/r/n".
print(total)
self.important(len(f"!! -- {message} -- !!") - 2)
print(Color.NATIVE.value, end="")
elif rank == 3:
clearscreen()
print(color, end="")
total = f"!!! --- {message} --- !!!"
self.important(len(f"!!! --- {message} --- !!!") - 2)
print(total)
self.important(len(f"!!! --- {message} --- !!!") - 2)
print(Color.NATIVE.value, end="")
else:
if rank == 1:
print(f"! - {message} - !")
elif rank == 2:
total = f"!! -- {message} -- !!"
self.important(len(total) - 2)
print(total)
self.important(len(total) - 2)
elif rank == 3:
clearscreen()
total = f"!!! --- {message} --- !!!"
self.important(len(total) - 2)
print(total)
self.important(len(total) - 2)
else:
if color != "DEFAULT":
if rank == 1:
print(f"! - {color}[{char}]:{Color.NATIVE.value} {message} - !")
elif rank == 2:
total = f"!! -- {color}[{char}]:{Color.NATIVE.value} {message} -- !!"
self.important(len(f"!! -- [{char}]: {message} -- !!") - 2)
print(total)
self.important(len(f"!! -- [{char}]: {message} -- !!") - 2)
elif rank == 3:
clearscreen()
total = f"!!! --- {color}[{char}]:{Color.NATIVE.value} {message} --- !!!"
self.important(len(f"!!! --- [{char}]: {message} --- !!!") - 2)
print(total)
self.important(len(f"!!! --- [{char}]: {message} --- !!!") - 2)
else:
if rank == 1:
print(f"! - [{char}]: {message} - !")
elif rank == 2:
total = f"!! -- [{char}]: {message} -- !!"
self.important(len(total) - 2)
print(total)
self.important(len(total) - 2)
elif rank == 3:
clearscreen()
total = f"!!! --- [{char}]: {message} --- !!!"
self.important(len(total) - 2)
print(total)
self.important(len(total) - 2)
1 Answer 1
Your color code is not portable. Instead, try using something like the termcolor
package which handles the complexity of colors for you. It seems like you've imported an initialized colorama
, but aren't using that. That is a fine alternative. Just don't try doing it yourself (your Color
enum)! With termcolor
you can make help functions that abstract away color names:
print_warning = lambda x: cprint(f'warning: {x}', 'yellow')
# Later in your code
print_warning('something went wrong')
Instead of:
print('I', end='')
for dash in range(wrap):
print('-', end='')
print('I', end='')
print()
Why not:
print('I' + ('-' * wrap) + 'I')
Or you could pull it out into a variable to make it a bit cleaner:
divider = '-' * wrap
print(f'I{divider}I')
tprint
is way too confusing for me to follow. Perhaps try breaking it into several methods instead of one method whose behavior changes drastically depending on the parameters provided.