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.ansi = ansi
self.color_bold = color_bold
def ansi_background(self):
return self.ansi+10
COLOR_NAMES = {}
COLOR_CODES = {}
@ -43,6 +45,7 @@ RESET = "\x0F"
ANSI_FORMAT = "\033[%sm"
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_RESET = "\033[22m"

View file

@ -180,9 +180,18 @@ def _color_tokens(s: str) -> typing.List[str]:
matches.append(char)
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:
color = False
color_bold = False
has_foreground = False
has_background = False
bold = False
for token in _color_tokens(s):
@ -191,32 +200,27 @@ def to_ansi_colors(s: str) -> str:
if type == utils.consts.COLOR:
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:
color_bold = False
replace += utils.consts.ANSI_BOLD_RESET
if match and (match.group(1) or match.group(2)):
foreground = _color_match(match.group(1), True)
background = _color_match(match.group(2), False)
color = True
foreground_s = str(foreground.ansi).zfill(2)
if foreground.color_bold:
color_bold = True
foreground_s = "%s;1" % foreground_s
replace += utils.consts.ANSI_FORMAT % foreground_s
if foreground:
has_foreground = True
replace += utils.consts.ANSI_FORMAT % foreground
if background:
has_background = True
replace += utils.consts.ANSI_FORMAT % background
else:
if color:
replace += utils.consts.ANSI_COLOR_RESET
if color_bold and not bold:
replace += utils.consts.ANSI_BOLD_RESET
color = False
color_bold = False
if has_foreground:
replace += utils.consts.ANSI_FOREGROUND_RESET
if has_background:
replace += utils.consts.ANSI_BACKGROUND_RESET
has_foreground = False
has_background = False
elif type == utils.consts.BOLD:
if bold:
if not color_bold:
replace += utils.consts.ANSI_BOLD_RESET
replace += utils.consts.ANSI_BOLD_RESET
else:
replace += utils.consts.ANSI_BOLD
bold = not bold