mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-06 09:20:57 +01:00
Merge pull request #2731 from xonsh/wincolors_ptk2
Fix regression for INTENSIFY_COLORS_ON_WIN due to PTK2
This commit is contained in:
commit
33e6d8772b
3 changed files with 46 additions and 18 deletions
13
news/color_win.rst
Normal file
13
news/color_win.rst
Normal file
|
@ -0,0 +1,13 @@
|
|||
**Added:** None
|
||||
|
||||
**Changed:** None
|
||||
|
||||
**Deprecated:** None
|
||||
|
||||
**Removed:** None
|
||||
|
||||
**Fixed:**
|
||||
|
||||
* Fix regression in ``INTENSIFY_COLORS_ON_WIN`` functionality due to prompt_toolkit 2 update.
|
||||
|
||||
**Security:** None
|
|
@ -298,7 +298,7 @@ def clean_jobs():
|
|||
else:
|
||||
msg = 'there is an unfinished job'
|
||||
|
||||
if builtins.__xonsh_env__['SHELL_TYPE'] != 'prompt_toolkit':
|
||||
if 'prompt_toolkit' not in builtins.__xonsh_env__['SHELL_TYPE']:
|
||||
# The Ctrl+D binding for prompt_toolkit already inserts a
|
||||
# newline
|
||||
print()
|
||||
|
|
|
@ -41,7 +41,7 @@ import operator
|
|||
# dependencies
|
||||
from xonsh import __version__
|
||||
from xonsh.lazyasd import LazyObject, LazyDict, lazyobject
|
||||
from xonsh.platform import (has_prompt_toolkit, scandir, DEFAULT_ENCODING,
|
||||
from xonsh.platform import (scandir, DEFAULT_ENCODING,
|
||||
ON_LINUX, ON_WINDOWS, PYTHON_VERSION_INFO,
|
||||
expanduser, os_environ)
|
||||
|
||||
|
@ -1568,18 +1568,37 @@ def color_style():
|
|||
return builtins.__xonsh_shell__.shell.color_style()
|
||||
|
||||
|
||||
def _token_attr_from_stylemap(stylemap):
|
||||
"""yields tokens attr, and index from a stylemap """
|
||||
import prompt_toolkit as ptk
|
||||
if builtins.__xonsh_shell__.shell_type == 'prompt_toolkit1':
|
||||
style = ptk.styles.style_from_dict(stylemap)
|
||||
for token in stylemap:
|
||||
yield token, style.token_to_attrs[token]
|
||||
else:
|
||||
style = ptk.styles.style_from_pygments_dict(stylemap)
|
||||
for token in stylemap:
|
||||
style_str = 'class:{}'.format(
|
||||
ptk.styles.pygments.pygments_token_to_classname(token)
|
||||
)
|
||||
yield (token, style.get_attrs_for_style_str(style_str))
|
||||
|
||||
|
||||
def _get_color_lookup_table():
|
||||
"""Returns the prompt_toolkit win32 ColorLookupTable """
|
||||
if builtins.__xonsh_shell__.shell_type == 'prompt_toolkit1':
|
||||
from prompt_toolkit.terminal.win32_output import ColorLookupTable
|
||||
else:
|
||||
from prompt_toolkit.output.win32 import ColorLookupTable
|
||||
return ColorLookupTable()
|
||||
|
||||
|
||||
def _get_color_indexes(style_map):
|
||||
""" Generates the color and windows color index for a style """
|
||||
import prompt_toolkit
|
||||
table = prompt_toolkit.terminal.win32_output.ColorLookupTable()
|
||||
pt_style = prompt_toolkit.styles.style_from_dict(style_map)
|
||||
for token in style_map:
|
||||
attr = pt_style.token_to_attrs[token]
|
||||
"""Generates the color and windows color index for a style """
|
||||
table = _get_color_lookup_table()
|
||||
for token, attr in _token_attr_from_stylemap(style_map):
|
||||
if attr.color is not None:
|
||||
try:
|
||||
index = table.lookup_color(attr.color, attr.bgcolor)
|
||||
except AttributeError:
|
||||
index = table.lookup_fg_color(attr.color)
|
||||
index = table.lookup_fg_color(attr.color)
|
||||
try:
|
||||
rgb = (int(attr.color[0:2], 16),
|
||||
int(attr.color[2:4], 16),
|
||||
|
@ -1596,9 +1615,7 @@ def intensify_colors_for_cmd_exe(style_map, replace_colors=None, ansi=False):
|
|||
"""
|
||||
modified_style = {}
|
||||
stype = builtins.__xonsh_env__.get('SHELL_TYPE')
|
||||
if (not ON_WINDOWS or
|
||||
(stype not in ('prompt_toolkit', 'best')) or
|
||||
(stype == 'best' and not has_prompt_toolkit())):
|
||||
if (not ON_WINDOWS or 'prompt_toolkit' not in stype):
|
||||
return modified_style
|
||||
if replace_colors is None:
|
||||
if ansi:
|
||||
|
@ -1634,9 +1651,7 @@ def expand_gray_colors_for_cmd_exe(style_map):
|
|||
"""
|
||||
modified_style = {}
|
||||
stype = builtins.__xonsh_env__.get('SHELL_TYPE')
|
||||
if (not ON_WINDOWS or
|
||||
(stype not in ('prompt_toolkit', 'best')) or
|
||||
(stype == 'best' and not has_prompt_toolkit())):
|
||||
if (not ON_WINDOWS or 'prompt_toolkit1' != stype):
|
||||
return modified_style
|
||||
for token, idx, rgb in _get_color_indexes(style_map):
|
||||
if idx == 7 and rgb:
|
||||
|
|
Loading…
Add table
Reference in a new issue