re-apply original changes

This commit is contained in:
Gyuri Horak 2020-10-27 15:23:58 +01:00
parent 723167f7d0
commit 31dd811ef0
Failed to generate hash of commit
8 changed files with 80 additions and 30 deletions

1
.gitignore vendored
View file

@ -83,3 +83,4 @@ venv/
# mypy
.dmypy.json
.mypy_cache

View file

@ -0,0 +1,31 @@
**Added:**
* PTK style rules can be defined in custom styles using the ``Token.PTK`` token prefix.
For example ``custom_style["Token.PTK.CompletionMenu.Completion.Current"] = "bg:#ff0000 #fff"`` sets the ``completion-menu.completion.current`` PTK style to white on red.
* Added new environment variable ``XONSH_STYLE_OVERRIDES``. It's a dictionary containing pygments/ptk style definitions that overrides the styles defined by ``XONSH_COLOR_STYLE``.
For example::
$XONSH_STYLE_OVERRIDES["Token.Literal.String.Single"] = "#00ff00" # green 'strings' (pygments)
$XONSH_STYLE_OVERRIDES["completion-menu"] = "bg:#ffff00 #000" # black on yellow completion (ptk)
$XONSH_STYLE_OVERRIDES["Token.PTK.CompletionMenu.Completion.Current"] = "bg:#ff0000 #fff" # current completion is white on red (ptk via pygments)
**Changed:**
* <news item>
**Deprecated:**
* ``PTK_STYLE_OVERRIDES`` has been deprecated, its function replaced by ``XONSH_STYLE_OVERRIDES``
**Removed:**
* <news item>
**Fixed:**
* <news item>
**Security:**
* <news item>

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

@ -361,6 +361,11 @@ def test_colorize_file_ca(xonsh_builtins_LS_COLORS, monkeypatch):
{"Literal.String.Single": "#ff0000"},
{Token.Literal.String.Single: "#ff0000"},
), # short str key
(
"test5",
{"completion-menu.completion.current": "#00ff00"},
{Token.PTK.CompletionMenu.Completion.Current: "#00ff00"},
), # ptk style
],
)
def test_register_custom_pygments_style(name, styles, refrules):

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

@ -30,8 +30,6 @@ from xonsh.platform import (
os_environ,
)
from xonsh.style_tools import PTK2_STYLE
from xonsh.tools import (
always_true,
always_false,
@ -1217,8 +1215,8 @@ def DEFAULT_VARS():
is_str_str_dict,
to_str_str_dict,
dict_to_str,
dict(PTK2_STYLE),
"A dictionary containing custom prompt_toolkit style definitions.",
{},
"A dictionary containing custom prompt_toolkit style definitions. (deprecated)",
),
"PUSHD_MINUS": Var(
is_bool,
@ -1718,6 +1716,18 @@ def DEFAULT_VARS():
"Whether or not to store the ``stdout`` and ``stderr`` streams in the "
"history files.",
),
"XONSH_STYLE_OVERRIDES": Var(
is_str_str_dict,
to_str_str_dict,
dict_to_str,
{},
"A dictionary containing custom prompt_toolkit/pygments style definitions.\n"
"The following style definitions are supported:\n\n"
" - ``pygments.token.Token`` - ``$XONSH_STYLE_OVERRIDES[Token.Keyword] = '#ff0000'``\n"
" - pygments token name (string) - ``$XONSH_STYLE_OVERRIDES['Token.Keyword'] = '#ff0000'``\n"
" - ptk style name (string) - ``$XONSH_STYLE_OVERRIDES['pygments.keyword'] = '#ff0000'``\n\n"
"(The rules above are all have the same effect.)",
),
"XONSH_TRACE_SUBPROC": Var(
is_bool,
to_bool,

View file

@ -633,7 +633,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()

View file

@ -181,8 +181,6 @@ DEFAULT_STYLE_DICT = LazyObject(
lambda: "",
{
Token: "",
Token.Aborted: "ansibrightblack",
Token.AutoSuggestion: "ansibrightblack",
Token.Color.BACKGROUND_BLACK: "bg:ansiblack",
Token.Color.BACKGROUND_BLUE: "bg:ansiblue",
Token.Color.BACKGROUND_CYAN: "bg:ansicyan",
@ -314,9 +312,6 @@ DEFAULT_STYLE_DICT = LazyObject(
Token.Literal.String.Regex: "ansimagenta",
Token.Literal.String.Single: "",
Token.Literal.String.Symbol: "ansiyellow",
Token.Menu.Completions: "bg:ansigray ansiblack",
Token.Menu.Completions.Completion: "",
Token.Menu.Completions.Completion.Current: "bg:ansibrightblack ansiwhite",
Token.Name: "",
Token.Name.Attribute: "ansibrightyellow",
Token.Name.Builtin: "ansigreen",
@ -342,24 +337,18 @@ DEFAULT_STYLE_DICT = LazyObject(
Token.Operator.Word: "bold ansimagenta",
Token.Other: "",
Token.Punctuation: "",
Token.Scrollbar: "bg:ansibrightblack",
Token.Scrollbar.Arrow: "bg:ansiblack ansiwhite bold",
Token.Scrollbar.Button: "bg:ansiblack",
Token.Text: "",
Token.Text.Whitespace: "ansigray",
Token.PTK.Aborting: "ansibrightblack",
Token.PTK.AutoSuggestion: "ansibrightblack",
Token.PTK.CompletionMenu: "bg:ansigray ansiblack",
Token.PTK.CompletionMenu.Completion: "",
Token.PTK.CompletionMenu.Completion.Current: "bg:ansibrightblack ansiwhite",
Token.PTK.Scrollbar.Arrow: "bg:ansiblack ansiwhite bold",
Token.PTK.Scrollbar.Background: "bg:ansibrightblack",
Token.PTK.Scrollbar.Button: "bg:ansiblack",
},
),
globals(),
"DEFAULT_STYLE_DICT",
)
PTK2_STYLE = {
"completion-menu": "bg:ansigray ansiblack",
"completion-menu.completion": "",
"completion-menu.completion.current": "bg:ansibrightblack ansiwhite",
"scrollbar.background": "bg:ansibrightblack",
"scrollbar.arrow": "bg:ansiblack ansiwhite bold",
"scrollbar.button": "bg:ansiblack",
"auto-suggestion": "ansibrightblack",
"aborting": "ansibrightblack",
}