basicly working

This commit is contained in:
Anthony Scopatz 2019-03-12 18:36:18 -04:00
parent ac742be19a
commit 20ac4bf46f
4 changed files with 61 additions and 31 deletions

View file

@ -1518,8 +1518,6 @@ or ``{BOLD_BLUE}``. Colors have the form shown below:
``BOLD_UNDERLINE_INTENSE_BLACK``, which is the most metal color you
can use!
.. note:: Not all color modifier comboninations have been implemented on prompt-toolkit.
You can make use of additional variables beyond these by adding them to the
``PROMPT_FIELDS`` environment variable. The values in this dictionary
should be strings (which will be inserted into the prompt verbatim), or

View file

@ -2,30 +2,32 @@
import pytest
from xonsh.pyghooks import (
Color,
color_name_to_pygments_code,
code_by_name,
)
DEFAULT_STYLES = {
# Reset
"NO_COLOR": "noinherit", # Text Reset
Color.NO_COLOR: "noinherit", # Text Reset
# Regular Colors
"BLACK": "ansiblack",
"BLUE": "ansiblue",
"CYAN": "ansicyan",
"GREEN": "ansigreen",
"PURPLE": "ansimagenta",
"RED": "ansired",
"WHITE": "ansigray",
"YELLOW": "ansiyellow",
"INTENSE_BLACK": "ansibrightblack",
"INTENSE_BLUE": "ansibrightblue",
"INTENSE_CYAN": "ansibrightcyan",
"INTENSE_GREEN": "ansibrightgreen",
"INTENSE_PURPLE": "ansibrightmagenta",
"INTENSE_RED": "ansibrightred",
"INTENSE_WHITE": "ansiwhite",
"INTENSE_YELLOW": "ansibrightyellow",
Color.BLACK: "ansiblack",
Color.BLUE: "ansiblue",
Color.CYAN: "ansicyan",
Color.GREEN: "ansigreen",
Color.PURPLE: "ansimagenta",
Color.RED: "ansired",
Color.WHITE: "ansigray",
Color.YELLOW: "ansiyellow",
Color.INTENSE_BLACK: "ansibrightblack",
Color.INTENSE_BLUE: "ansibrightblue",
Color.INTENSE_CYAN: "ansibrightcyan",
Color.INTENSE_GREEN: "ansibrightgreen",
Color.INTENSE_PURPLE: "ansibrightmagenta",
Color.INTENSE_RED: "ansibrightred",
Color.INTENSE_WHITE: "ansiwhite",
Color.INTENSE_YELLOW: "ansibrightyellow",
}
@ -60,3 +62,36 @@ def test_color_name_to_pygments_code(name, exp):
styles = DEFAULT_STYLES.copy()
obs = color_name_to_pygments_code(name, styles)
assert obs == exp
@pytest.mark.parametrize(
"name, exp",
[
("NO_COLOR", "noinherit"),
("RED", "ansired"),
("BACKGROUND_RED", "bg:ansired"),
("BACKGROUND_INTENSE_RED", "bg:ansibrightred"),
("BOLD_RED", "bold ansired"),
("UNDERLINE_RED", "underline ansired"),
("BOLD_UNDERLINE_RED", "bold underline ansired"),
("UNDERLINE_BOLD_RED", "underline bold ansired"),
# test unsupported modifiers
("BOLD_FAINT_RED", "bold ansired"),
("BOLD_SLOWBLINK_RED", "bold ansired"),
("BOLD_FASTBLINK_RED", "bold ansired"),
("BOLD_INVERT_RED", "bold ansired"),
("BOLD_CONCEAL_RED", "bold ansired"),
("BOLD_STRIKETHROUGH_RED", "bold ansired"),
# test hexes
("#000", "#000"),
("#000000", "#000000"),
("BACKGROUND_#000", "bg:#000"),
("BACKGROUND_#000000", "bg:#000000"),
("BG#000", "bg:#000"),
("bg#000000", "bg:#000000"),
],
)
def test_code_by_name(name, exp):
styles = DEFAULT_STYLES.copy()
obs = code_by_name(name, styles)
assert obs == exp

View file

@ -27,7 +27,7 @@ def KNOWN_XONSH_COLORS():
"RED",
"GREEN",
"YELLOW",
"BLUE", 1
"BLUE",
"PURPLE",
"CYAN",
"WHITE",

View file

@ -299,8 +299,9 @@ def PYGMENTS_MODIFIERS():
def color_name_to_pygments_code(name, styles):
"""Converts a xonsh color name to a pygments color code."""
if name in styles:
return styles[name]
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))
@ -315,7 +316,7 @@ def color_name_to_pygments_code(name, styles):
if "#" in color:
fgcolor = color
else:
fgcolor = styles[color]
fgcolor = styles[getattr(Color, color)]
res = "bg:" + fgcolor
else:
# have regular, non-background color
@ -330,9 +331,9 @@ def color_name_to_pygments_code(name, styles):
if "#" in color:
mods.append(color)
else:
mods.append(styles[color])
mods.append(styles[getattr(Color, color)])
res = " ".join(mods)
styles[name] = res
styles[token] = res
return res
@ -352,7 +353,7 @@ def code_by_name(name, styles):
Pygments style color code.
"""
fg, _, bg = name.upper().replace('HEX', '#').partition("__")
if fg.startswith("BACKGROUND_") or fg.startwith("BG#"):
if fg.startswith("BACKGROUND_") or fg.startswith("BG#"):
# swap fore & back if needed.
fg, bg = bg, fg
# convert names to codes
@ -360,7 +361,7 @@ def code_by_name(name, styles):
code = "noinherit"
elif len(fg) == 0:
code = color_name_to_pygments_code(bg, styles)
elif len(fg) == 0:
elif len(bg) == 0:
code = color_name_to_pygments_code(fg, styles)
else:
# have both colors
@ -1295,10 +1296,6 @@ def make_pygments_style(palette):
for name, t in BASE_XONSH_COLORS.items():
color = find_closest_color(t, palette)
style[getattr(Color, name)] = "#" + color
style[getattr(Color, "BOLD_" + name)] = "bold #" + color
style[getattr(Color, "UNDERLINE_" + name)] = "underline #" + color
style[getattr(Color, "BOLD_UNDERLINE_" + name)] = "bold underline #" + color
style[getattr(Color, "BACKGROUND_" + name)] = "bg:#" + color
return style