Exemplo n.º 1
0
    def __init__(self,
                 text='',
                 style='',
                 focusable=False,
                 key_bindings=None,
                 show_cursor=True,
                 modal=False,
                 get_cursor_position=None):
        from prompt_toolkit.key_binding.key_bindings import KeyBindingsBase
        assert isinstance(style, six.text_type)
        assert key_bindings is None or isinstance(key_bindings,
                                                  KeyBindingsBase)
        assert isinstance(show_cursor, bool)
        assert isinstance(modal, bool)
        assert get_cursor_position is None or callable(get_cursor_position)

        self.text = text  # No type check on 'text'. This is done dynamically.
        self.style = style
        self.focusable = to_filter(focusable)

        # Key bindings.
        self.key_bindings = key_bindings
        self.show_cursor = show_cursor
        self.modal = modal
        self.get_cursor_position = get_cursor_position

        #: Cache for the content.
        self._content_cache = SimpleCache(maxsize=18)
        self._fragment_cache = SimpleCache(maxsize=1)
        # Only cache one fragment list. We don't need the previous item.

        # Render info for the mouse support.
        self._fragments = None
Exemplo n.º 2
0
    def __init__(self,
                 get_tokens,
                 default_char=None,
                 get_default_char=None,
                 align_right=False,
                 align_center=False,
                 has_focus=False):
        assert callable(get_tokens)
        assert default_char is None or isinstance(default_char, Char)
        assert get_default_char is None or callable(get_default_char)
        assert not (default_char and get_default_char)

        self.align_right = to_cli_filter(align_right)
        self.align_center = to_cli_filter(align_center)
        self._has_focus_filter = to_cli_filter(has_focus)

        self.get_tokens = get_tokens

        # Construct `get_default_char` callable.
        if default_char:
            get_default_char = lambda _: default_char
        elif not get_default_char:
            get_default_char = lambda _: Char(' ', Token.Transparent)

        self.get_default_char = get_default_char

        #: Cache for the content.
        self._content_cache = SimpleCache(maxsize=18)
        self._token_cache = SimpleCache(maxsize=1)
        # Only cache one token list. We don't need the previous item.

        # Render info for the mouse support.
        self._tokens = None
Exemplo n.º 3
0
    def __init__(
        self,
        text: AnyFormattedText = "",
        style: str = "",
        focusable: FilterOrBool = False,
        key_bindings: Optional["KeyBindingsBase"] = None,
        show_cursor: bool = True,
        modal: bool = False,
        get_cursor_position: Optional[Callable[[], Optional[Point]]] = None,
    ) -> None:

        self.text = text  # No type check on 'text'. This is done dynamically.
        self.style = style
        self.focusable = to_filter(focusable)

        # Key bindings.
        self.key_bindings = key_bindings
        self.show_cursor = show_cursor
        self.modal = modal
        self.get_cursor_position = get_cursor_position

        #: Cache for the content.
        self._content_cache: SimpleCache[Hashable,
                                         UIContent] = SimpleCache(maxsize=18)
        self._fragment_cache: SimpleCache[int,
                                          StyleAndTextTuples] = SimpleCache(
                                              maxsize=1)
        # Only cache one fragment list. We don't need the previous item.

        # Render info for the mouse support.
        self._fragments: Optional[StyleAndTextTuples] = None
    def __init__(self,
                 get_tokens,
                 default_char=None,
                 get_default_char=None,
                 align_right=False,
                 align_center=False,
                 has_focus=False,
                 wrap_lines=True):
        assert default_char is None or isinstance(default_char, Char)
        assert get_default_char is None or callable(get_default_char)
        assert not (default_char and get_default_char)

        self.align_right = to_cli_filter(align_right)
        self.align_center = to_cli_filter(align_center)
        self._has_focus_filter = to_cli_filter(has_focus)
        self.wrap_lines = to_cli_filter(wrap_lines)

        self.get_tokens = get_tokens

        # Construct `get_default_char` callable.
        if default_char:
            get_default_char = lambda _: default_char
        elif not get_default_char:
            get_default_char = lambda _: Char(' ', Token)

        self.get_default_char = get_default_char

        #: Cache for rendered screens.
        self._screen_cache = SimpleCache(maxsize=18)
        self._token_cache = SimpleCache(maxsize=1)
        # Only cache one token list. We don't need the previous item.

        # Render info for the mouse support.
        self._tokens = None  # The last rendered tokens.
        self._pos_to_indexes = None  # Mapping from mouse positions (x,y) to
