def interactive(self): history = _pt_history.InMemoryHistory() cmd_list = [cmd['command'] for cmd in self.CMDS] completion = _completers.WordCompleter( cmd_list, ignore_case=True, ) while True: try: text = _pt.prompt(u'> ', style=DocumentStyle, history=history, completer=completion) if text: splitted = text.strip().split() cmd = splitted.pop(0) if cmd in cmd_list: command = getattr(self, cmd) command(*splitted) if text == 'exit': break except TypeError: print command.__doc__ except KeyboardInterrupt: pass except EOFError: break print "Bye!"
def _CreatePromptApplication(self): """Creates a shell prompt Application.""" # Make sure that complete_while_typing is disabled when # enable_history_search is enabled. (First convert to SimpleFilter, to # avoid doing bitwise operations on bool objects.) complete_while_typing = shortcuts.to_simple_filter(True) enable_history_search = shortcuts.to_simple_filter(False) multiline = shortcuts.to_simple_filter(False) complete_while_typing &= ~enable_history_search return application.Application( layout=layout.CreatePromptLayout( message=self.prompt, lexer=None, is_password=False, reserve_space_for_menu=self.MENU_RESERVE_SPACE, multiline=filters.Condition(lambda cli: multiline()), get_prompt_tokens=None, get_continuation_tokens=None, get_bottom_toolbar_tokens=self._GetBottomToolbarTokens, display_completions_in_columns=False, extra_input_processors=None, wrap_lines=True, show_help=filters.Condition( lambda _: self.key_bindings.help_key.toggle)), buffer=pt_buffer.Buffer( enable_history_search=enable_history_search, complete_while_typing=complete_while_typing, is_multiline=multiline, history=pt_history.InMemoryHistory(), validator=None, completer=shell_completer.ShellCliCompleter(), auto_suggest=None, accept_action=pt_buffer.AcceptAction.RETURN_DOCUMENT, initial_document=document.Document(''), ), style=shell_style.GetDocumentStyle(), clipboard=None, key_bindings_registry=self.key_bindings.MakeRegistry(), get_title=None, mouse_support=False, erase_when_done=False, on_abort=application.AbortAction.RAISE_EXCEPTION, on_exit=application.AbortAction.RAISE_EXCEPTION)
def main(): reset_tty = FixControlQ() try: hist = pt_history.InMemoryHistory() while True: try: text = Prompt(hist) if text is None or text.strip() == 'exit': sys.exit(0) subprocess.call('gcloud ' + text, shell=True) except EOFError: # on ctrl-d, exit loop break except KeyboardInterrupt: # ignore ctrl-c pass finally: # when exitting, restore the tty to its original state. reset_tty()
def CreateCli(gcloud_py_dir): """Creates the CLI application. Args: gcloud_py_dir: str, path to completion lookup table Returns: cli, a cli instance """ completer = ShellCliCompleter(gcloud_py_dir) in_memory_history = history.InMemoryHistory() auto_suggest_from_history = auto_suggest.AutoSuggestFromHistory() key_manager = _KeyBindings() layout = shortcuts.create_prompt_layout( lexer=shell.BashLexer, get_bottom_toolbar_tokens=GetBottomToolbarTokens, message=u'Cloud SDK {0}> '.format(config.CLOUD_SDK_VERSION)) cli_buffer = ptkbuffer.Buffer( history=in_memory_history, auto_suggest=auto_suggest_from_history, complete_while_typing=True, completer=completer, accept_action=interface.AcceptAction.RETURN_DOCUMENT) application = Application( style=GetDocumentStyle(), buffer=cli_buffer, layout=layout, key_bindings_registry=key_manager.registry, mouse_support=True) cli = interface.CommandLineInterface( application=application, eventloop=shortcuts.create_eventloop()) return cli
def CreatePromptApplication( message='', multiline=False, wrap_lines=True, is_password=False, complete_while_typing=True, enable_history_search=False, lexer=None, enable_system_bindings=False, enable_open_in_editor=False, validator=None, completer=None, reserve_space_for_menu=5, auto_suggest=None, style=None, history=None, clipboard=None, get_prompt_tokens=None, get_continuation_tokens=None, get_bottom_toolbar_tokens=None, display_completions_in_columns=False, get_title=None, mouse_support=False, extra_input_processors=None, key_bindings_registry=None, on_abort=application.AbortAction.RAISE_EXCEPTION, on_exit=application.AbortAction.RAISE_EXCEPTION, accept_action=pt_buffer.AcceptAction.RETURN_DOCUMENT, erase_when_done=False, default='', get_help_tokens=None): """Create the shell prompt Application.""" if key_bindings_registry is None: key_bindings_registry = manager.KeyBindingManager.for_prompt( enable_system_bindings=enable_system_bindings, enable_open_in_editor=enable_open_in_editor).registry # Make sure that complete_while_typing is disabled when enable_history_search # is enabled. (First convert to SimpleFilter, to avoid doing bitwise # operations on bool objects.) complete_while_typing = shortcuts.to_simple_filter(complete_while_typing) enable_history_search = shortcuts.to_simple_filter(enable_history_search) multiline = shortcuts.to_simple_filter(multiline) complete_while_typing &= ~enable_history_search # Create application return application.Application( layout=layout.CreatePromptLayout( message=message, lexer=lexer, is_password=is_password, reserve_space_for_menu=(reserve_space_for_menu if completer is not None else 0), multiline=filters.Condition(lambda cli: multiline()), get_prompt_tokens=get_prompt_tokens, get_continuation_tokens=get_continuation_tokens, get_bottom_toolbar_tokens=get_bottom_toolbar_tokens, display_completions_in_columns=display_completions_in_columns, extra_input_processors=extra_input_processors, wrap_lines=wrap_lines, get_help_tokens=get_help_tokens, show_help=filters.Condition(lambda _: SHOW_HELP_WINDOW)), buffer=pt_buffer.Buffer( enable_history_search=enable_history_search, complete_while_typing=complete_while_typing, is_multiline=multiline, history=(history or pt_history.InMemoryHistory()), validator=validator, completer=completer, auto_suggest=auto_suggest, accept_action=accept_action, initial_document=document.Document(default), ), style=style, clipboard=clipboard, key_bindings_registry=key_bindings_registry, get_title=get_title, mouse_support=mouse_support, erase_when_done=erase_when_done, on_abort=on_abort, on_exit=on_exit)