def main(): eventloop = create_eventloop() done = [False] # Non local def on_read_start(cli): """ This function is called when we start reading at the input. (Actually the start of the read-input event loop.) """ # Following function should be run in the background. # We do it by using an executor thread from the `CommandLineInterface` # instance. def run(): # Send every second a redraw request. while not done[0]: time.sleep(1) cli.request_redraw() cli.eventloop.run_in_executor(run) def on_read_end(cli): done[0] = True app = Application(layout=Window( BufferControl(input_processors=[BeforeInput(_clock_tokens)])), on_start=Callback(on_read_start), on_stop=Callback(on_read_end)) cli = CommandLineInterface(application=app, eventloop=eventloop) code_obj = cli.run() print('You said: %s' % code_obj.text) eventloop.close()
def _create_application(self): """ Create CommandLineInterface instance. """ # Create Vi command buffer. def handle_action(cli, buffer): ' When enter is pressed in the Vi command line. ' text = buffer.text # Remember: leave_command_mode resets the buffer. # First leave command mode. We want to make sure that the working # pane is focussed again before executing the command handlers. self.leave_command_mode(append_to_history=True) # Execute command. handle_command(self, text) # Create history and search buffers. commands_history = FileHistory( os.path.join(self.config_directory, 'commands_history')) command_buffer = Buffer( accept_action=AcceptAction(handler=handle_action), enable_history_search=Always(), completer=create_command_completer(self), history=commands_history) search_buffer_history = FileHistory( os.path.join(self.config_directory, 'search_history')) search_buffer = Buffer(history=search_buffer_history, enable_history_search=Always(), accept_action=AcceptAction.IGNORE) # Create app. # Create CLI. application = Application( layout=self.editor_layout.layout, key_bindings_registry=self.key_bindings_manager.registry, buffers={ COMMAND_BUFFER: command_buffer, SEARCH_BUFFER: search_buffer, }, get_style=lambda: self.current_style, paste_mode=Condition(lambda cli: self.paste_mode), ignore_case=Condition(lambda cli: self.ignore_case), use_alternate_screen=True, on_abort=AbortAction.IGNORE, on_exit=AbortAction.IGNORE, on_buffer_changed=Callback(self._current_buffer_changed)) # Handle command line previews. # (e.g. when typing ':colorscheme blue', it should already show the # preview before pressing enter.) def preview(): if self.cli.current_buffer == command_buffer: self.previewer.preview(command_buffer.text) command_buffer.on_text_changed += preview return application
def create_application(self): """ Create an `Application` instance for use in a `CommandLineInterface`. """ buffers = { 'docstring': Buffer(read_only=True), } buffers.update(self._extra_buffers or {}) return Application( layout=create_layout( self, self.key_bindings_manager, lexer=self._lexer, input_buffer_height=self._input_buffer_height, extra_buffer_processors=self._extra_buffer_processors, extra_body=self._extra_layout_body, extra_toolbars=self._extra_toolbars), buffer=self._create_buffer(), buffers=buffers, key_bindings_registry=self.key_bindings_registry, paste_mode=Condition(lambda cli: self.paste_mode), mouse_support=Condition(lambda cli: self.enable_mouse_support), on_abort=AbortAction.RETRY, on_exit=self._on_exit, style=DynamicStyle(lambda: self._current_style), get_title=lambda: self.terminal_title, on_start=self._on_start, on_input_timeout=Callback(self._on_input_timeout))
def __init__(self, *a, **kw): self._startup_paths = kw.pop('startup_paths', None) kw.update({ '_accept_action': AcceptAction.run_in_terminal( handler=self._process_document, render_cli_done=True), '_on_start': Callback(self._on_start), '_on_exit': AbortAction.RETURN_NONE, }) super(PythonRepl, self).__init__(*a, **kw)
def create_application(self, completer, history, display_completions_in_columns): self.key_manager = self.create_key_manager() toolbar = Toolbar(lambda: self.model_completer.match_fuzzy, lambda: self.enable_vi_bindings, lambda: self.show_completion_columns, lambda: self.show_help) style_factory = StyleFactory(self.theme) buffers = {'clidocs': Buffer(read_only=True)} return Application( layout=self.create_layout(display_completions_in_columns, toolbar), mouse_support=False, style=style_factory.style, buffers=buffers, buffer=self.create_buffer(completer, history), on_abort=AbortAction.RETRY, on_exit=AbortAction.RAISE_EXCEPTION, on_input_timeout=Callback(self.on_input_timeout), key_bindings_registry=self.key_manager.manager.registry, )
def create_history_application(python_input, original_document): """ Create an `Application` for the history screen. This has to be run as a sub application of `python_input`. When this application runs and returns, it retuns the selected lines. """ history_mapping = HistoryMapping(python_input.history, original_document) def default_buffer_pos_changed(): """ When the cursor changes in the default buffer. Synchronize with history buffer. """ # Only when this buffer has the focus. if application.focus_stack.current == DEFAULT_BUFFER: try: line_no = default_buffer.document.cursor_position_row - \ history_mapping.result_line_offset if line_no < 0: # When the cursor is above the inserted region. raise IndexError history_lineno = sorted( history_mapping.selected_lines)[line_no] except IndexError: pass else: history_buffer.cursor_position = \ history_buffer.document.translate_row_col_to_index(history_lineno, 0) def history_buffer_pos_changed(): """ When the cursor changes in the history buffer. Synchronize. """ # Only when this buffer has the focus. if application.focus_stack.current == HISTORY_BUFFER: line_no = history_buffer.document.cursor_position_row if line_no in history_mapping.selected_lines: default_lineno = sorted(history_mapping.selected_lines).index(line_no) + \ history_mapping.result_line_offset default_buffer.cursor_position = \ default_buffer.document.translate_row_col_to_index(default_lineno, 0) history_buffer = Buffer( initial_document=Document(history_mapping.concatenated_history), on_cursor_position_changed=Callback(history_buffer_pos_changed), accept_action=AcceptAction( lambda cli, buffer: cli.set_return_value(default_buffer.document)), read_only=True) default_buffer = Buffer( initial_document=history_mapping.get_new_document(), on_cursor_position_changed=Callback(default_buffer_pos_changed), read_only=True) help_buffer = Buffer(initial_document=Document(HELP_TEXT, 0), accept_action=AcceptAction.IGNORE, read_only=True) application = Application( layout=create_layout(python_input, history_mapping), use_alternate_screen=True, buffers={ HISTORY_BUFFER: history_buffer, DEFAULT_BUFFER: default_buffer, HELP_BUFFER: help_buffer, }, initial_focussed_buffer=HISTORY_BUFFER, style=python_input._current_style, mouse_support=Condition(lambda cli: python_input.enable_mouse_support), key_bindings_registry=create_key_bindings(python_input, history_mapping)) return application