Exemple #1
0
 def apply_transformation(self, ti):
     try:
         fragments = to_formatted_text(
             HTML(fragment_list_to_text(ti.fragments)))
         return Transformation(fragments)
     except Exception:
         return Transformation(ti.fragments)
    def update_entries(self, buffer, force=False):
        new_lineno = self.get_lineno(self.entries, buffer.cursor_position)
        if force or self.current_lineno != new_lineno:
            self.current_lineno = new_lineno

            text = ANSI(self.preview_callback(self.current_lineno))
            formatted_text = to_formatted_text(text)
            plain_text = fragment_list_to_text(formatted_text)
            self.preview_control.buffer.set_document(
                Document(plain_text, 0), bypass_readonly=True
            )
            self.preview_control.formatted_lines = self.preview_control.parse_formatted_text(
                formatted_text
            )
Exemple #3
0
    def _get_line_prefix(self, line_number, wrap_count, get_prompt_text_2):
        """
        Return whatever needs to be inserted before every line.
        (the prompt, or a line continuation.)
        """
        # First line: display the "arg" or the prompt.
        if line_number == 0 and wrap_count == 0:
            if not is_true(self.multiline) and get_app().key_processor.arg is not None:
                return self._inline_arg()
            else:
                return get_prompt_text_2()

        # For the next lines, display the appropriate continuation.
        prompt_width = get_cwidth(fragment_list_to_text(get_prompt_text_2()))
        return self._get_continuation(prompt_width, line_number, wrap_count)
    def _get_line_prefix(self, line_number, wrap_count, get_prompt_text_2):
        """
        Return whatever needs to be inserted before every line.
        (the prompt, or a line continuation.)
        """
        # First line: display the "arg" or the prompt.
        if line_number == 0 and wrap_count == 0:
            if not is_true(self.multiline) and get_app().key_processor.arg is not None:
                return self._inline_arg()
            else:
                return get_prompt_text_2()

        # For the next lines, display the appropriate continuation.
        prompt_width = get_cwidth(fragment_list_to_text(get_prompt_text_2()))
        return self._get_continuation(prompt_width, line_number, wrap_count)
Exemple #5
0
 def print(self, *text, sep: str = "\n", start: str = "\n") -> None:
     """Print Text to the Console Output, and then update the display."""
     self.console_backend.write_text(
         crlf(
             ("" if self.first else start)
             + sep.join(
                 fragment_list_to_text(line)
                 if isinstance(line, FormattedText)
                 else str(line)
                 for line in text
                 if line is not None
             )
         )
     )
     self.first = False
     self.redraw()
Exemple #6
0
 def __init__(
     self,
     text="",
     focusable=False,
     wrap_lines=True,
     width=None,
     height=None,
     scrollbar=False,
     dont_extend_height=True,
     dont_extend_width=False,
     read_only=True,
 ):
     self.read_only = read_only
     formatted_text = to_formatted_text(text)
     plain_text = fragment_list_to_text(formatted_text)
     self.buffer = Buffer(
         document=Document(plain_text, 0),
         read_only=Condition(lambda: self.read_only),
     )
     self.control = FormattedBufferControl(
         buffer=self.buffer,
         formatted_text=formatted_text,
         input_processors=[
             FormatTextProcessor(),
             HighlightSelectionProcessor()
         ],
         include_default_input_processors=False,
         focusable=focusable,
         focus_on_click=focusable,
     )
     self.scrollbar = scrollbar
     right_margins = [
         ConditionalMargin(
             ScrollbarMargin(display_arrows=True),
             filter=Condition(lambda: self.scrollbar),
         ),
     ]
     self.window = Window(
         content=self.control,
         width=width,
         height=height,
         wrap_lines=wrap_lines,
         right_margins=right_margins,
         dont_extend_height=dont_extend_height,
         dont_extend_width=dont_extend_width,
     )
 def get_width(self, ui_content):
     " Width to report to the `Window`. "
     # Take the width from the first line.
     text = fragment_list_to_text(self.get_prompt())
     return get_cwidth(text)
 def display_meta_text(self):
     " The 'meta' field as plain text. "
     from prompt_toolkit.formatted_text import fragment_list_to_text
     return fragment_list_to_text(self.display_meta)
 def display_meta_text(self):
     " The 'meta' field as plain text. "
     from prompt_toolkit.formatted_text import fragment_list_to_text
     return fragment_list_to_text(self.display_meta)
 def display_text(self) -> str:
     " The 'display' field as plain text. "
     from prompt_toolkit.formatted_text import fragment_list_to_text
     return fragment_list_to_text(self.display)
Exemple #11
0
 def apply_transformation(self, ti):
     fragments = to_formatted_text(ANSI(fragment_list_to_text(ti.fragments)))
     return Transformation(fragments)
