Support IRC colors 16 through 98

This commit is contained in:
jesopo 2019-01-16 11:18:51 +00:00
parent 8e5642f5d7
commit 8f4312d002
2 changed files with 22 additions and 11 deletions

View file

@ -1,15 +1,26 @@
import typing
from . import _consts_256_color
BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
class IRCColor(object):
def __init__(self, irc: int, ansi: int, color_bold: bool):
def __init__(self, irc: int, ansi: int, is_256):
self.irc = irc
self.ansi = ansi
self.color_bold = color_bold
def ansi_background(self):
return self.ansi+10
self.is_256 = is_256
def to_irc(self):
return str(self.irc)
def to_ansi(self, background=False):
if not self.is_256:
code = self.ansi + (10 if background else 0)
return ANSI_FORMAT % code
else:
return ANSI_256_COLOR % (
48 if background else 38,
self.ansi
)
COLOR_NAMES = {}
COLOR_CODES = {}
@ -37,6 +48,9 @@ GREY = _color("grey", 14, 90, False)
LIGHTGREY = _color("light grey", 15, 37, False)
TRANSPARENT = _color("transparent", 99, 39, False)
for irc_code, ansi_code in _consts_256_color.COLORS.items():
_color("256-%d" % irc_code, irc_code, ansi_code, True)
BOLD = "\x02"
ITALIC = "\x1D"
UNDERLINE = "\x1F"
@ -45,6 +59,7 @@ COLOR = "\x03"
RESET = "\x0F"
ANSI_FORMAT = "\033[%sm"
ANSI_256_COLOR = "\033[%d;5;%dm"
ANSI_RESET = "\033[0m"
ANSI_FOREGROUND_RESET = "\033[39m"
ANSI_BACKGROUND_RESET = "\033[49m"

View file

@ -167,7 +167,7 @@ def _format_tokens(s: str) -> typing.List[str]:
if char.isdigit() and len(current_color) < 2:
if current_color:
next_color = int(current_color + char)
can_add = next_color <= 15 or next_color == 99
can_add = next_color <= 99
else:
can_add = True
@ -206,10 +206,7 @@ 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())
return color.to_ansi(not foreground)
def parse_format(s: str) -> str:
has_foreground = False
@ -230,10 +227,9 @@ def parse_format(s: str) -> str:
if foreground:
has_foreground = True
replace += utils.consts.ANSI_FORMAT % foreground
if background:
has_background = True
replace += utils.consts.ANSI_FORMAT % background
replace += foreground or background
else:
if has_foreground:
has_foreground = False