import re from mudrich.text import Text from ..patches.style import MudStyle, ProtoStyle from mudrich.color import Color from typing import Union, List, Tuple LETTERS = { "x": Color.from_ansi(0), "r": Color.from_ansi(1), "g": Color.from_ansi(2), "y": Color.from_ansi(3), "b": Color.from_ansi(4), "m": Color.from_ansi(5), "c": Color.from_ansi(6), "w": Color.from_ansi(7), } EV_REGEX = { "fg_ansi_bold": re.compile(r"^(r|g|y|b|m|c|x|w)"), "fg_ansi_normal": re.compile(r"^(R|G|Y|B|M|C|X|W)"), "bg_ansi_bold": re.compile(r"^\[(r|g|y|b|m|c|x|w)"), "bg_ansi_normal": re.compile(r"^\[(R|G|Y|B|M|C|X|W)"), "fg_xterm": re.compile(r"^[0-5]{3}"), "bg_xterm": re.compile(r"^\[([0-5]{3})"), } def apply_fg_ansi_bold(proto: ProtoStyle, code): proto.bold = True
def apply_color_rule(mark: ProtoStyle, rule_tuple): mode, g, data, original = rule_tuple if mode == "letters": for c in data: if c == "n": # ANSI reset mark.do_reset() continue if (bit := CHAR_MAP.get(c, None)): setattr(mark, bit, True) elif (bit := CHAR_MAP.get(c.lower(), None)): setattr(mark, bit, False) elif (code := BASE_COLOR_MAP.get(c, None)): setattr(mark, "color", Color.from_ansi(code)) elif (code := BASE_COLOR_MAP.get(c.lower(), None)): setattr(mark, "bgcolor", Color.from_ansi(code)) else: pass # I dunno what we got passed, but it ain't relevant. elif g == BgMode.FG: if mode == "numbers": setattr(mark, "color", Color.from_ansi(data)) elif mode == "name": if (found := COLORS.get(data)): setattr(mark, "color", Color.from_ansi(found["xterm"])) elif mode in ("rgb", "hex1", "hex2"): setattr(mark, "color", Color.from_rgb(data["red"], data["green"], data["blue"])) elif g == BgMode.BG:
def test_from_ansi() -> None: assert Color.from_ansi(1) == Color("color(1)", ColorType.STANDARD, 1)