我真的认为有更好的方法来做这件事,但我似乎找不到。请你指出正确的方向好吗?
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)发布于 2018-10-29 18:46:35
您的颜色代码是不可移植的。相反,尝试使用类似于termcolor套餐的东西,它为您处理颜色的复杂性。似乎您已经导入了一个初始化的colorama,但没有使用它。这是一个很好的选择。只是不要尝试自己做(你的Color枚举)!使用termcolor,您可以使用帮助函数抽象出颜色名称:
print_warning = lambda x: cprint(f'warning: {x}', 'yellow')
# Later in your code
print_warning('something went wrong')而不是:
print('I', end='')
for dash in range(wrap):
print('-', end='')
print('I', end='')
print()为什么不:
print('I' + ('-' * wrap) + 'I')或者你可以把它拉到一个变量中,让它变得更干净:
divider = '-' * wrap
print(f'I{divider}I')tprint太让我迷惑了,我跟不上。也许可以尝试将其分解为几个方法,而不是一个根据所提供的参数其行为发生剧烈变化的方法。
https://codereview.stackexchange.com/questions/206517
复制相似问题