added singleline() interface

This commit is contained in:
Anthony Scopatz 2015-12-31 00:48:49 -08:00
parent a432960cb3
commit 3055b891f2
3 changed files with 37 additions and 24 deletions

View file

@ -117,6 +117,11 @@ class BaseShell(object):
self.need_more_lines = False self.need_more_lines = False
self.default('') self.default('')
def singleline(self, **kwargs):
"""Reads a single line of input from the shell."""
msg = '{0} has not implemented singleline().'
raise RuntimeError(msg.format(self.__class__.__name__))
def precmd(self, line): def precmd(self, line):
"""Called just before execution of line.""" """Called just before execution of line."""
return line if self.need_more_lines else line.lstrip() return line if self.need_more_lines else line.lstrip()

View file

@ -38,35 +38,39 @@ class PromptToolkitShell(BaseShell):
enable_open_in_editor=True) enable_open_in_editor=True)
load_xonsh_bindings(self.key_bindings_manager) load_xonsh_bindings(self.key_bindings_manager)
def singleline(self, auto_suggest=None, enable_history_search=True,
**kwargs):
"""Reads a single line of input from the shell."""
token_func, style_cls = self._get_prompt_tokens_and_style()
env = builtins.__xonsh_env__
mouse_support = env.get('MOUSE_SUPPORT')
auto_suggest = auto_suggest if env.get('AUTO_SUGGEST') else None
completions_display = env.get('COMPLETIONS_DISPLAY')
multicolumn = (completions_display == 'multi')
completer = None if completions_display == 'none' else self.pt_completer
with self.prompter:
line = self.prompter.prompt(
mouse_support=mouse_support,
auto_suggest=auto_suggest,
get_prompt_tokens=token_func,
style=style_cls,
completer=completer,
lexer=PygmentsLexer(XonshLexer),
history=self.history,
enable_history_search=enable_history_search,
reserve_space_for_menu=0,
key_bindings_registry=self.key_bindings_manager.registry,
display_completions_in_columns=multicolumn)
return line
def cmdloop(self, intro=None): def cmdloop(self, intro=None):
"""Enters a loop that reads and execute input from user.""" """Enters a loop that reads and execute input from user."""
if intro: if intro:
print(intro) print(intro)
_auto_suggest = AutoSuggestFromHistory() auto_suggest = AutoSuggestFromHistory()
while not builtins.__xonsh_exit__: while not builtins.__xonsh_exit__:
try: try:
token_func, style_cls = self._get_prompt_tokens_and_style() line = self.singleline(auto_suggest=auto_suggest)
mouse_support = builtins.__xonsh_env__.get('MOUSE_SUPPORT')
if builtins.__xonsh_env__.get('AUTO_SUGGEST'):
auto_suggest = _auto_suggest
else:
auto_suggest = None
completions_display = builtins.__xonsh_env__.get('COMPLETIONS_DISPLAY')
multicolumn = (completions_display == 'multi')
completer = None if completions_display == 'none' else self.pt_completer
with self.prompter:
line = self.prompter.prompt(
mouse_support=mouse_support,
auto_suggest=auto_suggest,
get_prompt_tokens=token_func,
style=style_cls,
completer=completer,
lexer=PygmentsLexer(XonshLexer),
history=self.history,
enable_history_search=True,
reserve_space_for_menu=0,
key_bindings_registry=self.key_bindings_manager.registry,
display_completions_in_columns=multicolumn)
if not line: if not line:
self.emptyline() self.emptyline()
else: else:

View file

@ -101,6 +101,10 @@ class ReadlineShell(BaseShell, Cmd):
def __del__(self): def __del__(self):
teardown_readline() teardown_readline()
def singleline(self, **kwargs):
"""Reads a single line of input."""
return input(self.prompt)
def parseline(self, line): def parseline(self, line):
"""Overridden to no-op.""" """Overridden to no-op."""
return '', line, line return '', line, line
@ -198,7 +202,7 @@ class ReadlineShell(BaseShell, Cmd):
if inserter is not None: if inserter is not None:
readline.set_pre_input_hook(inserter) readline.set_pre_input_hook(inserter)
try: try:
line = input(self.prompt) line = self.singleline()
except EOFError: except EOFError:
if builtins.__xonsh_env__.get("IGNOREEOF"): if builtins.__xonsh_env__.get("IGNOREEOF"):
self.stdout.write('Use "exit" to leave the shell.' self.stdout.write('Use "exit" to leave the shell.'