def run(app): eventloop = shortcuts.create_eventloop() try: cli = interface.CommandLineInterface(application=app, eventloop=eventloop) cli.run(reset_current_buffer=False) finally: eventloop.close()
def __init__(self, prompt='$ ', cosh=None): self.prompt = prompt self.coshell = cosh self.key_bindings = bindings.KeyBindings( edit_mode=self.coshell.edit_mode == 'emacs') # Create the CLI. self.cli = interface.CommandLineInterface( application=self._CreatePromptApplication(), eventloop=shortcuts.create_eventloop(), output=shortcuts.create_output()) self.key_bindings.Initialize(self.cli)
def build_cli(self): history = pt_history.FileHistory( os.path.expanduser('~/.edgedbhistory')) key_binding_manager = pt_keymanager.KeyBindingManager( enable_system_bindings=True, enable_search=True, enable_abort_and_exit_bindings=True) @key_binding_manager.registry.add_binding(pt_keys.Keys.F3) def _graphql_toggle(event): self.graphql = not self.graphql @key_binding_manager.registry.add_binding(pt_keys.Keys.Tab) def _tab(event): b = cli.current_buffer before_cursor = b.document.current_line_before_cursor if b.text and (not before_cursor or before_cursor.isspace()): b.insert_text(' ') layout = pt_shortcuts.create_prompt_layout( lexer=pt_lexers.PygmentsLexer(eql_pygments.EdgeQLLexer), reserve_space_for_menu=4, get_prompt_tokens=self.get_prompt_tokens, get_continuation_tokens=self.get_continuation_tokens, get_bottom_toolbar_tokens=self.get_toolbar_tokens, multiline=True) buf = InputBuffer( history=history, # to make reserve_space_for_menu work: complete_while_typing=pt_filters.Always(), accept_action=pt_app.AcceptAction.RETURN_DOCUMENT) app = pt_app.Application( style=self.style, layout=layout, buffer=buf, ignore_case=True, key_bindings_registry=key_binding_manager.registry, on_exit=pt_app.AbortAction.RAISE_EXCEPTION, on_abort=pt_app.AbortAction.RETRY) cli = pt_interface.CommandLineInterface( application=app, eventloop=self.eventloop) return cli
def RunApplication(app): """Run a prompt_toolkit Application.""" eventloop = shortcuts.create_eventloop() # Create CommandLineInterface. cli = interface.CommandLineInterface(application=app, eventloop=eventloop, output=shortcuts.create_output()) # Note: We pass `reset_current_buffer=False`, because that way it's easy to # give DEFAULT_BUFFER a default value, without it getting erased. We # don't have to reset anyway, because this is the first and only time # that this CommandLineInterface will run. try: result = cli.run(reset_current_buffer=False) if isinstance(result, document.Document): # Backwards-compatibility. return result.text return result finally: eventloop.close()
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