From ac742be19ab9c3eb6c2c4e7ac890af7642b9bf81 Mon Sep 17 00:00:00 2001 From: Anthony Scopatz Date: Tue, 12 Mar 2019 17:28:09 -0400 Subject: [PATCH] updated pyghooks --- xonsh/color_tools.py | 26 +++++++ xonsh/pyghooks.py | 175 ++++--------------------------------------- xonsh/style_tools.py | 96 +----------------------- 3 files changed, 45 insertions(+), 252 deletions(-) diff --git a/xonsh/color_tools.py b/xonsh/color_tools.py index d50560e17..971d76d49 100644 --- a/xonsh/color_tools.py +++ b/xonsh/color_tools.py @@ -16,6 +16,32 @@ RE_BACKGROUND = LazyObject( ) +@lazyobject +def KNOWN_XONSH_COLORS(): + """These are the minimum number of colors that need to be implemented by + any style. + """ + return frozenset([ + "NO_COLOR", + "BLACK", + "RED", + "GREEN", + "YELLOW", + "BLUE", 1 + "PURPLE", + "CYAN", + "WHITE", + "INTENSE_BLACK", + "INTENSE_RED", + "INTENSE_GREEN", + "INTENSE_YELLOW", + "INTENSE_BLUE", + "INTENSE_PURPLE", + "INTENSE_CYAN", + "INTENSE_WHITE", + ]) + + @lazyobject def BASE_XONSH_COLORS(): return { diff --git a/xonsh/pyghooks.py b/xonsh/pyghooks.py index 19d16388c..53b956279 100644 --- a/xonsh/pyghooks.py +++ b/xonsh/pyghooks.py @@ -44,6 +44,7 @@ from xonsh.color_tools import ( RE_XONSH_COLOR, make_palette, find_closest_color, + iscolor, ) from xonsh.style_tools import norm_name from xonsh.lazyimps import terminal256 @@ -350,34 +351,22 @@ def code_by_name(name, styles): code : str Pygments style color code. """ - fg, _, bg = name.lower().partition("__") - if fg.startswith("background_"): + fg, _, bg = name.upper().replace('HEX', '#').partition("__") + if fg.startswith("BACKGROUND_") or fg.startwith("BG#"): + # swap fore & back if needed. fg, bg = bg, fg - codes = [] - # foreground color - if len(fg) == 0: - pass - elif "hex" in fg: - for p in fg.split("_"): - codes.append("#" + p[3:] if p.startswith("hex") else p) + # convert names to codes + if len(fg) == 0 and len(bg) == 0: + code = "noinherit" + elif len(fg) == 0: + code = color_name_to_pygments_code(bg, styles) + elif len(fg) == 0: + code = color_name_to_pygments_code(fg, styles) else: - fgtok = getattr(Color, fg.upper()) - if fgtok in styles: - codes.append(styles[fgtok]) - else: - codes += fg.split("_") - # background color - if len(bg) == 0: - pass - elif bg.startswith("background_hex"): - codes.append("bg:#" + bg[14:]) - else: - bgtok = getattr(Color, bg.upper()) - if bgtok in styles: - codes.append(styles[bgtok]) - else: - codes.append(bg.replace("background_", "bg:")) - code = " ".join(codes) + # have both colors + code = color_name_to_pygments_code(bg, styles) + code += " " + code += color_name_to_pygments_code(fg, styles) return code @@ -412,7 +401,7 @@ def _partial_color_tokenize_main(template, styles): for literal, field, spec, conv in FORMATTER.parse(template): if field is None: value += literal - elif field in KNOWN_COLORS or "#" in field: + elif iscolor(field): value += literal next_color, fg, bg = color_by_name(field, fg, bg) if next_color is not color: @@ -634,112 +623,6 @@ XONSH_BASE_STYLE = LazyObject( ) -KNOWN_COLORS = LazyObject( - lambda: frozenset( - [ - "BACKGROUND_BLACK", - "BACKGROUND_BLUE", - "BACKGROUND_CYAN", - "BACKGROUND_GREEN", - "BACKGROUND_INTENSE_BLACK", - "BACKGROUND_INTENSE_BLUE", - "BACKGROUND_INTENSE_CYAN", - "BACKGROUND_INTENSE_GREEN", - "BACKGROUND_INTENSE_PURPLE", - "BACKGROUND_INTENSE_RED", - "BACKGROUND_INTENSE_WHITE", - "BACKGROUND_INTENSE_YELLOW", - "BACKGROUND_PURPLE", - "BACKGROUND_RED", - "BACKGROUND_WHITE", - "BACKGROUND_YELLOW", - "BLACK", - "BLUE", - "BOLD_BLACK", - "BOLD_BLUE", - "BOLD_CYAN", - "BOLD_GREEN", - "BOLD_INTENSE_BLACK", - "BOLD_INTENSE_BLUE", - "BOLD_INTENSE_CYAN", - "BOLD_INTENSE_GREEN", - "BOLD_INTENSE_PURPLE", - "BOLD_INTENSE_RED", - "BOLD_INTENSE_WHITE", - "BOLD_INTENSE_YELLOW", - "BOLD_PURPLE", - "BOLD_RED", - "BOLD_UNDERLINE_BLACK", - "BOLD_UNDERLINE_BLUE", - "BOLD_UNDERLINE_CYAN", - "BOLD_UNDERLINE_GREEN", - "BOLD_UNDERLINE_INTENSE_BLACK", - "BOLD_UNDERLINE_INTENSE_BLUE", - "BOLD_UNDERLINE_INTENSE_CYAN", - "BOLD_UNDERLINE_INTENSE_GREEN", - "BOLD_UNDERLINE_INTENSE_PURPLE", - "BOLD_UNDERLINE_INTENSE_RED", - "BOLD_UNDERLINE_INTENSE_WHITE", - "BOLD_UNDERLINE_INTENSE_YELLOW", - "BOLD_UNDERLINE_PURPLE", - "BOLD_UNDERLINE_RED", - "BOLD_UNDERLINE_WHITE", - "BOLD_UNDERLINE_YELLOW", - "BOLD_WHITE", - "BOLD_YELLOW", - "CYAN", - "GREEN", - "INTENSE_BLACK", - "INTENSE_BLUE", - "INTENSE_CYAN", - "INTENSE_GREEN", - "INTENSE_PURPLE", - "INTENSE_RED", - "INTENSE_WHITE", - "INTENSE_YELLOW", - "NO_COLOR", - "PURPLE", - "RED", - "UNDERLINE_BLACK", - "UNDERLINE_BLUE", - "UNDERLINE_CYAN", - "UNDERLINE_GREEN", - "UNDERLINE_INTENSE_BLACK", - "UNDERLINE_INTENSE_BLUE", - "UNDERLINE_INTENSE_CYAN", - "UNDERLINE_INTENSE_GREEN", - "UNDERLINE_INTENSE_PURPLE", - "UNDERLINE_INTENSE_RED", - "UNDERLINE_INTENSE_WHITE", - "UNDERLINE_INTENSE_YELLOW", - "UNDERLINE_PURPLE", - "UNDERLINE_RED", - "UNDERLINE_WHITE", - "UNDERLINE_YELLOW", - "WHITE", - "YELLOW", - ] - ), - globals(), - "KNOWN_COLORS", -) - - -def _expand_style(cmap): - """Expands a style in order to more quickly make color map changes.""" - for key, val in list(cmap.items()): - if key is Color.NO_COLOR: - continue - _, _, key = str(key).rpartition(".") - cmap[getattr(Color, "BOLD_" + key)] = "bold " + val - cmap[getattr(Color, "UNDERLINE_" + key)] = "underline " + val - cmap[getattr(Color, "BOLD_UNDERLINE_" + key)] = "bold underline " + val - if val == "noinherit": - cmap[getattr(Color, "BACKGROUND_" + key)] = val - else: - cmap[getattr(Color, "BACKGROUND_" + key)] = "bg:" + val - - def _bw_style(): style = { Color.BLACK: "noinherit", @@ -760,7 +643,6 @@ def _bw_style(): Color.WHITE: "noinherit", Color.YELLOW: "noinherit", } - _expand_style(style) return style @@ -784,7 +666,6 @@ def _default_style(): Color.WHITE: "ansigray", Color.YELLOW: "ansiyellow", } - _expand_style(style) return style @@ -808,7 +689,6 @@ def _monokai_style(): Color.WHITE: "#d7d7d7", Color.YELLOW: "#e2e22e", } - _expand_style(style) return style @@ -835,7 +715,6 @@ def _algol_style(): Color.WHITE: "#888", Color.YELLOW: "#FF0000", } - _expand_style(style) return style @@ -859,7 +738,6 @@ def _algol_nu_style(): Color.WHITE: "#888", Color.YELLOW: "#FF0000", } - _expand_style(style) return style @@ -883,7 +761,6 @@ def _autumn_style(): Color.WHITE: "#aaaaaa", Color.YELLOW: "#aa5500", } - _expand_style(style) return style @@ -907,7 +784,6 @@ def _borland_style(): Color.WHITE: "#aaaaaa", Color.YELLOW: "#a61717", } - _expand_style(style) return style @@ -931,7 +807,6 @@ def _colorful_style(): Color.WHITE: "#bbbbbb", Color.YELLOW: "#A60", } - _expand_style(style) return style @@ -955,7 +830,6 @@ def _emacs_style(): Color.WHITE: "#bbbbbb", Color.YELLOW: "#BB6622", } - _expand_style(style) return style @@ -979,7 +853,6 @@ def _friendly_style(): Color.WHITE: "#bbbbbb", Color.YELLOW: "#c65d09", } - _expand_style(style) return style @@ -1003,7 +876,6 @@ def _fruity_style(): Color.WHITE: "#cdcaa9", Color.YELLOW: "#fb660a", } - _expand_style(style) return style @@ -1027,7 +899,6 @@ def _igor_style(): Color.WHITE: "#CC00A3", Color.YELLOW: "#C34E00", } - _expand_style(style) return style @@ -1051,7 +922,6 @@ def _lovelace_style(): Color.WHITE: "#888888", Color.YELLOW: "#b85820", } - _expand_style(style) return style @@ -1075,7 +945,6 @@ def _manni_style(): Color.WHITE: "#AAAAAA", Color.YELLOW: "#CC3300", } - _expand_style(style) return style @@ -1099,7 +968,6 @@ def _murphy_style(): Color.WHITE: "#bbbbbb", Color.YELLOW: "#c65d09", } - _expand_style(style) return style @@ -1123,7 +991,6 @@ def _native_style(): Color.WHITE: "#aaaaaa", Color.YELLOW: "#a61717", } - _expand_style(style) return style @@ -1147,7 +1014,6 @@ def _paraiso_dark_style(): Color.WHITE: "#5bc4bf", Color.YELLOW: "#f99b15", } - _expand_style(style) return style @@ -1171,7 +1037,6 @@ def _paraiso_light_style(): Color.WHITE: "#8d8687", Color.YELLOW: "#f99b15", } - _expand_style(style) return style @@ -1195,7 +1060,6 @@ def _pastie_style(): Color.WHITE: "#bbbbbb", Color.YELLOW: "#aa6600", } - _expand_style(style) return style @@ -1219,7 +1083,6 @@ def _perldoc_style(): Color.WHITE: "#a7a7a7", Color.YELLOW: "#cb6c20", } - _expand_style(style) return style @@ -1243,7 +1106,6 @@ def _rrt_style(): Color.WHITE: "#87ceeb", Color.YELLOW: "#ff0000", } - _expand_style(style) return style @@ -1267,7 +1129,6 @@ def _tango_style(): Color.WHITE: "#f8f8f8", Color.YELLOW: "#8f5902", } - _expand_style(style) return style @@ -1291,7 +1152,6 @@ def _trac_style(): Color.WHITE: "#aaaaaa", Color.YELLOW: "#808000", } - _expand_style(style) return style @@ -1315,7 +1175,6 @@ def _vim_style(): Color.WHITE: "#cccccc", Color.YELLOW: "#cd0000", } - _expand_style(style) return style @@ -1339,7 +1198,6 @@ def _vs_style(): Color.WHITE: "#2b91af", Color.YELLOW: "#a31515", } - _expand_style(style) return style @@ -1363,7 +1221,6 @@ def _xcode_style(): Color.WHITE: "#3F6E75", Color.YELLOW: "#836C28", } - _expand_style(style) return style diff --git a/xonsh/style_tools.py b/xonsh/style_tools.py index 432ec4aa9..6247a080d 100644 --- a/xonsh/style_tools.py +++ b/xonsh/style_tools.py @@ -4,7 +4,7 @@ from collections import defaultdict from xonsh.platform import HAS_PYGMENTS from xonsh.lazyasd import LazyObject -from xonsh.color_tools import RE_BACKGROUND +from xonsh.color_tools import RE_BACKGROUND, iscolor from xonsh.tools import FORMATTER @@ -92,7 +92,7 @@ def _partial_color_tokenize_main(template, styles): for literal, field, spec, conv in FORMATTER.parse(template): if field is None: value += literal - elif field in KNOWN_COLORS or "#" in field: + elif iscolor(field): value += literal next_color, fg, bg = color_by_name(field, fg, bg) if next_color is not color: @@ -164,99 +164,9 @@ def color_by_name(name, fg=None, bg=None): def norm_name(name): """Normalizes a color name.""" - return name.replace("#", "HEX").replace("BGHEX", "BACKGROUND_HEX") + return name.upper().replace("#", "HEX") -KNOWN_COLORS = LazyObject( - lambda: frozenset( - [ - "BACKGROUND_BLACK", - "BACKGROUND_BLUE", - "BACKGROUND_CYAN", - "BACKGROUND_GREEN", - "BACKGROUND_INTENSE_BLACK", - "BACKGROUND_INTENSE_BLUE", - "BACKGROUND_INTENSE_CYAN", - "BACKGROUND_INTENSE_GREEN", - "BACKGROUND_INTENSE_PURPLE", - "BACKGROUND_INTENSE_RED", - "BACKGROUND_INTENSE_WHITE", - "BACKGROUND_INTENSE_YELLOW", - "BACKGROUND_PURPLE", - "BACKGROUND_RED", - "BACKGROUND_WHITE", - "BACKGROUND_YELLOW", - "BLACK", - "BLUE", - "BOLD_BLACK", - "BOLD_BLUE", - "BOLD_CYAN", - "BOLD_GREEN", - "BOLD_INTENSE_BLACK", - "BOLD_INTENSE_BLUE", - "BOLD_INTENSE_CYAN", - "BOLD_INTENSE_GREEN", - "BOLD_INTENSE_PURPLE", - "BOLD_INTENSE_RED", - "BOLD_INTENSE_WHITE", - "BOLD_INTENSE_YELLOW", - "BOLD_PURPLE", - "BOLD_RED", - "BOLD_UNDERLINE_BLACK", - "BOLD_UNDERLINE_BLUE", - "BOLD_UNDERLINE_CYAN", - "BOLD_UNDERLINE_GREEN", - "BOLD_UNDERLINE_INTENSE_BLACK", - "BOLD_UNDERLINE_INTENSE_BLUE", - "BOLD_UNDERLINE_INTENSE_CYAN", - "BOLD_UNDERLINE_INTENSE_GREEN", - "BOLD_UNDERLINE_INTENSE_PURPLE", - "BOLD_UNDERLINE_INTENSE_RED", - "BOLD_UNDERLINE_INTENSE_WHITE", - "BOLD_UNDERLINE_INTENSE_YELLOW", - "BOLD_UNDERLINE_PURPLE", - "BOLD_UNDERLINE_RED", - "BOLD_UNDERLINE_WHITE", - "BOLD_UNDERLINE_YELLOW", - "BOLD_WHITE", - "BOLD_YELLOW", - "CYAN", - "GREEN", - "INTENSE_BLACK", - "INTENSE_BLUE", - "INTENSE_CYAN", - "INTENSE_GREEN", - "INTENSE_PURPLE", - "INTENSE_RED", - "INTENSE_WHITE", - "INTENSE_YELLOW", - "NO_COLOR", - "PURPLE", - "RED", - "UNDERLINE_BLACK", - "UNDERLINE_BLUE", - "UNDERLINE_CYAN", - "UNDERLINE_GREEN", - "UNDERLINE_INTENSE_BLACK", - "UNDERLINE_INTENSE_BLUE", - "UNDERLINE_INTENSE_CYAN", - "UNDERLINE_INTENSE_GREEN", - "UNDERLINE_INTENSE_PURPLE", - "UNDERLINE_INTENSE_RED", - "UNDERLINE_INTENSE_WHITE", - "UNDERLINE_INTENSE_YELLOW", - "UNDERLINE_PURPLE", - "UNDERLINE_RED", - "UNDERLINE_WHITE", - "UNDERLINE_YELLOW", - "WHITE", - "YELLOW", - ] - ), - globals(), - "KNOWN_COLORS", -) - DEFAULT_STYLE_DICT = LazyObject( lambda: defaultdict( lambda: "",