Stop supporting using ansi bold for brightening colors, support ansi background

colors
This commit is contained in:
jesopo 2018-11-14 22:07:34 +00:00
parent 957b881dbd
commit 19b195eb1d
2 changed files with 31 additions and 24 deletions

View file

@ -8,6 +8,8 @@ class IRCColor(object):
self.irc = irc self.irc = irc
self.ansi = ansi self.ansi = ansi
self.color_bold = color_bold self.color_bold = color_bold
def ansi_background(self):
return self.ansi+10
COLOR_NAMES = {} COLOR_NAMES = {}
COLOR_CODES = {} COLOR_CODES = {}
@ -43,6 +45,7 @@ RESET = "\x0F"
ANSI_FORMAT = "\033[%sm" ANSI_FORMAT = "\033[%sm"
ANSI_RESET = "\033[0m" ANSI_RESET = "\033[0m"
ANSI_COLOR_RESET = "\033[39m" ANSI_FOREGROUND_RESET = "\033[39m"
ANSI_BACKGROUND_RESET = "\033[49m"
ANSI_BOLD = "\033[1m" ANSI_BOLD = "\033[1m"
ANSI_BOLD_RESET = "\033[22m" ANSI_BOLD_RESET = "\033[22m"

View file

@ -180,9 +180,18 @@ def _color_tokens(s: str) -> typing.List[str]:
matches.append(char) matches.append(char)
return matches return matches
def _color_match(code: typing.Optional[str], foreground: bool) -> str:
if not code:
return ""
color = utils.consts.COLOR_CODES[int(code)]
if foreground:
return str(color.ansi)
else:
return str(color.ansi_background())
def to_ansi_colors(s: str) -> str: def to_ansi_colors(s: str) -> str:
color = False has_foreground = False
color_bold = False has_background = False
bold = False bold = False
for token in _color_tokens(s): for token in _color_tokens(s):
@ -191,31 +200,26 @@ def to_ansi_colors(s: str) -> str:
if type == utils.consts.COLOR: if type == utils.consts.COLOR:
match = REGEX_COLOR.match(token) match = REGEX_COLOR.match(token)
foreground_match = match.group(1)
if foreground_match:
code = int(foreground_match.lstrip("0") or "0")
foreground = utils.consts.COLOR_CODES[code]
if color_bold and not foreground.color_bold and not bold: if match and (match.group(1) or match.group(2)):
color_bold = False foreground = _color_match(match.group(1), True)
replace += utils.consts.ANSI_BOLD_RESET background = _color_match(match.group(2), False)
color = True if foreground:
foreground_s = str(foreground.ansi).zfill(2) has_foreground = True
if foreground.color_bold: replace += utils.consts.ANSI_FORMAT % foreground
color_bold = True if background:
foreground_s = "%s;1" % foreground_s has_background = True
replace += utils.consts.ANSI_FORMAT % foreground_s replace += utils.consts.ANSI_FORMAT % background
else: else:
if color: if has_foreground:
replace += utils.consts.ANSI_COLOR_RESET replace += utils.consts.ANSI_FOREGROUND_RESET
if color_bold and not bold: if has_background:
replace += utils.consts.ANSI_BOLD_RESET replace += utils.consts.ANSI_BACKGROUND_RESET
color = False has_foreground = False
color_bold = False has_background = False
elif type == utils.consts.BOLD: elif type == utils.consts.BOLD:
if bold: if bold:
if not color_bold:
replace += utils.consts.ANSI_BOLD_RESET replace += utils.consts.ANSI_BOLD_RESET
else: else:
replace += utils.consts.ANSI_BOLD replace += utils.consts.ANSI_BOLD