mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-05 00:41:00 +01:00
added singleline() interface
This commit is contained in:
parent
a432960cb3
commit
3055b891f2
3 changed files with 37 additions and 24 deletions
|
@ -117,6 +117,11 @@ class BaseShell(object):
|
|||
self.need_more_lines = False
|
||||
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):
|
||||
"""Called just before execution of line."""
|
||||
return line if self.need_more_lines else line.lstrip()
|
||||
|
|
|
@ -38,35 +38,39 @@ class PromptToolkitShell(BaseShell):
|
|||
enable_open_in_editor=True)
|
||||
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):
|
||||
"""Enters a loop that reads and execute input from user."""
|
||||
if intro:
|
||||
print(intro)
|
||||
_auto_suggest = AutoSuggestFromHistory()
|
||||
auto_suggest = AutoSuggestFromHistory()
|
||||
while not builtins.__xonsh_exit__:
|
||||
try:
|
||||
token_func, style_cls = self._get_prompt_tokens_and_style()
|
||||
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)
|
||||
line = self.singleline(auto_suggest=auto_suggest)
|
||||
if not line:
|
||||
self.emptyline()
|
||||
else:
|
||||
|
|
|
@ -101,6 +101,10 @@ class ReadlineShell(BaseShell, Cmd):
|
|||
def __del__(self):
|
||||
teardown_readline()
|
||||
|
||||
def singleline(self, **kwargs):
|
||||
"""Reads a single line of input."""
|
||||
return input(self.prompt)
|
||||
|
||||
def parseline(self, line):
|
||||
"""Overridden to no-op."""
|
||||
return '', line, line
|
||||
|
@ -198,7 +202,7 @@ class ReadlineShell(BaseShell, Cmd):
|
|||
if inserter is not None:
|
||||
readline.set_pre_input_hook(inserter)
|
||||
try:
|
||||
line = input(self.prompt)
|
||||
line = self.singleline()
|
||||
except EOFError:
|
||||
if builtins.__xonsh_env__.get("IGNOREEOF"):
|
||||
self.stdout.write('Use "exit" to leave the shell.'
|
||||
|
|
Loading…
Add table
Reference in a new issue