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)
def color_by_name(name, fg=None, bg=None): """Converts a color name to a color token, foreground name, and background name. Will take into consideration current foreground and background colors, if provided. Parameters ---------- name : str Color name. fg : str, optional Foreground color name. bg : str, optional Background color name. Returns ------- tok : Token Pygments Token.Color subclass fg : str or None New computed foreground color name. bg : str or None New computed background color name. """ name = name.upper() if name in ("RESET", "NO_COLOR"): if name == "NO_COLOR": warn_deprecated_no_color() return Color.RESET, None, None m = RE_BACKGROUND.search(name) if m is None: # must be foreground color fg = norm_name(name) else: bg = norm_name(name) # assemble token if fg is None and bg is None: tokname = "RESET" elif fg is None: tokname = bg elif bg is None: tokname = fg else: tokname = fg + "__" + bg tok = getattr(Color, tokname) return tok, fg, bg
def color_by_name(name, fg=None, bg=None): """Converts a color name to a color token, foreground name, and background name. Will take into consideration current foreground and background colors, if provided. Parameters ---------- name : str Color name. fg : str, optional Foreground color name. bg : str, optional Background color name. Returns ------- tok : Token Pygments Token.Color subclass fg : str or None New computed foreground color name. bg : str or None New computed background color name. """ name = name.upper() if name == 'NO_COLOR': return Color.NO_COLOR, None, None m = RE_BACKGROUND.search(name) if m is None: # must be foreground color fg = norm_name(name) else: bg = norm_name(name) # assemble token if fg is None and bg is None: tokname = 'NO_COLOR' elif fg is None: tokname = bg elif bg is None: tokname = fg else: tokname = fg + '__' + bg tok = getattr(Color, tokname) return tok, fg, bg