Пример #1
0
def ansi_color_name_to_escape_code(name, style="default", cmap=None):
    """Converts a color name to the inner part of an ANSI escape code"""
    cmap = _ensure_color_map(style=style, cmap=cmap)
    if name in cmap:
        return cmap[name]
    m = RE_XONSH_COLOR.match(name)
    if m is None:
        raise ValueError("{!r} is not a color!".format(name))
    parts = m.groupdict()
    # convert regex match into actual ANSI colors
    if parts["reset"] is not None:
        if parts["reset"] == "NO_COLOR":
            warn_deprecated_no_color()
        res = "0"
    elif parts["bghex"] is not None:
        res = "48;5;" + rgb_to_256(parts["bghex"][3:])[0]
    elif parts["background"] is not None:
        color = parts["color"]
        if "#" in color:
            res = "48;5;" + rgb_to_256(color[1:])[0]
        else:
            fgcolor = cmap[color]
            if fgcolor.isdecimal():
                res = str(int(fgcolor) + 10)
            elif fgcolor.startswith("38;"):
                res = "4" + fgcolor[1:]
            elif fgcolor == "DEFAULT":
                res = "39"
            else:
                msg = (
                    "when converting {!r}, did not recognize {!r} within "
                    "the following color map as a valid color:\n\n{!r}"
                )
                raise ValueError(msg.format(name, fgcolor, cmap))
    else:
        # have regular, non-background color
        mods = parts["modifiers"]
        if mods is None:
            mods = []
        else:
            mods = mods.strip("_").split("_")
            mods = [ANSI_ESCAPE_MODIFIERS[mod] for mod in mods]
        color = parts["color"]
        if "#" in color:
            mods.append("38;5;" + rgb_to_256(color[1:])[0])
        elif color == "DEFAULT":
            res = "39"
        else:
            mods.append(cmap[color])
        res = ";".join(mods)
    cmap[name] = res
    return res
Пример #2
0
def ansi_color_name_to_escape_code(name, style="default", cmap=None):
    """Converts a color name to the inner part of an ANSI escape code"""
    cmap = _ensure_color_map(style=style, cmap=cmap)
    if name in cmap:
        return cmap[name]
    m = RE_XONSH_COLOR.match(name)
    if m is None:
        raise ValueError("{!r} is not a color!".format(name))
    parts = m.groupdict()
    # convert regex match into actual ANSI colors
    if parts["nocolor"] is not None:
        res = "0"
    elif parts["bghex"] is not None:
        res = "48;5;" + rgb_to_256(parts["bghex"][3:])[0]
    elif parts["background"] is not None:
        color = parts["color"]
        if "#" in color:
            res = "48;5;" + rgb_to_256(color[1:])[0]
        else:
            fgcolor = cmap[color]
            if fgcolor.isdecimal():
                res = str(int(fgcolor) + 10)
            elif fgcolor.startswith("38;"):
                res = "4" + fgcolor[1:]
            else:
                msg = (
                    "when converting {!r}, did not recognize {!r} within "
                    "the following color map as a valid color:\n\n{!r}"
                )
                raise ValueError(msg.format(name, fgcolor, cmap))
    else:
        # have regular, non-background color
        mods = parts["modifiers"]
        if mods is None:
            mods = []
        else:
            mods = mods.strip("_").split("_")
            mods = [ANSI_ESCAPE_MODIFIERS[mod] for mod in mods]
        color = parts["color"]
        if "#" in color:
            mods.append("38;5;" + rgb_to_256(color[1:])[0])
        else:
            mods.append(cmap[color])
        res = ";".join(mods)
    cmap[name] = res
    return res
Пример #3
0
def color_name_to_pygments_code(name, styles):
    """Converts a xonsh color name to a pygments color code."""
    token = getattr(Color, norm_name(name))
    if token in styles:
        return styles[token]
    m = RE_XONSH_COLOR.match(name)
    if m is None:
        raise ValueError("{!r} is not a color!".format(name))
    parts = m.groupdict()
    # convert regex match into actual pygments colors
    if parts["reset"] is not None:
        if parts["reset"] == "NO_COLOR":
            warn_deprecated_no_color()
        res = "noinherit"
    elif parts["bghex"] is not None:
        res = "bg:#" + parts["bghex"][3:]
    elif parts["background"] is not None:
        color = parts["color"]
        if "#" in color:
            fgcolor = color
        else:
            fgcolor = styles[getattr(Color, color)]
        res = "bg:" + fgcolor
    else:
        # have regular, non-background color
        mods = parts["modifiers"]
        if mods is None:
            mods = []
        else:
            mods = mods.strip("_").split("_")
            mods = [PYGMENTS_MODIFIERS[mod] for mod in mods]
        mods = list(filter(None, mods))  # remove unsupported entries
        color = parts["color"]
        if "#" in color:
            mods.append(color)
        else:
            mods.append(styles[getattr(Color, color)])
        res = " ".join(mods)
    styles[token] = res
    return res
Пример #4
0
def color_name_to_pygments_code(name, styles):
    """Converts a xonsh color name to a pygments color code."""
    token = getattr(Color, norm_name(name))
    if token in styles:
        return styles[token]
    m = RE_XONSH_COLOR.match(name)
    if m is None:
        raise ValueError("{!r} is not a color!".format(name))
    parts = m.groupdict()
    # convert regex match into actual pygments colors
    if parts["nocolor"] is not None:
        res = "noinherit"
    elif parts["bghex"] is not None:
        res = "bg:#" + parts["bghex"][3:]
    elif parts["background"] is not None:
        color = parts["color"]
        if "#" in color:
            fgcolor = color
        else:
            fgcolor = styles[getattr(Color, color)]
        res = "bg:" + fgcolor
    else:
        # have regular, non-background color
        mods = parts["modifiers"]
        if mods is None:
            mods = []
        else:
            mods = mods.strip("_").split("_")
            mods = [PYGMENTS_MODIFIERS[mod] for mod in mods]
        mods = list(filter(None, mods))  # remove unsupported entries
        color = parts["color"]
        if "#" in color:
            mods.append(color)
        else:
            mods.append(styles[getattr(Color, color)])
        res = " ".join(mods)
    styles[token] = res
    return res