From 3055b891f2d1adab619b682ccd09ce70ebf55385 Mon Sep 17 00:00:00 2001 From: Anthony Scopatz Date: Thu, 31 Dec 2015 00:48:49 -0800 Subject: [PATCH] added singleline() interface --- xonsh/base_shell.py | 5 +++++ xonsh/ptk/shell.py | 50 ++++++++++++++++++++++------------------- xonsh/readline_shell.py | 6 ++++- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/xonsh/base_shell.py b/xonsh/base_shell.py index 807f78e41..571454c93 100644 --- a/xonsh/base_shell.py +++ b/xonsh/base_shell.py @@ -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() diff --git a/xonsh/ptk/shell.py b/xonsh/ptk/shell.py index 22223285c..21c56c894 100644 --- a/xonsh/ptk/shell.py +++ b/xonsh/ptk/shell.py @@ -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: diff --git a/xonsh/readline_shell.py b/xonsh/readline_shell.py index ad7112b36..feab0d0cb 100644 --- a/xonsh/readline_shell.py +++ b/xonsh/readline_shell.py @@ -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.'