def colored(msg, color=None, background=None, style=None, force=False):
"""
Return the colored version of a string *msg*. *color*'s: red, green, yellow, blue, pink,
cyan, white. *background*'s: none, red, green, yellow, blue, pink, cyan, grey. *style*'s: none,
bright, underline. Unless *force* is *True*, the *msg* string is returned unchanged in case the
output is not a tty.
"""
try:
if not force and not os.isatty(sys.stdout.fileno()):
return msg
except:
return msg
color = colors.get(color, colors["white"])
background = backgrounds.get(background, backgrounds["none"])
if not isinstance(style, (tuple, list, set)):
style = (style,)
style = ";".join(str(styles.get(s, styles["none"])) for s in style)
return "\033[{};{};{}m{}\033[0m".format(style, background, color, msg)
评论列表
文章目录