'to_ansi_colors(' -> 'parse_format(' as it's become a lot more than just colors.

strip \x08 (in case hexchat users paste it) (print_activity.py, src.utils.irc)
This commit is contained in:
jesopo 2019-01-13 21:56:36 +00:00
parent 97b7249989
commit 598fcb80b9
2 changed files with 12 additions and 4 deletions

View file

@ -9,7 +9,8 @@ class Module(ModuleManager.BaseModule):
target = str(event["server"]) target = str(event["server"])
if not channel == None: if not channel == None:
target += channel target += channel
self.bot.log.info("%s | %s", [target, utils.irc.to_ansi_colors(line)]) formatted_line = utils.irc.parse_format(line)
self.bot.log.info("%s | %s", [target, formatted_line])
def _mode_symbols(self, user, channel, server): def _mode_symbols(self, user, channel, server):
modes = channel.get_user_status(user) modes = channel.get_user_status(user)

View file

@ -150,7 +150,10 @@ FORMAT_TOKENS = [
utils.consts.RESET, utils.consts.RESET,
utils.consts.UNDERLINE utils.consts.UNDERLINE
] ]
def _color_tokens(s: str) -> typing.List[str]: FORMAT_STRIP = [
"\x08" # backspace
]
def _format_tokens(s: str) -> typing.List[str]:
is_color = False is_color = False
foreground = "" foreground = ""
background = "" background = ""
@ -195,6 +198,8 @@ def _color_tokens(s: str) -> typing.List[str]:
is_color = True is_color = True
elif char in FORMAT_TOKENS: elif char in FORMAT_TOKENS:
matches.append(char) matches.append(char)
elif char in FORMAT_STRIP:
matches.append(char)
return matches return matches
def _color_match(code: typing.Optional[str], foreground: bool) -> str: def _color_match(code: typing.Optional[str], foreground: bool) -> str:
@ -206,13 +211,13 @@ def _color_match(code: typing.Optional[str], foreground: bool) -> str:
else: else:
return str(color.ansi_background()) return str(color.ansi_background())
def to_ansi_colors(s: str) -> str: def parse_format(s: str) -> str:
has_foreground = False has_foreground = False
has_background = False has_background = False
bold = False bold = False
underline = False underline = False
for token in _color_tokens(s): for token in _format_tokens(s):
replace = "" replace = ""
type = token[0] type = token[0]
@ -250,6 +255,8 @@ def to_ansi_colors(s: str) -> str:
else: else:
replace += utils.consts.ANSI_UNDERLINE replace += utils.consts.ANSI_UNDERLINE
underline = not underline underline = not underline
elif type in FORMAT_STRIP:
replace = ""
s = s.replace(token, replace, 1) s = s.replace(token, replace, 1)