def XonshTerminal256Formatter(): if (ptk_version_info() and ptk_version_info() > (2, 0) and pygments_version_info() and (2, 2, 0) <= pygments_version_info() < (2, 4, 0)): # Monky patch pygments' dict of console codes # with the new color names used by PTK2 # Can be removed once pygment names get fixed. _monkey_patch_pygments_codes() class XonshTerminal256FormatterProxy(terminal256.Terminal256Formatter): """Proxy class for xonsh terminal256 formatting that understands. xonsh color tokens. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # just keep the opening token for colors. color_names = set(map(str, Color.subtypes)) for name, (opener, closer) in self.style_string.items(): if name in color_names: self.style_string[name] = (opener, "") # special case DEFAULT, because it is special. self.style_string["Token.Color.DEFAULT"] = ("\x1b[39m", "") return XonshTerminal256FormatterProxy
def XonshTerminal256Formatter(): if ( ptk_version_info() and ptk_version_info() > (2, 0) and pygments_version_info() and (2, 2) <= pygments_version_info() < (2, 3) ): # Monky patch pygments' dict of console codes # with the new color names used by PTK2 # Can be removed once pygment names get fixed. _monkey_patch_pygments_codes() class XonshTerminal256FormatterProxy(terminal256.Terminal256Formatter): """Proxy class for xonsh terminal256 formatting that understands. xonsh color tokens. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # just keep the opening token for colors. color_names = set(map(str, Color.subtypes)) for name, (opener, closer) in self.style_string.items(): if name in color_names: self.style_string[name] = (opener, "") # special case NO_COLOR, because it is special. self.style_string["Token.Color.NO_COLOR"] = ("\x1b[39m", "") return XonshTerminal256FormatterProxy
def style_name(self, value): if self._style_name == value: return if value not in STYLES: try: # loading style dynamically pygments_style_by_name(value) except Exception: print( "Could not find style {0!r}, using default".format(value), file=sys.stderr, ) value = "default" builtins.__xonsh__.env["XONSH_COLOR_STYLE"] = value cmap = STYLES[value] if value == "default": self._smap = XONSH_BASE_STYLE.copy() else: try: self._smap = get_style_by_name(value)().styles.copy() except (ImportError, pygments.util.ClassNotFound): self._smap = XONSH_BASE_STYLE.copy() compound = CompoundColorMap( ChainMap(self.trap, cmap, PTK_STYLE, self._smap)) self.styles = ChainMap(self.trap, cmap, PTK_STYLE, self._smap, compound) self._style_name = value # Convert new ansicolor names to old PTK1 names # Can be remvoed when PTK1 support is dropped. if (builtins.__xonsh__.shell.shell_type != "prompt_toolkit2" and pygments_version_info() and pygments_version_info() < (2, 4, 0)): for smap in [self.trap, cmap, PTK_STYLE, self._smap]: smap.update(ansicolors_to_ptk1_names(smap)) if ON_WINDOWS and "prompt_toolkit" in builtins.__xonsh__.shell.shell_type: self.enhance_colors_for_cmd_exe()
def xonsh_style_proxy(styler): """Factory for a proxy class to a xonsh style.""" # Monky patch pygments' list of known ansi colors # with the new ansi color names used by PTK2 # Can be removed once pygment names get fixed. if pygments_version_info() and pygments_version_info() < (2, 4, 0): pygments.style.ansicolors.update(ANSICOLOR_NAMES_MAP) class XonshStyleProxy(Style): """Simple proxy class to fool prompt toolkit.""" target = styler styles = styler.styles def __new__(cls, *args, **kwargs): return cls.target return XonshStyleProxy
def _monkey_patch_pygments_codes(): """ Monky patch pygments' dict of console codes, with new color names """ if pygments_version_info() and pygments_version_info() >= (2, 4, 0): return import pygments.console if "brightblack" in pygments.console.codes: # Assume that colors are already fixed in pygments # for example when using pygments from source return if not getattr(pygments.console, "_xonsh_patched", False): patched_codes = {} for new, old in PTK_NEW_OLD_COLOR_MAP.items(): if old in pygments.console.codes: patched_codes[new[1:]] = pygments.console.codes[old] pygments.console.codes.update(patched_codes) pygments.console._xonsh_patched = True