def _create_windows(self): """Create all the windows of the display (input, output, debugger, text completer)""" module_names = [m.name for m in self.model.modules] search_field = SearchToolbar() # Generate the input text area input_field = TextArea(prompt='> ', style='class:arrow', completer=ModuleCompleter(module_names), search_field=search_field, height=1, multiline=False, wrap_lines=True) # Field to show current time end_time = str(self.model.get_end_time()) time_field = TextArea(text="", style='class:rprompt', height=1, width=len(end_time) * 2 + 7, multiline=False) output = Label(text="") self.display.update() # Create container with display window and input text area container = pt_containers.HSplit([ self.display.get_top_view(), pt_containers.Window(height=1, char='-'), output, pt_containers.VSplit([input_field, time_field]), search_field ]) # Floating menu for text completion completion_menu = CompletionsMenu(max_height=5, scroll_offset=1) body = pt_containers.FloatContainer(content=container, floats=[ pt_containers.Float( xcursor=True, ycursor=True, content=completion_menu) ]) return body, input_field, time_field, output
buffer_control = controls.BufferControl(buffer=buffer) content_display = controls.FormattedTextControl(text="hello world") current_display = controls.FormattedTextControl(text="") layout = Layout( containers.HSplit([ containers.Window(header, height=1, style="reverse"), containers.FloatContainer( containers.HSplit([ containers.Window(content=buffer_control, height=1), containers.Window(height=1, char="-", style="class:line"), containers.Window(content=current_display, height=1), containers.Window(height=1, char="-", style="class:line"), containers.Window(content=content_display), ]), floats=[ containers.Float( xcursor=True, ycursor=True, content=menus.CompletionsMenu(max_height=12, scroll_offset=1), ) ], ), ValidationToolbar(), ])) kb = KeyBindings() @kb.add("c-g")
completer=animal_completer, ) header = controls.FormattedTextControl(top_text) display = controls.FormattedTextControl(text="hello world") layout = Layout( containers.HSplit([ containers.Window(header, height=1, style="reverse"), containers.FloatContainer( containers.HSplit([ containers.Window(content=controls.BufferControl( buffer=buffer)) ]), floats=[ containers.Float( xcursor=True, ycursor=True, content=menus.CompletionsMenu(max_height=16, scroll_offset=1), ) ], ), containers.Window(height=1, char="-", style="class:line"), containers.Window(content=display), ])) kb = KeyBindings() @kb.add("c-g") @kb.add("c-c")
def __init__( self, switchboard_inst: switchboard_base.SwitchboardBase, device_log_file_name: str, configuration: console_config.ConsoleConfiguration, console_input: Optional[prompt_toolkit_input.Input] = None, console_output: Optional[output.Output] = None) -> None: """Initializes a prompt_toolkit console application. Args: switchboard_inst: Switchboard capability instance. device_log_file_name: The device log file to read (stream) logs from. configuration: Console configuration. console_input: Console input. Defaults to stdin. console_output: Console output. Defaults to stdout. """ self._device_log_file_name = device_log_file_name self._window_line_transforms = configuration.window_line_transforms self._line_to_window_id = configuration.line_to_window_id self._log_file = None # Set by run(). body_frames = [] self._window_id_to_text_area = collections.OrderedDict() for window in configuration.windows: text_area, enclosing_frame = _make_ui_window(window, switchboard_inst) self._window_id_to_text_area[window.window_id] = text_area body_frames.append(enclosing_frame) self._menu_exit_item = widgets.MenuItem("Exit", handler=_do_exit) if body_frames: self._default_focused_element = body_frames[0] else: self._default_focused_element = self._menu_exit_item help_window_visibility = _HelpWindowVisibility( focus_element_on_hide=self._default_focused_element) close_help_button = widgets.Button( text="Ok", handler=help_window_visibility.hide) help_window_visibility.focus_element_on_show = close_help_button help_float_dialog = containers.Float( containers.ConditionalContainer( content=widgets.Dialog( title="Console help", body=widgets.TextArea( text=_HELP_TEXT, focusable=True, read_only=True, scrollbar=True, width=_HELP_TEXT_WIDTH, ), buttons=[close_help_button], ), filter=filters.Condition(help_window_visibility.is_visible), ), ) root_container = widgets.MenuContainer( body=containers.FloatContainer( content=containers.HSplit(body_frames), floats=[help_float_dialog], ), menu_items=[ self._menu_exit_item, widgets.MenuItem("Help", handler=help_window_visibility.show), ], ) button_list = switchboard_inst.button_list button_names = button_list[0].valid_buttons() if button_list else [] if button_names: button_menu_items = [ _make_button_menu_item(button_name, switchboard_inst, root_container) for button_name in button_names ] self._button_dropdown = widgets.MenuItem( "Buttons", children=button_menu_items) root_container.menu_items.append(self._button_dropdown) else: self._button_dropdown = None # Global key bindings. bindings = key_binding.KeyBindings() bindings.add("tab")(focus.focus_next) bindings.add("s-tab")(focus.focus_previous) style = styles.Style.from_dict({ "menu-bar": "bg:#aaaaaa #888888", }) self._application = application.Application( layout=layout.Layout( container=root_container, focused_element=self._default_focused_element, ), style=style, full_screen=True, key_bindings=bindings, editing_mode=enums.EditingMode.VI, input=console_input, output=console_output, )
def _make_ui_window( window: console_config.WindowType, switchboard_inst: switchboard_base.SwitchboardBase ) -> Tuple[widgets.TextArea, widgets.Frame]: """Creates a text area and an enclosing frame. Args: window: Console window description. switchboard_inst: Switchboard capability instance. Returns: A tuple containing the window's text area and its enclosing UI frame. """ if isinstance(window, console_config.CommandInputWindow): def handle_command_input(input_buffer: buffer.Buffer) -> None: command = input_buffer.text switchboard_inst.send(command, port=window.transport) command_input_area = widgets.TextArea( text="", multiline=False, accept_handler=handle_command_input, history=None, focusable=True, read_only=False, get_line_prefix=lambda line_num, wrap_count: " > " ) command_input_area_bindings = key_binding.KeyBindings() @command_input_area_bindings.add("c-c") def _handle_control_c(event) -> None: # pylint: disable=unused-variable del event # Unused. command_input_area.text = "" if window.reopen_transport_on_ctrl_c: switchboard_inst.add_log_note( "The transport is running in non-interactive mode. " "Ctrl-C keystrokes are ignored by the device. " "Reopening the command transport as a workaround. " "Shell environment variables will be lost.") switchboard_inst.close_transport(port=window.transport) switchboard_inst.open_transport(port=window.transport) else: switchboard_inst.send(b"\x03", port=window.transport) command_input_area.control.key_bindings = command_input_area_bindings command_input_frame = widgets.Frame( body=command_input_area, title=window.name) return command_input_area, command_input_frame else: # CommandResponseWindow or LogWindow search_toolbar = widgets.SearchToolbar() text_area = _AppendableTextArea( text="", focusable=True, read_only=True, line_numbers=True, scrollbar=True, search_field=search_toolbar, ) text_area.window.ignore_content_height = lambda: True frame = widgets.Frame( body=containers.HSplit([ text_area, search_toolbar, ]), title=window.name) return text_area, frame
def create(self): self.buffer = pt.buffer.Buffer( accept_action=pt.buffer.AcceptAction(handler=self.handle_command), history=self.history, completer=CommandCompleter(self.valid_commands), on_cursor_position_changed=self.handle_help_bar, ) self.buffers = {'PROMPT': self.buffer} for i in range(10): self.buffers['FN%d' % i] = pt.buffer.Buffer( accept_action=pt.buffer.AcceptAction(handler=self.save_state), history=None, completer=None) divider_line = lambda: containers.Window( content=controls.FillControl('-', token=Token.Line), height=dimension.LayoutDimension.exact(1)) buffer_sublayout = containers.VSplit([ # containers.Window(content=controls.TokenListControl(get_tokens=lambda cli : ([Token.Red, u'> ']))), self.text_block(lambda x: [(Token.Prompt, u">> ")], dont_extend_width=True), containers.Window( content=controls.BufferControl(buffer_name='PROMPT'), dont_extend_height=True), ]) self.main_layout = containers.HSplit([ divider_line(), self.text_block(self.test_mode), self.text_block(self.test_settings), divider_line(), self.text_block(self.turn_table_settings), divider_line(), self.text_block(self.save_location_setting), divider_line(), buffer_sublayout, containers.Window(content=controls.TokenListControl( get_tokens=self.status_bar)), ]) self.save_load_layout = containers.HSplit( [divider_line()] + [self.file_selection(i) for i in range(10)] + [ divider_line(), containers.Window(content=controls.TokenListControl( get_tokens=self.status_bar)), ]) self.update_state_name_display() loop = pt.shortcuts.create_eventloop() application = pt.application.Application( layout=self.main_layout, buffers=self.buffers, style=self.style, initial_focussed_buffer='PROMPT', key_bindings_registry=registry) cli = pt.interface.CommandLineInterface(application=application, eventloop=loop) cli.state = "MAIN" self.init_key_bindings() cli.run()
def window_rows(windows): return containers.HSplit(windows)
def on_text_changed(buf): global display if buf.text != display.text: display.text = buf.text top_text = "Press C-c to quit" buffer = Buffer(on_text_changed=on_text_changed) header = controls.FormattedTextControl(top_text) display = controls.FormattedTextControl(text="hello world") layout = Layout( containers.HSplit([ containers.Window(header, height=1, style="reverse"), containers.Window(content=controls.BufferControl(buffer=buffer)), containers.Window(content=display), ])) kb = KeyBindings() @kb.add("c-c") def exit_(event): event.app.exit() app = Application(layout=layout, full_screen=True, key_bindings=kb) app.run()