style override support for readline/ansi

This commit is contained in:
Gyuri Horak 2020-10-23 17:10:54 +02:00
parent f18f45bcab
commit 39b2a50e7d
Failed to generate hash of commit
3 changed files with 21 additions and 7 deletions

View file

@ -147,9 +147,10 @@ def test_ansi_color_name_to_escape_code_for_all_styles(color, style):
[
("test1", {}, {}),
("test2", {"Color.RED": "#ff0000"}, {"RED": "38;5;196"}),
("test3", {"BOLD_RED": "bold #ff0000"}, {"BOLD_RED": "1;38;5;196"}),
("test3", {"Token.Color.RED": "#ff0000"}, {"RED": "38;5;196"}),
("test4", {"BOLD_RED": "bold #ff0000"}, {"BOLD_RED": "1;38;5;196"}),
(
"test4",
"test5",
{"INTENSE_RED": "italic underline bg:#ff0000 #ff0000"},
{"INTENSE_RED": "3;4;48;5;196;38;5;196"},
),

View file

@ -146,6 +146,9 @@ def ansi_partial_color_format(template, style="default", cmap=None, hide=False):
def _ansi_partial_color_format_main(template, style="default", cmap=None, hide=False):
cmap = _ensure_color_map(style=style, cmap=cmap)
overrides = builtins.__xonsh__.env["XONSH_STYLE_OVERRIDES"]
if overrides:
cmap.update(_style_dict_to_ansi(overrides))
esc = ("\001" if hide else "") + "\033["
m = "m" + ("\002" if hide else "")
bopen = "{"
@ -1104,6 +1107,18 @@ def _pygments_to_ansi_style(style):
return ";".join(ansi_style_list)
def _style_dict_to_ansi(styles):
"""Converts pygments like style dict to ANSI rules"""
ansi_style = {}
for token, style in styles.items():
token = str(token) # convert pygments token to str
parts = token.split(".")
if len(parts) == 1 or parts[-2] == "Color":
ansi_style[parts[-1]] = _pygments_to_ansi_style(style)
return ansi_style
def register_custom_ansi_style(name, styles, base="default"):
"""Register custom ANSI style.
@ -1118,11 +1133,7 @@ def register_custom_ansi_style(name, styles, base="default"):
"""
base_style = ANSI_STYLES[base].copy()
for token, style in styles.items():
token = str(token) # convert pygments token to str
parts = token.split(".")
if len(parts) == 1 or parts[-2] == "Color":
base_style[parts[-1]] = _pygments_to_ansi_style(style)
base_style.update(_style_dict_to_ansi(styles))
ANSI_STYLES[name] = base_style

View file

@ -636,7 +636,9 @@ class ReadlineShell(BaseShell, cmd.Cmd):
else:
# assume this is a list of (Token, str) tuples and format it
env = builtins.__xonsh__.env
style_overrides_env = env.get("XONSH_STYLE_OVERRIDES", {})
self.styler.style_name = env.get("XONSH_COLOR_STYLE")
self.styler.override(style_overrides_env)
style_proxy = pyghooks.xonsh_style_proxy(self.styler)
formatter = pyghooks.XonshTerminal256Formatter(style=style_proxy)
s = pygments.format(string, formatter).rstrip()