Exemple #12
0
 def _to_text(self, formatted_text_list):
     return fragment_list_to_text(
         to_formatted_text(merge_formatted_text(formatted_text_list)))
    def __init__(
        self,
        entries,
        preview_callback=None,
        input_callback=None,
        input_completions=None,
    ):
        if not entries or len(entries) == 0:
            raise RuntimeError("Entries cannot be empty.")
        self.entries = entries
        self.input_callback = input_callback
        self.preview_callback = preview_callback

        self.ansi_escape_8bit = re.compile(
            r"(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x1B\[|\x9B)[0-?]*[ -/]*[@-~])"
        )
        self.current_lineno = 1
        self.max_entry_width = min(
            max(
                map(
                    lambda x: len(x) + 1, self.ansi_escape_8bit.sub("", entries).split("\n")
                )
            ),
            48,
        )

        entries_text = ANSI(self.entries)
        entries_formatted_text = to_formatted_text(entries_text)
        entries_plain_text = fragment_list_to_text(entries_formatted_text)
        self.entries_control = FormattedBufferControl(
            buffer=Buffer(
                document=Document(entries_plain_text, 0),
                name="entries",
                on_cursor_position_changed=self.update_entries,
                read_only=True,
            ),
            focusable=True,
            formatted_text=entries_formatted_text,
            include_default_input_processors=False,
            input_processors=[FormatTextProcessor(), HighlightSelectionProcessor()],
        )

        if self.preview_callback:
            self.preview_text = ANSI(self.preview_callback(self.current_lineno))
            formatted_text = to_formatted_text(self.preview_text)
            plain_text = fragment_list_to_text(formatted_text)
            self.preview_control = FormattedBufferControl(
                buffer=Buffer(
                    document=Document(plain_text, 0),
                    name="preview",
                    on_cursor_position_changed=self.update_preview,
                    read_only=True,
                ),
                focusable=True,
                formatted_text=formatted_text,
                include_default_input_processors=False,
                input_processors=[FormatTextProcessor(), HighlightSelectionProcessor()],
            )
            # Alternative (not scrollable):
            # self.preview_control = FormattedTextControl(
            #     focusable=True,
            #     show_cursor=True,
            #     text=ANSI(self.preview_callback(self.current_lineno)),
            # )
            entries_container = VSplit(
                [
                    Window(
                        content=self.entries_control,
                        width=self.max_entry_width,
                        wrap_lines=True,
                    ),
                    Window(width=3, char=" | "),
                    Window(content=self.preview_control, wrap_lines=True),
                ]
            )
        else:
            entries_container = Window(
                content=self.entries_control,
                width=self.max_entry_width,
                wrap_lines=True,
            )
        if self.input_callback:
            self.search_field = SearchToolbar()
            self.input_field = TextArea(
                accept_handler=self.input_accept,
                completer=FuzzyWordCompleter(list(input_completions)),
                complete_while_typing=True,
                height=1,
                multiline=False,
                prompt="> ",
                search_field=self.search_field,
                wrap_lines=False,
            )
            self.root_container = FloatContainer(
                content=HSplit(
                    [entries_container, Window(height=1, char="-"), self.input_field,]
                ),
                floats=[
                    Float(
                        content=CompletionsMenu(max_height=16, scroll_offset=1),
                        xcursor=True,
                        ycursor=True,
                    )
                ],
            )

        else:
            self.root_container = entries_container
        self.layout = Layout(self.root_container)

        self.app = Application(full_screen=True, key_bindings=kb, layout=self.layout)
 def get_width(self, ui_content):
     " Width to report to the `Window`. "
     # Take the width from the first line.
     text = fragment_list_to_text(self.get_prompt())
     return get_cwidth(text)
Exemple #15
0
 def text(self, text):
     formatted_text = to_formatted_text(text)
     self.control.update_formatted_text(formatted_text)
     plain_text = fragment_list_to_text(formatted_text)
     self.document = Document(plain_text, 0)
Exemple #16
0
    def display_text(self) -> str:
        "The 'display' field as plain text."
        from prompt_toolkit.formatted_text import fragment_list_to_text

        return fragment_list_to_text(self.display)
Exemple #17
0
def _to_text(button: Button) -> str:
    control = to_window(button).content
    return fragment_list_to_text(control.text())
Exemple #18
0
 def is_private(completion: Completion) -> bool:
     text = fragment_list_to_text(to_formatted_text(completion.display))
     return text.startswith("_")
Exemple #19
0
 def apply_transformation(self, transformation_input):
     fragments = to_formatted_text(
         HTML(fragment_list_to_text(transformation_input.fragments)))
     return Transformation(fragments)
 def get_width(self, get_ui_content: Callable[[], UIContent]) -> int:
     " Width to report to the `Window`. "
     # Take the width from the first line.
     text = fragment_list_to_text(self.get_prompt())
     return get_cwidth(text)
Exemple #21
0
def to_text(widget):
    control = to_window(widget).content
    return fragment_list_to_text(control.text())