def arrange_ui(self, container): container.children.clear() container.children.extend([ VSplit(self.captures), to_container(self.output_field), self.ui.statusbar, to_container(self.input_field), ])
def test_toggle_back_button(self): """The back button is only visible if there are at least 2 pages.""" first_label = Label("First") second_label = Label("Second") self.window.push(first_label) self.window.push(second_label) children = to_container(self.window).get_children() back_button = children[1] exit_button = to_widget(children[2]) assert exit_button.text == "Exit" # Back button is visible assert to_widget(back_button.content).text == "Back" assert back_button.filter() label = children[0].get_container() assert label.text == "Second" self.window.pop() # Back button is't visible assert to_widget(back_button.content).text == "Back" assert not back_button.filter() label = children[0].get_container() assert label.text == "First"
def _build_layout(self): " Rebuild a new Container object and return that. " logger.info('Rebuilding layout.') if not self.pymux.arrangement.windows: # No Pymux windows in the arrangement. return Window() active_window = self.pymux.arrangement.get_active_window() # When zoomed, only show the current pane, otherwise show all of them. if active_window.zoom: return to_container( _create_container_for_process(self.pymux, active_window, active_window.active_pane, zoom=True)) else: window = self.pymux.arrangement.get_active_window() return HSplit([ # Some spacing for the top status bar. ConditionalContainer( content=Window(height=1), filter=Condition(lambda: self.pymux.enable_pane_status)), # The actual content. _create_split(self.pymux, window, window.root) ])
def is_interactive(self): """Whether this job needs user interaction""" if self.runtime_widget: for c in walk(to_container(self.runtime_widget), skip_hidden=True): if isinstance(c, Window) and c.content.is_focusable(): return True return False
def _setup_app(self): label = Label("", style="bg:black fg:white") cc = ConditionalContainer(label, Condition(lambda: label.text != "")) self.hsplit.children.append(to_container(Label(""))) self.hsplit.children.append(to_container(cc)) self.kb.add("a")(lambda event: event.app.exit( AppResult(app_factory=apply_app_factory))) self.kb.add("p")(lambda event: event.app.exit( AppResult(app_factory=persist_app_factory))) def apply_app_factory(): print("Applying network interface changes...") with self.context.get_client() as c: try: c.call("interface.commit") except ClientException as e: return AppResult( app=message_dialog( "Error", "\n".join( textwrap.wrap(format_error(self.context, e), 74))), app_result_handler=lambda _: NetworkInterfaceList( self.context), ) return NetworkInterfaceList(self.context) def persist_app_factory(): with self.context.get_client() as c: c.call("interface.checkin") return NetworkInterfaceList(self.context) def get_text(): with self.context.get_client() as c: if checkin_waiting := c.call("interface.checkin_waiting"): n = int(checkin_waiting) return ( f"Network interface changes have been applied.\n" f"Press <p> to persist them if the network is still operational\n" f"or they will be rolled back in {n} seconds.") elif c.call("interface.has_pending_changes"): return ("You have pending network interface changes.\n" "Press <a> to apply them.")
def __init__(self, content, width=None, height=None, report_write_position_callback=None): assert is_dimension(width) assert is_dimension(height) assert report_write_position_callback is None or callable(report_write_position_callback) self.content = to_container(content) self.width = width self.height = height self.report_write_position_callback = report_write_position_callback
def test_pop_all_pages(self): first_label = Label("First") self.window.push(first_label) assert len(self.window.pages) == 1 # We can't pop the last page. self.window.pop() self.window.pop() assert len(self.window.pages) == 1 children = to_container(self.window).get_children() label = children[0].get_container() assert label.text == "First" # We can reset the pages to the default container. self.window.reset() assert len(self.window.pages) == 1 children = to_container(self.window).get_children() label = children[0].get_container() assert label.content.text() == "Empty container"
def test_pop_all_pages(self): first_label = Label("First") self.window.reset(first_label) assert len(self.window.pages) == 1 # We can't pop the last page. self.window.pop() self.window.pop() assert len(self.window.pages) == 1 children = to_container(self.window).get_children() label = children[0].get_container() assert label.text == "First" # We can reset the pages to the default container. self.window.reset() assert len(self.window.pages) == 1 children = to_container(self.window).get_children() books_list = children[0].get_container() assert isinstance(books_list, BooksList)
def __init__(self, content, width=None, height=None, report_write_position_callback=None): assert is_dimension(width) assert is_dimension(height) assert report_write_position_callback is None or callable( report_write_position_callback) self.content = to_container(content) self.width = width self.height = height self.report_write_position_callback = report_write_position_callback
def has_focus(value: "FocusableElement") -> Condition: """ Enable when this buffer has the focus. """ from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout import walk from prompt_toolkit.layout.containers import Container, Window, to_container from prompt_toolkit.layout.controls import UIControl if isinstance(value, str): def test() -> bool: return get_app().current_buffer.name == value elif isinstance(value, Buffer): def test() -> bool: return get_app().current_buffer == value elif isinstance(value, UIControl): def test() -> bool: return get_app().layout.current_control == value else: value = to_container(value) if isinstance(value, Window): def test() -> bool: return get_app().layout.current_window == value else: def test() -> bool: # Consider focused when any window inside this container is # focused. current_window = get_app().layout.current_window for c in walk(cast(Container, value)): if isinstance(c, Window) and c == current_window: return True return False @Condition def has_focus_filter() -> bool: return test() return has_focus_filter
def test_default_menu(self): """The default menu has 2 buttons and a label.""" children = to_container(self.window).get_children() assert len(children) == 3 empty_label = children[0].get_container().content back_button = children[1] exit_button = to_widget(children[2]) assert empty_label.text() == "Empty container" # Back button is not visible assert to_widget(back_button.content).text == "Back" assert not back_button.filter() # Exit button is visible assert isinstance(exit_button, Button) assert exit_button.text == "Exit"
def test_default_menu(self): """The default menu has 2 buttons and a BooksList.""" children = to_container(self.window).get_children() assert len(children) == 3 books_list = children[0].get_container() back_button = children[1] exit_button = to_widget(children[2]) assert isinstance(books_list, BooksList) # Back button is not visible assert to_widget(back_button.content).text == "Back" assert not back_button.filter() # Exit button is visible assert isinstance(exit_button, Button) assert exit_button.text == "Exit"
def add_jobs(self, *jobs): """Add :class:`~.jobs.base.JobBase` instances""" for job in jobs: if job.name in self._jobs: raise RuntimeError(f'Job was already added: {job.name}') else: self._jobs[job.name].job = job self._jobs[job.name].widget = jobwidgets.JobWidget(job, self._app) self._jobs[job.name].container = to_container(self._jobs[job.name].widget) # Terminate application if all jobs finished job.signal.register('finished', self._exit_if_all_jobs_finished) # Terminate application if any job finished with non-zero exit code job.signal.register('finished', self._exit_if_job_failed) if self._jobs[job.name].widget.is_interactive: # Display next interactive job when currently focused job finishes job.signal.register('finished', self._update_jobs_container) self._update_jobs_container()
def has_focus(value): """ Enable when this buffer has the focus. """ from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout.controls import UIControl from prompt_toolkit.layout.containers import to_container, Window from prompt_toolkit.layout import walk if isinstance(value, six.text_type): def test(): return get_app().current_buffer.name == value elif isinstance(value, Buffer): def test(): return get_app().current_buffer == value elif isinstance(value, UIControl): def test(): return get_app().layout.current_control == value else: value = to_container(value) if isinstance(value, Window): def test(): return get_app().layout.current_window == value else: def test(): # Consider focused when any window inside this container is # focused. current_window = get_app().layout.current_window for c in walk(value): if isinstance(c, Window) and c == current_window: return True return False @Condition def has_focus_filter(): return test() return has_focus_filter
def _build_layout(self): " Rebuild a new Container object and return that. " logger.info('Rebuilding layout.') if not self.pymux.arrangement.windows: # No Pymux windows in the arrangement. return Window() active_window = self.pymux.arrangement.get_active_window() # When zoomed, only show the current pane, otherwise show all of them. if active_window.zoom: return to_container(_create_container_for_process( self.pymux, active_window, active_window.active_pane, zoom=True)) else: window = self.pymux.arrangement.get_active_window() return HSplit([ # Some spacing for the top status bar. ConditionalContainer( content=Window(height=1), filter=Condition(lambda: self.pymux.enable_pane_status)), # The actual content. _create_split(self.pymux, window, window.root) ])
def __init__(self, pymux, arrangement_pane, content): content = to_container(content) _ContainerProxy.__init__(self, content) self.pymux = pymux self.arrangement_pane = arrangement_pane
def __init__(self, pymux, arrangement_pane, content): content = to_container(content) _ContainerProxy.__init__(self, content) self.pymux = pymux self.arrangement_pane = arrangement_pane
def _get_current_status(self, window): return to_container(window).get_children()[0].content.buffer.text