def print_last_kbd_macro(event):
    " Print the last keyboard macro. "
    # TODO: Make the format suitable for the inputrc file.
    def print_macro():
        for k in event.app.key_processor.macro:
            print(k)

    from prompt_toolkit.application.run_in_terminal import run_in_terminal
    run_in_terminal(print_macro)
Exemplo n.º 2
0
def print_last_kbd_macro(event):
    " Print the last keyboard macro. "

    # TODO: Make the format suitable for the inputrc file.
    def print_macro():
        for k in event.app.key_processor.macro:
            print(k)

    from prompt_toolkit.application.run_in_terminal import run_in_terminal
    run_in_terminal(print_macro)
def print_last_kbd_macro(event: E) -> None:
    """
    Print the last keyboard macro.
    """
    # TODO: Make the format suitable for the inputrc file.
    def print_macro() -> None:
        macro = event.app.emacs_state.macro
        if macro:
            for k in macro:
                print(k)

    from prompt_toolkit.application.run_in_terminal import run_in_terminal
    run_in_terminal(print_macro)
Exemplo n.º 4
0
def print_last_kbd_macro(event: E) -> None:
    """
    Print the last keyboard macro.
    """

    # TODO: Make the format suitable for the inputrc file.
    def print_macro() -> None:
        macro = event.app.emacs_state.macro
        if macro:
            for k in macro:
                print(k)

    from prompt_toolkit.application.run_in_terminal import run_in_terminal
    run_in_terminal(print_macro)
Exemplo n.º 5
0
            def run():
                def cleanup():
                    if orig_visual:
                        os.environ['VISUAL'] = orig_visual
                    else:
                        del os.environ['VISUAL']

                yield From(run_in_terminal(cleanup, in_executor=True))
Exemplo n.º 6
0
 def _run_in_terminal(self, func):
     # Make sure that when an application was active for this connection,
     # that we print the text above the application.
     with context(self._context_id):
         return run_in_terminal(func)
Exemplo n.º 7
0
def print_formatted_text(
    *values: Any,
    sep: str = " ",
    end: str = "\n",
    file: Optional[TextIO] = None,
    flush: bool = False,
    style: Optional[BaseStyle] = None,
    output: Optional[Output] = None,
    color_depth: Optional[ColorDepth] = None,
    style_transformation: Optional[StyleTransformation] = None,
    include_default_pygments_style: bool = True,
) -> None:
    """
    ::

        print_formatted_text(*values, sep=' ', end='\\n', file=None, flush=False, style=None, output=None)

    Print text to stdout. This is supposed to be compatible with Python's print
    function, but supports printing of formatted text. You can pass a
    :class:`~prompt_toolkit.formatted_text.FormattedText`,
    :class:`~prompt_toolkit.formatted_text.HTML` or
    :class:`~prompt_toolkit.formatted_text.ANSI` object to print formatted
    text.

    * Print HTML as follows::

        print_formatted_text(HTML('<i>Some italic text</i> <ansired>This is red!</ansired>'))

        style = Style.from_dict({
            'hello': '#ff0066',
            'world': '#884444 italic',
        })
        print_formatted_text(HTML('<hello>Hello</hello> <world>world</world>!'), style=style)

    * Print a list of (style_str, text) tuples in the given style to the
      output.  E.g.::

        style = Style.from_dict({
            'hello': '#ff0066',
            'world': '#884444 italic',
        })
        fragments = FormattedText([
            ('class:hello', 'Hello'),
            ('class:world', 'World'),
        ])
        print_formatted_text(fragments, style=style)

    If you want to print a list of Pygments tokens, wrap it in
    :class:`~prompt_toolkit.formatted_text.PygmentsTokens` to do the
    conversion.

    If a prompt_toolkit `Application` is currently running, this will always
    print above the application or prompt (similar to `patch_stdout`). So,
    `print_formatted_text` will erase the current application, print the text,
    and render the application again.

    :param values: Any kind of printable object, or formatted string.
    :param sep: String inserted between values, default a space.
    :param end: String appended after the last value, default a newline.
    :param style: :class:`.Style` instance for the color scheme.
    :param include_default_pygments_style: `bool`. Include the default Pygments
        style when set to `True` (the default).
    """
    assert not (output and file)

    # Create Output object.
    if output is None:
        if file:
            output = create_output(stdout=file)
        else:
            output = get_app_session().output

    assert isinstance(output, Output)

    # Get color depth.
    color_depth = color_depth or output.get_default_color_depth()

    # Merges values.
    def to_text(val: Any) -> StyleAndTextTuples:
        # Normal lists which are not instances of `FormattedText` are
        # considered plain text.
        if isinstance(val, list) and not isinstance(val, FormattedText):
            return to_formatted_text("{0}".format(val))
        return to_formatted_text(val, auto_convert=True)

    fragments = []
    for i, value in enumerate(values):
        fragments.extend(to_text(value))

        if sep and i != len(values) - 1:
            fragments.extend(to_text(sep))

    fragments.extend(to_text(end))

    # Print output.
    def render() -> None:
        assert isinstance(output, Output)

        renderer_print_formatted_text(
            output,
            fragments,
            _create_merged_style(
                style,
                include_default_pygments_style=include_default_pygments_style),
            color_depth=color_depth,
            style_transformation=style_transformation,
        )

        # Flush the output stream.
        if flush:
            output.flush()

    # If an application is running, print above the app. This does not require
    # `patch_stdout`.
    loop: Optional[AbstractEventLoop] = None

    app = get_app_or_none()
    if app is not None:
        loop = app.loop

    if loop is not None:
        loop.call_soon_threadsafe(lambda: run_in_terminal(render))
    else:
        render()