Exemple #1
0
def print_container(
    container: "AnyContainer",
    file: Optional[TextIO] = None,
    style: Optional[BaseStyle] = None,
    include_default_pygments_style: bool = True,
) -> None:
    """
    Print any layout to the output in a non-interactive way.

    Example usage::

        from prompt_toolkit.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(),
        style=_create_merged_style(
            style,
            include_default_pygments_style=include_default_pygments_style),
    )
    app.run(pre_run=exit_immediately, in_thread=True)
Exemple #2
0
def print_container(
    container: "AnyContainer",
    file: Optional[TextIO] = None,
    style: Optional[BaseStyle] = None,
    include_default_pygments_style: bool = True,
) -> None:
    """
    Print any layout to the output in a non-interactive way.

    Example usage::

        from prompt_toolkit.widgets import Frame, TextArea
        print_container(
            Frame(TextArea(text='Hello world!')))
    """
    if file:
        output = create_output(stdout=file)
    else:
        output = get_app_session().output

    app: Application[None] = Application(
        layout=Layout(container=container),
        output=output,
        # `DummyInput` will cause the application to terminate immediately.
        input=DummyInput(),
        style=_create_merged_style(
            style,
            include_default_pygments_style=include_default_pygments_style),
    )
    try:
        app.run(in_thread=True)
    except EOFError:
        pass
def setup_stdout_redirection():
    """
    Prompt toolkit will try to create a VT100/Win32 output which expects stdout/stdout to have a file descriptor.
    Since Click's CliRunner wraps stdout in io.TextIOWrapper that does not have one we will get an error.
    Therefore we create a AppSession with an custom output module that simply writes to the current sys.stdout
    """
    class StdoutOutput(DummyOutput, ABC):
        def write(self, data: str) -> None:
            sys.stdout.write(data)

    # Create app session
    with create_app_session(input=DummyInput(), output=StdoutOutput()):
        # Yield control to the test case
        yield
Exemple #4
0
def print_container(container, file=None):
    """
    Print any layout to the output in a non-interactive way.

    Example usage::

        from prompt_toolkit.widgets import Frame, TextArea
        print_container(
            Frame(TextArea(text='Hello world!')))
    """
    if file:
        output = create_output(stdout=file)
    else:
        output = get_default_output()

    def exit_immediately():
        # Use `call_from_executor` to exit "soon", so that we still render one
        # initial time, before exiting the application.
        get_event_loop().call_from_executor(lambda: app.exit())

    app = Application(layout=Layout(container=container),
                      output=output,
                      input=DummyInput())
    app.run(pre_run=exit_immediately)
 def __init__(self):
     super(DummyApplication, self).__init__(output=DummyOutput(),
                                            input=DummyInput())
Exemple #6
0
def editor():
    return Editor(output=DummyOutput(), input=DummyInput())
Exemple #7
0
 def __init__(self) -> None:
     super().__init__(output=DummyOutput(), input=DummyInput())