Exemplo n.º 5
0
 def __init__(self) -> None:
     self._bindings: List[Binding] = []
     self._get_bindings_for_keys_cache: SimpleCache[KeysTuple, List[Binding]] = \
         SimpleCache(maxsize=10000)
     self._get_bindings_starting_with_keys_cache: SimpleCache[KeysTuple, List[Binding]] = \
         SimpleCache(maxsize=1000)
     self.__version = 0  # For cache invalidation.
    def __init__(self,
                 buffer_name=DEFAULT_BUFFER,
                 input_processors=None,
                 highlighters=None,
                 lexer=None,
                 preview_search=False,
                 search_buffer_name=SEARCH_BUFFER,
                 get_search_state=None,
                 wrap_lines=True,
                 menu_position=None,
                 default_char=None,
                 focus_on_click=False):
        assert input_processors is None or all(
            isinstance(i, Processor) for i in input_processors)
        assert highlighters is None or all(
            isinstance(i, Highlighter) for i in highlighters)
        assert menu_position is None or callable(menu_position)
        assert lexer is None or isinstance(lexer, Lexer)
        assert get_search_state is None or callable(get_search_state)

        self.preview_search = to_cli_filter(preview_search)
        self.get_search_state = get_search_state
        self.wrap_lines = to_cli_filter(wrap_lines)
        self.focus_on_click = to_cli_filter(focus_on_click)

        self.input_processors = input_processors or []
        self.highlighters = highlighters or []
        self.buffer_name = buffer_name
        self.menu_position = menu_position
        self.lexer = lexer or SimpleLexer()
        self.default_char = default_char or Char(token=Token.Transparent)
        self.search_buffer_name = search_buffer_name

        #: Cache for the lexer.
        #: Often, due to cursor movement, undo/redo and window resizing
        #: operations, it happens that a short time, the same document has to be
        #: lexed. This is a faily easy way to cache such an expensive operation.
        self._token_cache = SimpleCache(maxsize=8)

        #: Keep a similar cache for rendered screens. (when we scroll up/down
        #: through the screen, or when we change another buffer, we don't want
        #: to recreate the same screen again.)
        self._screen_cache = SimpleCache(maxsize=8)

        #: Highlight Cache.
        #: When nothing of the buffer content or processors has changed, but
        #: the highlighting of the selection/search changes,
        self._highlight_cache = SimpleCache(maxsize=8)

        self._xy_to_cursor_position = None
        self._last_click_timestamp = None
Exemplo n.º 7
0
    def __init__(
        self, chars: str = "[](){}<>", max_cursor_distance: int = 1000
    ) -> None:
        self.chars = chars
        self.max_cursor_distance = max_cursor_distance

        self._positions_cache: SimpleCache[
            Hashable, List[Tuple[int, int]]
        ] = SimpleCache(maxsize=8)
Exemplo n.º 8
0
    def __init__(self,
                 buffer=None,
                 input_processors=None,
                 include_default_input_processors=True,
                 lexer=None,
                 preview_search=False,
                 focusable=True,
                 search_buffer_control=None,
                 menu_position=None,
                 focus_on_click=False,
                 key_bindings=None):
        from prompt_toolkit.key_binding.key_bindings import KeyBindingsBase
        assert buffer is None or isinstance(buffer, Buffer)
        assert input_processors is None or isinstance(input_processors, list)
        assert isinstance(include_default_input_processors, bool)
        assert menu_position is None or callable(menu_position)
        assert lexer is None or isinstance(lexer, Lexer)
        assert (search_buffer_control is None
                or callable(search_buffer_control)
                or isinstance(search_buffer_control, SearchBufferControl))
        assert key_bindings is None or isinstance(key_bindings,
                                                  KeyBindingsBase)

        self.input_processors = input_processors
        self.include_default_input_processors = include_default_input_processors

        self.default_input_processors = [
            HighlightSearchProcessor(),
            HighlightIncrementalSearchProcessor(),
            HighlightSelectionProcessor(),
            DisplayMultipleCursors(),
        ]

        self.preview_search = to_filter(preview_search)
        self.focusable = to_filter(focusable)
        self.focus_on_click = to_filter(focus_on_click)

        self.buffer = buffer or Buffer()
        self.menu_position = menu_position
        self.lexer = lexer or SimpleLexer()
        self.key_bindings = key_bindings
        self._search_buffer_control = search_buffer_control

        #: Cache for the lexer.
        #: Often, due to cursor movement, undo/redo and window resizing
        #: operations, it happens that a short time, the same document has to be
        #: lexed. This is a fairly easy way to cache such an expensive operation.
        self._fragment_cache = SimpleCache(maxsize=8)

        self._xy_to_cursor_position = None
        self._last_click_timestamp = None
        self._last_get_processed_line = None
