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
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
def _ansi_partial_color_format_main(template, style='default', cmap=None, hide=False): if cmap is not None: pass elif style in ANSI_STYLES: cmap = ANSI_STYLES[style] else: try: # dynamically loading the style cmap = ansi_style_by_name(style) except Exception: msg = 'Could not find color style {0!r}, using default.' print(msg.format(style), file=sys.stderr) builtins.__xonsh_env__['XONSH_COLOR_STYLE'] = 'default' cmap = ANSI_STYLES['default'] formatter = string.Formatter() esc = ('\001' if hide else '') + '\033[' m = 'm' + ('\002' if hide else '') bopen = '{' bclose = '}' colon = ':' expl = '!' toks = [] for literal, field, spec, conv in formatter.parse(template): toks.append(literal) if field is None: pass elif field in cmap: toks.extend([esc, cmap[field], m]) elif '#' in field: field = field.lower() pre, _, post = field.partition('#') f_or_b = '38' if RE_BACKGROUND.search(pre) is None else '48' rgb, _, post = post.partition('_') c256, _ = rgb_to_256(rgb) color = f_or_b + ';5;' + c256 mods = pre + '_' + post if 'underline' in mods: color = '4;' + color if 'bold' in mods: color = '1;' + color toks.extend([esc, color, m]) elif field is not None: toks.append(bopen) toks.append(field) if conv is not None and len(conv) > 0: toks.append(expl) toks.append(conv) if spec is not None and len(spec) > 0: toks.append(colon) toks.append(spec) toks.append(bclose) return ''.join(toks)
def _ansi_partial_color_format_main(template, style="default", cmap=None, hide=False): if cmap is not None: pass elif style in ANSI_STYLES: cmap = ANSI_STYLES[style] else: try: # dynamically loading the style cmap = ansi_style_by_name(style) except Exception: msg = "Could not find color style {0!r}, using default." print(msg.format(style), file=sys.stderr) builtins.__xonsh_env__["XONSH_COLOR_STYLE"] = "default" cmap = ANSI_STYLES["default"] formatter = string.Formatter() esc = ("\001" if hide else "") + "\033[" m = "m" + ("\002" if hide else "") bopen = "{" bclose = "}" colon = ":" expl = "!" toks = [] for literal, field, spec, conv in formatter.parse(template): toks.append(literal) if field is None: pass elif field in cmap: toks.extend([esc, cmap[field], m]) elif "#" in field: field = field.lower() pre, _, post = field.partition("#") f_or_b = "38" if RE_BACKGROUND.search(pre) is None else "48" rgb, _, post = post.partition("_") c256, _ = rgb_to_256(rgb) color = f_or_b + ";5;" + c256 mods = pre + "_" + post if "underline" in mods: color = "4;" + color if "bold" in mods: color = "1;" + color toks.extend([esc, color, m]) elif field is not None: toks.append(bopen) toks.append(field) if conv is not None and len(conv) > 0: toks.append(expl) toks.append(conv) if spec is not None and len(spec) > 0: toks.append(colon) toks.append(spec) toks.append(bclose) return "".join(toks)
def _ansi_partial_color_format_main(template, style="default", cmap=None, hide=False): if cmap is not None: pass elif style in ANSI_STYLES: cmap = ANSI_STYLES[style] else: try: # dynamically loading the style cmap = ansi_style_by_name(style) except Exception: msg = "Could not find color style {0!r}, using default." print(msg.format(style), file=sys.stderr) builtins.__xonsh__.env["XONSH_COLOR_STYLE"] = "default" cmap = ANSI_STYLES["default"] formatter = string.Formatter() esc = ("\001" if hide else "") + "\033[" m = "m" + ("\002" if hide else "") bopen = "{" bclose = "}" colon = ":" expl = "!" toks = [] for literal, field, spec, conv in formatter.parse(template): toks.append(literal) if field is None: pass elif field in cmap: toks.extend([esc, cmap[field], m]) elif "#" in field: field = field.lower() pre, _, post = field.partition("#") f_or_b = "38" if RE_BACKGROUND.search(pre) is None else "48" rgb, _, post = post.partition("_") c256, _ = rgb_to_256(rgb) color = f_or_b + ";5;" + c256 mods = pre + "_" + post if "underline" in mods: color = "4;" + color if "bold" in mods: color = "1;" + color toks.extend([esc, color, m]) elif field is not None: toks.append(bopen) toks.append(field) if conv is not None and len(conv) > 0: toks.append(expl) toks.append(conv) if spec is not None and len(spec) > 0: toks.append(colon) toks.append(spec) toks.append(bclose) return "".join(toks)