'ANSI_RESET' -> 'ANSI_BOLD_RESET' typo, 'ansi_bold' -> 'color_bold'

clarification, fix issue in color tokenizing that caused bolds to be put in the
wrong place
This commit is contained in:
jesopo 2018-11-14 11:44:56 +00:00
parent bcdbb3640c
commit 2fcde64bfd
2 changed files with 29 additions and 26 deletions

View file

@ -4,15 +4,15 @@ BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
BITBOT_EXPORTS_MAGIC = "__bitbot_exports" BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
class IRCColor(object): class IRCColor(object):
def __init__(self, irc: int, ansi: int, ansi_bold: bool): def __init__(self, irc: int, ansi: int, color_bold: bool):
self.irc = irc self.irc = irc
self.ansi = ansi self.ansi = ansi
self.ansi_bold = ansi_bold self.color_bold = color_bold
COLOR_NAMES = {} COLOR_NAMES = {}
COLOR_CODES = {} COLOR_CODES = {}
def _color(name: str, irc: int, ansi: int, ansi_bold: bool): def _color(name: str, irc: int, ansi: int, color_bold: bool):
color = IRCColor(irc, ansi, ansi_bold) color = IRCColor(irc, ansi, color_bold)
COLOR_NAMES[name] = color COLOR_NAMES[name] = color
COLOR_CODES[irc] = color COLOR_CODES[irc] = color
return color return color

View file

@ -151,6 +151,22 @@ def _color_tokens(s: str) -> typing.List[str]:
matches = [] # type: typing.List[str] matches = [] # type: typing.List[str]
for char in s: for char in s:
if is_color:
if char.isdigit():
if background:
background += char
else:
foreground += char
continue
elif char == ",":
background += char
continue
else:
matches.append("\x03%s%s" % (foreground, background))
is_color = False
foreground = ""
background = ""
if char == utils.consts.COLOR: if char == utils.consts.COLOR:
if is_color: if is_color:
matches.append(char) matches.append(char)
@ -158,24 +174,11 @@ def _color_tokens(s: str) -> typing.List[str]:
is_color = True is_color = True
elif char == utils.consts.BOLD: elif char == utils.consts.BOLD:
matches.append(char) matches.append(char)
elif is_color:
if char.isdigit():
if background:
background += char
else:
foreground += char
elif char == ",":
background += char
else:
matches.append("\x03%s%s" % (foreground, background))
is_color = False
foreground = ""
background = ""
return matches return matches
def to_ansi_colors(s: str) -> str: def to_ansi_colors(s: str) -> str:
color = False color = False
ansi_bold = False color_bold = False
bold = False bold = False
for token in _color_tokens(s): for token in _color_tokens(s):
@ -189,25 +192,25 @@ def to_ansi_colors(s: str) -> str:
code = int(foreground_match.lstrip("0") or "0") code = int(foreground_match.lstrip("0") or "0")
foreground = utils.consts.COLOR_CODES[code] foreground = utils.consts.COLOR_CODES[code]
if ansi_bold and not foreground.ansi_bold and not bold: if color_bold and not foreground.color_bold and not bold:
ansi_bold = False color_bold = False
replace += utils.consts.ANSI_RESET replace += utils.consts.ANSI_BOLD_RESET
color = True color = True
replace += utils.consts.ANSI_FORMAT % foreground.ansi replace += utils.consts.ANSI_FORMAT % foreground.ansi
if foreground.ansi_bold: if foreground.color_bold:
ansi_bold = True color_bold = True
replace += utils.consts.ANSI_BOLD replace += utils.consts.ANSI_BOLD
else: else:
if color: if color:
replace += utils.consts.ANSI_COLOR_RESET replace += utils.consts.ANSI_COLOR_RESET
if ansi_bold and not bold: if color_bold and not bold:
replace += utils.consts.ANSI_BOLD_RESET replace += utils.consts.ANSI_BOLD_RESET
color = False color = False
ansi_bold = False color_bold = False
elif type == utils.consts.BOLD: elif type == utils.consts.BOLD:
if bold: if bold:
if not ansi_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