mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-05 17:00:58 +01:00
Merge pull request #1026 from adqm/lazily_load_shells
Lazily load prompt_toolkit
This commit is contained in:
commit
80f5b0b2eb
3 changed files with 19 additions and 19 deletions
|
@ -93,13 +93,12 @@ def ptk_version_info():
|
|||
return None
|
||||
|
||||
|
||||
BEST_SHELL_TYPE = None
|
||||
""" The 'best' available shell type, either 'prompt_toollit' or 'readline'. """
|
||||
|
||||
if ON_WINDOWS or has_prompt_toolkit():
|
||||
BEST_SHELL_TYPE = 'prompt_toolkit'
|
||||
else:
|
||||
BEST_SHELL_TYPE = 'readline'
|
||||
@lru_cache(1)
|
||||
def best_shell_type():
|
||||
if ON_WINDOWS or has_prompt_toolkit():
|
||||
return 'prompt_toolkit'
|
||||
else:
|
||||
return 'readline'
|
||||
|
||||
|
||||
@lru_cache(1)
|
||||
|
|
|
@ -7,7 +7,7 @@ from warnings import warn
|
|||
from xonsh import xontribs
|
||||
from xonsh.environ import xonshrc_context
|
||||
from xonsh.execer import Execer
|
||||
from xonsh.platform import (BEST_SHELL_TYPE, has_prompt_toolkit, ptk_version,
|
||||
from xonsh.platform import (best_shell_type, has_prompt_toolkit, ptk_version,
|
||||
ptk_version_info)
|
||||
from xonsh.tools import XonshError
|
||||
|
||||
|
@ -44,17 +44,16 @@ class Shell(object):
|
|||
kwargs.get('cacheall', False))
|
||||
env = builtins.__xonsh_env__
|
||||
# pick a valid shell
|
||||
if shell_type is not None:
|
||||
env['SHELL_TYPE'] = shell_type
|
||||
shell_type = env.get('SHELL_TYPE')
|
||||
if shell_type == 'best' or shell_type is None:
|
||||
shell_type = BEST_SHELL_TYPE
|
||||
shell_type = best_shell_type()
|
||||
elif shell_type == 'random':
|
||||
shell_type = random.choice(('readline', 'prompt_toolkit'))
|
||||
if shell_type == 'prompt_toolkit':
|
||||
if not has_prompt_toolkit():
|
||||
warn('prompt_toolkit is not available, using readline instead.')
|
||||
shell_type = env['SHELL_TYPE'] = 'readline'
|
||||
shell_type = 'readline'
|
||||
env['SHELL_TYPE'] = shell_type
|
||||
# actually make the shell
|
||||
if shell_type == 'none':
|
||||
from xonsh.base_shell import BaseShell as shell_class
|
||||
|
|
|
@ -36,11 +36,6 @@ from collections import OrderedDict, Sequence, Set
|
|||
from xonsh.platform import (has_prompt_toolkit, scandir, win_unicode_console,
|
||||
DEFAULT_ENCODING, ON_LINUX, ON_WINDOWS,
|
||||
PYTHON_VERSION_INFO)
|
||||
if has_prompt_toolkit():
|
||||
import prompt_toolkit
|
||||
else:
|
||||
prompt_toolkit = None
|
||||
|
||||
|
||||
IS_SUPERUSER = ctypes.windll.shell32.IsUserAnAdmin() != 0 if ON_WINDOWS else os.getuid() == 0
|
||||
|
||||
|
@ -861,6 +856,7 @@ def color_style():
|
|||
|
||||
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:
|
||||
|
@ -882,7 +878,10 @@ def intensify_colors_for_cmd_exe(style_map, replace_colors=None):
|
|||
range used by the gray colors
|
||||
"""
|
||||
modified_style = {}
|
||||
if not ON_WINDOWS or prompt_toolkit is None:
|
||||
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())):
|
||||
return modified_style
|
||||
if replace_colors is None:
|
||||
replace_colors = {1: '#44ffff', # subst blue with bright cyan
|
||||
|
@ -905,7 +904,10 @@ def expand_gray_colors_for_cmd_exe(style_map):
|
|||
in cmd.exe.
|
||||
"""
|
||||
modified_style = {}
|
||||
if not ON_WINDOWS or prompt_toolkit is None:
|
||||
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())):
|
||||
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