Exemplo n.º 9
0
    def __init__(self,
                 content,
                 width=None,
                 height=None,
                 get_width=None,
                 get_height=None,
                 dont_extend_width=False,
                 dont_extend_height=False,
                 left_margins=None,
                 right_margins=None,
                 scroll_offsets=None,
                 allow_scroll_beyond_bottom=False,
                 get_vertical_scroll=None,
                 get_horizontal_scroll=None,
                 always_hide_cursor=False):
        assert isinstance(content, UIControl)
        assert width is None or isinstance(width, LayoutDimension)
        assert height is None or isinstance(height, LayoutDimension)
        assert get_width is None or callable(get_width)
        assert get_height is None or callable(get_height)
        assert width is None or get_width is None
        assert height is None or get_height is None
        assert scroll_offsets is None or isinstance(scroll_offsets,
                                                    ScrollOffsets)
        assert left_margins is None or all(
            isinstance(m, Margin) for m in left_margins)
        assert right_margins is None or all(
            isinstance(m, Margin) for m in right_margins)
        assert get_vertical_scroll is None or callable(get_vertical_scroll)
        assert get_horizontal_scroll is None or callable(get_horizontal_scroll)

        self.allow_scroll_beyond_bottom = to_cli_filter(
            allow_scroll_beyond_bottom)
        self.always_hide_cursor = to_cli_filter(always_hide_cursor)

        self.content = content
        self.dont_extend_width = dont_extend_width
        self.dont_extend_height = dont_extend_height
        self.left_margins = left_margins or []
        self.right_margins = right_margins or []
        self.scroll_offsets = scroll_offsets or ScrollOffsets()
        self.get_vertical_scroll = get_vertical_scroll
        self.get_horizontal_scroll = get_horizontal_scroll
        self._width = get_width or (lambda cli: width)
        self._height = get_height or (lambda cli: height)

        # Cache for the screens generated by the margin.
        self._margin_cache = SimpleCache(maxsize=8)

        self.reset()
Exemplo n.º 10
0
    def __init__(
        self,
        buffer: Optional[Buffer] = None,
        input_processors: Optional[List[Processor]] = None,
        include_default_input_processors: bool = True,
        lexer: Optional[Lexer] = None,
        preview_search: FilterOrBool = False,
        focusable: FilterOrBool = True,
        search_buffer_control: Union[None, "SearchBufferControl",
                                     Callable[[],
                                              "SearchBufferControl"]] = None,
        menu_position: Optional[Callable] = None,
        focus_on_click: FilterOrBool = False,
        key_bindings: Optional["KeyBindingsBase"] = None,
    ):

        self.input_processors = input_processors
        self.include_default_input_processors = include_default_input_processors

        self.default_input_processors = [
            HighlightSearchProcessor(),
            HighlightIncrementalSearchProcessor(),
            HighlightSelectionProcessor(),
            DisplayMultipleCursors(),
        ]

        self.preview_search = to_filter(preview_search)
        self.focusable = to_filter(focusable)
        self.focus_on_click = to_filter(focus_on_click)

        self.buffer = buffer or Buffer()
        self.menu_position = menu_position
        self.lexer = lexer or SimpleLexer()
        self.key_bindings = key_bindings
        self._search_buffer_control = search_buffer_control

        #: Cache for the lexer.
        #: Often, due to cursor movement, undo/redo and window resizing
        #: operations, it happens that a short time, the same document has to be
        #: lexed. This is a fairly easy way to cache such an expensive operation.
        self._fragment_cache: SimpleCache[Hashable, Callable[
            [int], StyleAndTextTuples]] = SimpleCache(maxsize=8)

        self._last_click_timestamp: Optional[float] = None
        self._last_get_processed_line: Optional[Callable[
            [int], _ProcessedLine]] = None
