rprompt implementation

This commit is contained in:
Anthony Scopatz 2016-02-21 13:00:01 -05:00
parent 1b57e21cc2
commit c9ebd33abf
5 changed files with 34 additions and 0 deletions

View file

@ -23,6 +23,8 @@ Current Developments
* ``?`` and ``??`` operator output now has colored titles, like in IPython.
* ``??`` will syntax highlight source code if pygments is available.
* Python mode output is now syntax highlighted if pygments is available.
* New ``$RIGHT_PROMPT`` environment variable for displaying right-aligned
text in prompt-toolkit shell.
**Changed:**

View file

@ -79,6 +79,7 @@ DEFAULT_ENSURERS = {
re.compile('\w*PATH$'): (is_env_path, str_to_env_path, env_path_to_str),
'PATHEXT': (is_env_path, str_to_env_path, env_path_to_str),
'RAISE_SUBPROC_ERROR': (is_bool, to_bool, bool_to_str),
'RIGHT_PROMPT': (is_string, ensure_string, ensure_string),
'TEEPTY_PIPE_DELAY': (is_float, float, str),
'XONSHRC': (is_env_path, str_to_env_path, env_path_to_str),
'XONSH_COLOR_STYLE': (is_string, ensure_string, ensure_string),
@ -181,6 +182,7 @@ DEFAULT_VALUES = {
'PUSHD_MINUS': False,
'PUSHD_SILENT': False,
'RAISE_SUBPROC_ERROR': False,
'RIGHT_PROMPT': '',
'SHELL_TYPE': 'best',
'SUGGEST_COMMANDS': True,
'SUGGEST_MAX_NUM': 5,
@ -349,6 +351,10 @@ DEFAULT_DOCS = {
'This is most useful in xonsh scripts or modules where failures '
'should cause an end to execution. This is less useful at a terminal.'
'The error that is raised is a subprocess.CalledProcessError.'),
'RIGHT_PROMPT': VarDocs('Template string for right-aligned text '
'at the prompt. This may be parameterized in the same way as '
'the $PROMPT variable. Currently, this is only available in the '
'prompt-toolkit shell.'),
'SHELL_TYPE': VarDocs(
'Which shell is used. Currently two base shell types are supported:\n\n'
" - 'readline' that is backed by Python's readline module\n"

View file

@ -63,6 +63,7 @@ class PromptToolkitShell(BaseShell):
mouse_support=mouse_support,
auto_suggest=auto_suggest,
get_prompt_tokens=self.prompt_tokens,
get_rprompt_tokens=self.rprompt_tokens,
style=PygmentsStyle(xonsh_style_proxy(self.styler)),
completer=completer,
lexer=PygmentsLexer(XonshLexer),
@ -127,6 +128,20 @@ class PromptToolkitShell(BaseShell):
self.settitle()
return toks
def rprompt_tokens(self, cli):
"""Returns a list of (token, str) tuples for the current right
prompt.
"""
p = builtins.__xonsh_env__.get('RIGHT_PROMPT')
if len(p) == 0:
return []
try:
p = partial_format_prompt(p)
except Exception: # pylint: disable=broad-except
print_exception()
toks = partial_color_tokenize(p)
return toks
def format_color(self, string, **kwargs):
"""Formats a color string using Pygments. This, therefore, returns
a list of (Token, str) tuples.

View file

@ -4,6 +4,8 @@ from prompt_toolkit.utils import DummyContext
from prompt_toolkit.shortcuts import (create_prompt_application,
create_eventloop, create_asyncio_eventloop, create_output)
from xonsh.shell import prompt_toolkit_version_info
class Prompter(object):
def __init__(self, cli=None, *args, **kwargs):
@ -18,6 +20,7 @@ class Prompter(object):
will be created when the prompt() method is called.
"""
self.cli = cli
self.major_minor = prompt_toolkit_version_info()[:2]
def __enter__(self):
self.reset()
@ -61,6 +64,8 @@ class Prompter(object):
# Create CommandLineInterface.
if self.cli is None:
if self.major_minor <= (0, 57):
kwargs.pop('get_rprompt_tokens', None)
cli = CommandLineInterface(
application=create_prompt_application(message, **kwargs),
eventloop=eventloop,

View file

@ -33,6 +33,12 @@ def prompt_toolkit_version():
return getattr(prompt_toolkit, '__version__', '<0.57')
def prompt_toolkit_version_info():
"""Gets the prompt toolkit version info tuple."""
v = prompt_toolkit_version().strip('<>+-=.')
return tuple(map(int, v.split('.')))
def best_shell_type():
"""Gets the best shell type that is available"""
if ON_WINDOWS or is_prompt_toolkit_available():