mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 16:34:47 +01:00
style override support for readline/ansi
This commit is contained in:
parent
f18f45bcab
commit
39b2a50e7d
3 changed files with 21 additions and 7 deletions
|
@ -147,9 +147,10 @@ def test_ansi_color_name_to_escape_code_for_all_styles(color, style):
|
||||||
[
|
[
|
||||||
("test1", {}, {}),
|
("test1", {}, {}),
|
||||||
("test2", {"Color.RED": "#ff0000"}, {"RED": "38;5;196"}),
|
("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": "italic underline bg:#ff0000 #ff0000"},
|
||||||
{"INTENSE_RED": "3;4;48;5;196;38;5;196"},
|
{"INTENSE_RED": "3;4;48;5;196;38;5;196"},
|
||||||
),
|
),
|
||||||
|
|
|
@ -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):
|
def _ansi_partial_color_format_main(template, style="default", cmap=None, hide=False):
|
||||||
cmap = _ensure_color_map(style=style, cmap=cmap)
|
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["
|
esc = ("\001" if hide else "") + "\033["
|
||||||
m = "m" + ("\002" if hide else "")
|
m = "m" + ("\002" if hide else "")
|
||||||
bopen = "{"
|
bopen = "{"
|
||||||
|
@ -1104,6 +1107,18 @@ def _pygments_to_ansi_style(style):
|
||||||
return ";".join(ansi_style_list)
|
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"):
|
def register_custom_ansi_style(name, styles, base="default"):
|
||||||
"""Register custom ANSI style.
|
"""Register custom ANSI style.
|
||||||
|
|
||||||
|
@ -1118,11 +1133,7 @@ def register_custom_ansi_style(name, styles, base="default"):
|
||||||
"""
|
"""
|
||||||
base_style = ANSI_STYLES[base].copy()
|
base_style = ANSI_STYLES[base].copy()
|
||||||
|
|
||||||
for token, style in styles.items():
|
base_style.update(_style_dict_to_ansi(styles))
|
||||||
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)
|
|
||||||
|
|
||||||
ANSI_STYLES[name] = base_style
|
ANSI_STYLES[name] = base_style
|
||||||
|
|
||||||
|
|
|
@ -636,7 +636,9 @@ class ReadlineShell(BaseShell, cmd.Cmd):
|
||||||
else:
|
else:
|
||||||
# assume this is a list of (Token, str) tuples and format it
|
# assume this is a list of (Token, str) tuples and format it
|
||||||
env = builtins.__xonsh__.env
|
env = builtins.__xonsh__.env
|
||||||
|
style_overrides_env = env.get("XONSH_STYLE_OVERRIDES", {})
|
||||||
self.styler.style_name = env.get("XONSH_COLOR_STYLE")
|
self.styler.style_name = env.get("XONSH_COLOR_STYLE")
|
||||||
|
self.styler.override(style_overrides_env)
|
||||||
style_proxy = pyghooks.xonsh_style_proxy(self.styler)
|
style_proxy = pyghooks.xonsh_style_proxy(self.styler)
|
||||||
formatter = pyghooks.XonshTerminal256Formatter(style=style_proxy)
|
formatter = pyghooks.XonshTerminal256Formatter(style=style_proxy)
|
||||||
s = pygments.format(string, formatter).rstrip()
|
s = pygments.format(string, formatter).rstrip()
|
||||||
|
|
Loading…
Add table
Reference in a new issue