Exemplo n.º 11
0
    def __init__(self,
                 buffer_name=DEFAULT_BUFFER,
                 input_processors=None,
                 lexer=None,
                 preview_search=False,
                 search_buffer_name=SEARCH_BUFFER,
                 get_search_state=None,
                 menu_position=None,
                 default_char=None,
                 focus_on_click=False):
        assert input_processors is None or all(
            isinstance(i, Processor) for i in input_processors)
        assert menu_position is None or callable(menu_position)
        assert lexer is None or isinstance(lexer, Lexer)
        assert get_search_state is None or callable(get_search_state)
        assert default_char is None or isinstance(default_char, Char)

        self.preview_search = to_cli_filter(preview_search)
        self.get_search_state = get_search_state
        self.focus_on_click = to_cli_filter(focus_on_click)

        self.input_processors = input_processors or []
        self.buffer_name = buffer_name
        self.menu_position = menu_position
        self.lexer = lexer or SimpleLexer()
        self.default_char = default_char or Char(token=Token.Transparent)
        self.search_buffer_name = search_buffer_name

        #: Cache for the lexer.
        #: Often, due to cursor movement, undo/redo and window resizing
        #: operations, it happens that a short time, the same document has to be
        #: lexed. This is a faily easy way to cache such an expensive operation.
        self._token_cache = SimpleCache(maxsize=8)

        self._xy_to_cursor_position = None
        self._last_click_timestamp = None
        self._last_get_processed_line = None
    def __init__(self, chars='[](){}<>', max_cursor_distance=1000):
        self.chars = chars
        self.max_cursor_distance = max_cursor_distance

        self._positions_cache = SimpleCache(maxsize=8)
Exemplo n.º 13
0
 def __init__(self, app):
     self.app = app
     self._cache = SimpleCache()
Exemplo n.º 14
0
    def __init__(self,
                 buffer=None,
                 input_processor=None,
                 lexer=None,
                 preview_search=False,
                 focusable=True,
                 search_buffer_control=None,
                 get_search_buffer_control=None,
                 get_search_state=None,
                 menu_position=None,
                 focus_on_click=False,
                 key_bindings=None):
        from prompt_toolkit.key_binding.key_bindings import KeyBindingsBase
        assert buffer is None or isinstance(buffer, Buffer)
        assert input_processor is None or isinstance(input_processor,
                                                     Processor)
        assert menu_position is None or callable(menu_position)
        assert lexer is None or isinstance(lexer, Lexer)
        assert search_buffer_control is None or isinstance(
            search_buffer_control, BufferControl)
        assert get_search_buffer_control is None or callable(
            get_search_buffer_control)
        assert not (search_buffer_control and get_search_buffer_control)
        assert get_search_state is None or callable(get_search_state)
        assert key_bindings is None or isinstance(key_bindings,
                                                  KeyBindingsBase)

        # Default search state.
        if get_search_state is None:
            search_state = SearchState()

            def get_search_state():
                return search_state

        # Default input processor (display search and selection by default.)
        if input_processor is None:
            input_processor = merge_processors([
                HighlightSearchProcessor(),
                HighlightSelectionProcessor(),
                DisplayMultipleCursors(),
            ])

        self.preview_search = to_filter(preview_search)
        self.focusable = to_filter(focusable)
        self.get_search_state = get_search_state
        self.focus_on_click = to_filter(focus_on_click)

        self.input_processor = input_processor
        self.buffer = buffer or Buffer()
        self.menu_position = menu_position
        self.lexer = lexer or SimpleLexer()
        self.get_search_buffer_control = get_search_buffer_control
        self.key_bindings = key_bindings
        self._search_buffer_control = search_buffer_control

        #: Cache for the lexer.
        #: Often, due to cursor movement, undo/redo and window resizing
        #: operations, it happens that a short time, the same document has to be
        #: lexed. This is a fairly easy way to cache such an expensive operation.
        self._fragment_cache = SimpleCache(maxsize=8)

        self._xy_to_cursor_position = None
        self._last_click_timestamp = None
        self._last_get_processed_line = None
Exemplo n.º 15
0
 def __init__(self, styles: List[BaseStyle]) -> None:
     self.styles = styles
     self._style: SimpleCache[Hashable, Style] = SimpleCache(maxsize=1)
Exemplo n.º 16
0
 def __init__(self):
     self.bindings = []
     self._get_bindings_for_keys_cache = SimpleCache(maxsize=10000)
     self._get_bindings_starting_with_keys_cache = SimpleCache(maxsize=1000)
     self.__version = 0  # For cache invalidation.
Exemplo n.º 17
0
    def __init__(self, styles):
        assert all(isinstance(style, BaseStyle) for style in styles)

        self.styles = styles
        self._style = SimpleCache(maxsize=1)
Exemplo n.º 18
0
 def __init__(self):
     self.key_bindings = []
     self._get_bindings_for_keys_cache = SimpleCache(maxsize=10000)
     self._get_bindings_starting_with_keys_cache = SimpleCache(maxsize=1000)
Exemplo n.º 19
0
 def __init__(self, app: Application[_AppResult]) -> None:
     self.app = app
     self._cache: SimpleCache[Tuple[Window, FrozenSet[UIControl]],
                              KeyBindingsBase] = SimpleCache()