def print_container(container: 'Container', file: Optional[TextIO] = None) -> None: """ Print any layout to the output in a non-interactive way. Example usage:: from prompt_toolkit_dev.widgets import Frame, TextArea print_container( Frame(TextArea(text='Hello world!'))) """ if file: output = create_output(stdout=file) else: output = get_app_session().output def exit_immediately() -> None: # Use `call_from_executor` to exit "soon", so that we still render one # initial time, before exiting the application. get_event_loop().call_soon(lambda: app.exit()) app: Application[None] = Application( layout=Layout(container=container), output=output, input=DummyInput()) app.run(pre_run=exit_immediately)
def set_dummy_app(): """ Return a context manager that makes sure that this dummy application is active. This is important, because we need an `Application` with `is_done=False` flag, otherwise no keys will be processed. """ app = Application(layout=Layout(Window()), output=DummyOutput(), input=create_pipe_input()) return set_app(app)
def _create_app(dialog: AnyContainer, style: Optional[BaseStyle]) -> Application[Any]: # Key bindings. bindings = KeyBindings() bindings.add('tab')(focus_next) bindings.add('s-tab')(focus_previous) return Application(layout=Layout(dialog), key_bindings=merge_key_bindings([ load_key_bindings(), bindings, ]), mouse_support=True, style=style, full_screen=True)
def _(event): " Quit. " event.app.exit() style = Style.from_dict({ 'status': 'reverse', 'status.position': '#aaaa00', 'status.key': '#ffaa00', 'not-searching': '#888888', }) # create application. application = Application(layout=Layout( root_container, focused_element=text_area, ), key_bindings=bindings, enable_page_navigation_bindings=True, mouse_support=True, style=style, full_screen=True) def run(): application.run() if __name__ == '__main__': run()
body = HSplit([ Window(FormattedTextControl(left_text), align=WindowAlign.LEFT), Window(height=1, char='-'), Window(FormattedTextControl(center_text), align=WindowAlign.CENTER), Window(height=1, char='-'), Window(FormattedTextControl(right_text), align=WindowAlign.RIGHT), ]) # 2. Key bindings kb = KeyBindings() @kb.add('q') def _(event): " Quit application. " event.app.exit() # 3. The `Application` application = Application(layout=Layout(body), key_bindings=kb, full_screen=True) def run(): application.run() if __name__ == '__main__': run()
layout = Layout(container=root_container, focused_element=button1) # Key bindings. kb = KeyBindings() kb.add('tab')(focus_next) kb.add('s-tab')(focus_previous) # Styling. style = Style([ ('left-pane', 'bg:#888800 #000000'), ('right-pane', 'bg:#00aa00 #000000'), ('button', '#000000'), ('button-arrow', '#000000'), ('button focused', 'bg:#ff0000'), ('text-area focused', 'bg:#ff0000'), ]) # Build a main application object. application = Application(layout=layout, key_bindings=kb, style=style, full_screen=True) def main(): application.run() if __name__ == '__main__': main()
def main(): # The layout. search_field = SearchToolbar() # For reverse search. output_field = TextArea(style='class:output-field', text=help_text) input_field = TextArea(height=1, prompt='>>> ', style='class:input-field', multiline=False, wrap_lines=False, search_field=search_field) container = HSplit([ output_field, Window(height=1, char='-', style='class:line'), input_field, search_field, ]) # Attach accept handler to the input field. We do this by assigning the # handler to the `TextArea` that we created earlier. it is also possible to # pass it to the constructor of `TextArea`. # NOTE: It's better to assign an `accept_handler`, rather then adding a # custom ENTER key binding. This will automatically reset the input # field and add the strings to the history. def accept(buff): # Evaluate "calculator" expression. try: output = '\n\nIn: {}\nOut: {}'.format( input_field.text, eval(input_field.text)) # Don't do 'eval' in real code! except BaseException as e: output = '\n\n{}'.format(e) new_text = output_field.text + output # Add text to output buffer. output_field.buffer.document = Document(text=new_text, cursor_position=len(new_text)) input_field.accept_handler = accept # The key bindings. kb = KeyBindings() @kb.add('c-c') @kb.add('c-q') def _(event): " Pressing Ctrl-Q or Ctrl-C will exit the user interface. " event.app.exit() # Style. style = Style([ ('output-field', 'bg:#000044 #ffffff'), ('input-field', 'bg:#000000 #ffffff'), ('line', '#004400'), ]) # Run application. application = Application(layout=Layout(container, focused_element=input_field), key_bindings=kb, style=style, mouse_support=True, full_screen=True) application.run()
right_buffer.text = left_buffer.text[::-1] left_buffer.on_text_changed += default_buffer_changed # 3. Creating an `Application` instance # ---------------------------------- # This glues everything together. application = Application( layout=Layout(root_container, focused_element=left_window), key_bindings=kb, # Let's add mouse support! mouse_support=True, # Using an alternate screen buffer means as much as: "run full screen". # It switches the terminal to an alternate screen. full_screen=True) # 4. Run the application # ------------------- def run(): # Run the interface. (This runs the event loop until Ctrl-Q is pressed.) application.run()
def __enter__(self) -> 'ProgressBar': # Create UI Application. title_toolbar = ConditionalContainer( Window(FormattedTextControl(lambda: self.title), height=1, style='class:progressbar,title'), filter=Condition(lambda: self.title is not None)) bottom_toolbar = ConditionalContainer( Window(FormattedTextControl(lambda: self.bottom_toolbar, style='class:bottom-toolbar.text'), style='class:bottom-toolbar', height=1), filter=~is_done & renderer_height_is_known & Condition(lambda: self.bottom_toolbar is not None)) def width_for_formatter(formatter: Formatter) -> AnyDimension: # Needs to be passed as callable (partial) to the 'width' # parameter, because we want to call it on every resize. return formatter.get_width(progress_bar=self) progress_controls = [ Window( content=_ProgressControl(self, f), width=functools.partial(width_for_formatter, f)) for f in self.formatters ] self.app: Application[None] = Application( min_redraw_interval=.05, layout=Layout(HSplit([ title_toolbar, VSplit(progress_controls, height=lambda: D( preferred=len(self.counters), max=len(self.counters))), Window(), bottom_toolbar, ])), style=self.style, key_bindings=self.key_bindings, refresh_interval=.3, color_depth=self.color_depth, output=self.output, input=self.input) # Run application in different thread. def run() -> None: set_event_loop(self._app_loop) try: self.app.run() except BaseException as e: traceback.print_exc() print(e) ctx: contextvars.Context = contextvars.copy_context() self._thread = threading.Thread(target=ctx.run, args=(run, )) self._thread.start() # Attach WINCH signal handler in main thread. # (Interrupt that we receive during resize events.) self._has_sigwinch = hasattr(signal, 'SIGWINCH') and in_main_thread() if self._has_sigwinch: self._previous_winch_handler = signal.getsignal(signal.SIGWINCH) self._loop.add_signal_handler(signal.SIGWINCH, self.invalidate) return self
'focused button': 'bg:#880000 #ffffff noinherit', # Styling for Dialog widgets. 'radiolist focused': 'noreverse', 'radiolist focused radio.selected': 'reverse', 'button-bar': 'bg:#aaaaff' }) application = Application( layout=Layout( root_container, focused_element=yes_button, ), key_bindings=bindings, style=style, mouse_support=True, full_screen=True) def run(): result = application.run() print('You said: %r' % result) if __name__ == '__main__': run()
MenuItem('About', handler=do_about), ]), ], floats=[ Float(xcursor=True, ycursor=True, content=CompletionsMenu(max_height=16, scroll_offset=1)), ], key_bindings=bindings) style = Style.from_dict({ 'status': 'reverse', 'shadow': 'bg:#440044', }) layout = Layout(root_container, focused_element=text_field) application = Application(layout=layout, enable_page_navigation_bindings=True, style=style, mouse_support=True, full_screen=True) def run(): application.run() if __name__ == '__main__': run()