support for PTK specific styles

This commit is contained in:
Gyuri Horak 2020-10-17 22:26:30 +02:00
parent cc5ef74cd7
commit 604ac6f562
Failed to generate hash of commit
3 changed files with 24 additions and 3 deletions

1
.gitignore vendored
View file

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

View file

@ -40,6 +40,7 @@ from prompt_toolkit.styles.pygments import (
ANSI_OSC_PATTERN = re.compile("\x1b].*?\007")
CAPITAL_PATTERN = re.compile(r"([a-z])([A-Z])")
Token = _TokenType()
events.transmogrify("on_ptk_create", "LoadEvent")
@ -92,6 +93,18 @@ def remove_ansi_osc(prompt):
return prompt, osc_tokens
def _extract_ptk_style(pygments_style):
"""Extract PTK specific rules that are not handled by ``style_from_pygments_cls``
"""
rules = {}
for rule, value in pygments_style.styles.items():
if str(rule).startswith("Token.PTK"):
key = CAPITAL_PATTERN.sub(r"\1-\2", str(rule)[10:]).lower()
rules[key] = value
return rules
class PromptToolkitShell(BaseShell):
"""The xonsh shell for prompt_toolkit v2 and later."""
@ -201,7 +214,14 @@ class PromptToolkitShell(BaseShell):
if env.get("COLOR_INPUT"):
if HAS_PYGMENTS:
prompt_args["lexer"] = PygmentsLexer(pyghooks.XonshLexer)
style = style_from_pygments_cls(pyghooks.xonsh_style_proxy(self.styler))
style = merge_styles(
[
Style.from_dict(_extract_ptk_style(self.styler)),
style_from_pygments_cls(
pyghooks.xonsh_style_proxy(self.styler)
),
]
)
else:
style = style_from_pygments_dict(DEFAULT_STYLE_DICT)

View file

@ -382,8 +382,8 @@ class XonshStyle(Style):
self.background_color = style_obj.background_color
except (ImportError, pygments.util.ClassNotFound):
self._smap = XONSH_BASE_STYLE.copy()
compound = CompoundColorMap(ChainMap(self.trap, cmap, PTK_STYLE, self._smap))
self.styles = ChainMap(self.trap, cmap, PTK_STYLE, self._smap, compound)
compound = CompoundColorMap(ChainMap(self.trap, cmap, self._smap, PTK_STYLE))
self.styles = ChainMap(self.trap, cmap, self._smap, PTK_STYLE, compound)
self._style_name = value
for file_type, xonsh_color in builtins.__xonsh__.env.get(