omni-ai-cli 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +615 -2
- package/bin/ai.js +84 -0
- package/docs/README.en.md +615 -0
- package/docs/README.es.md +618 -0
- package/docs/README.fr.md +630 -0
- package/omni_ai/__init__.py +1 -0
- package/omni_ai/__main__.py +6 -0
- package/omni_ai/chat.py +1458 -0
- package/omni_ai/cli.py +991 -0
- package/omni_ai/config.py +531 -0
- package/omni_ai/deepseek.py +57 -0
- package/omni_ai/grok.py +111 -0
- package/omni_ai/onboarding.py +196 -0
- package/omni_ai/openrouter.py +335 -0
- package/omni_ai/osutils.py +86 -0
- package/omni_ai/pii.py +281 -0
- package/omni_ai/profiles.json +69 -0
- package/omni_ai/skills.py +124 -0
- package/omni_ai/sync.py +199 -0
- package/omni_ai/tools.py +75 -0
- package/omni_ai/ui.py +595 -0
- package/omni_ai/usage.py +305 -0
- package/omni_ai/vendor/prompt_toolkit/LICENSE +27 -0
- package/omni_ai/vendor/prompt_toolkit/__init__.py +52 -0
- package/omni_ai/vendor/prompt_toolkit/application/__init__.py +32 -0
- package/omni_ai/vendor/prompt_toolkit/application/application.py +1626 -0
- package/omni_ai/vendor/prompt_toolkit/application/current.py +195 -0
- package/omni_ai/vendor/prompt_toolkit/application/dummy.py +55 -0
- package/omni_ai/vendor/prompt_toolkit/application/run_in_terminal.py +114 -0
- package/omni_ai/vendor/prompt_toolkit/auto_suggest.py +177 -0
- package/omni_ai/vendor/prompt_toolkit/buffer.py +2029 -0
- package/omni_ai/vendor/prompt_toolkit/cache.py +127 -0
- package/omni_ai/vendor/prompt_toolkit/clipboard/__init__.py +17 -0
- package/omni_ai/vendor/prompt_toolkit/clipboard/base.py +109 -0
- package/omni_ai/vendor/prompt_toolkit/clipboard/in_memory.py +44 -0
- package/omni_ai/vendor/prompt_toolkit/clipboard/pyperclip.py +42 -0
- package/omni_ai/vendor/prompt_toolkit/completion/__init__.py +43 -0
- package/omni_ai/vendor/prompt_toolkit/completion/base.py +438 -0
- package/omni_ai/vendor/prompt_toolkit/completion/deduplicate.py +45 -0
- package/omni_ai/vendor/prompt_toolkit/completion/filesystem.py +118 -0
- package/omni_ai/vendor/prompt_toolkit/completion/fuzzy_completer.py +213 -0
- package/omni_ai/vendor/prompt_toolkit/completion/nested.py +109 -0
- package/omni_ai/vendor/prompt_toolkit/completion/word_completer.py +94 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/__init__.py +0 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/completers/__init__.py +5 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/completers/system.py +64 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/__init__.py +80 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/compiler.py +579 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/completion.py +100 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/lexer.py +94 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/regex_parser.py +279 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/validation.py +60 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/ssh/__init__.py +8 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/ssh/server.py +178 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/telnet/__init__.py +7 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/telnet/log.py +13 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/telnet/protocol.py +209 -0
- package/omni_ai/vendor/prompt_toolkit/contrib/telnet/server.py +428 -0
- package/omni_ai/vendor/prompt_toolkit/cursor_shapes.py +117 -0
- package/omni_ai/vendor/prompt_toolkit/data_structures.py +18 -0
- package/omni_ai/vendor/prompt_toolkit/document.py +1182 -0
- package/omni_ai/vendor/prompt_toolkit/enums.py +19 -0
- package/omni_ai/vendor/prompt_toolkit/eventloop/__init__.py +31 -0
- package/omni_ai/vendor/prompt_toolkit/eventloop/async_generator.py +125 -0
- package/omni_ai/vendor/prompt_toolkit/eventloop/inputhook.py +191 -0
- package/omni_ai/vendor/prompt_toolkit/eventloop/utils.py +101 -0
- package/omni_ai/vendor/prompt_toolkit/eventloop/win32.py +72 -0
- package/omni_ai/vendor/prompt_toolkit/filters/__init__.py +71 -0
- package/omni_ai/vendor/prompt_toolkit/filters/app.py +419 -0
- package/omni_ai/vendor/prompt_toolkit/filters/base.py +261 -0
- package/omni_ai/vendor/prompt_toolkit/filters/cli.py +65 -0
- package/omni_ai/vendor/prompt_toolkit/filters/utils.py +41 -0
- package/omni_ai/vendor/prompt_toolkit/formatted_text/__init__.py +59 -0
- package/omni_ai/vendor/prompt_toolkit/formatted_text/ansi.py +297 -0
- package/omni_ai/vendor/prompt_toolkit/formatted_text/base.py +179 -0
- package/omni_ai/vendor/prompt_toolkit/formatted_text/html.py +145 -0
- package/omni_ai/vendor/prompt_toolkit/formatted_text/pygments.py +32 -0
- package/omni_ai/vendor/prompt_toolkit/formatted_text/utils.py +103 -0
- package/omni_ai/vendor/prompt_toolkit/history.py +306 -0
- package/omni_ai/vendor/prompt_toolkit/input/__init__.py +14 -0
- package/omni_ai/vendor/prompt_toolkit/input/ansi_escape_sequences.py +344 -0
- package/omni_ai/vendor/prompt_toolkit/input/base.py +153 -0
- package/omni_ai/vendor/prompt_toolkit/input/defaults.py +79 -0
- package/omni_ai/vendor/prompt_toolkit/input/posix_pipe.py +118 -0
- package/omni_ai/vendor/prompt_toolkit/input/posix_utils.py +97 -0
- package/omni_ai/vendor/prompt_toolkit/input/typeahead.py +78 -0
- package/omni_ai/vendor/prompt_toolkit/input/vt100.py +309 -0
- package/omni_ai/vendor/prompt_toolkit/input/vt100_parser.py +250 -0
- package/omni_ai/vendor/prompt_toolkit/input/win32.py +749 -0
- package/omni_ai/vendor/prompt_toolkit/input/win32_pipe.py +156 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/__init__.py +22 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/__init__.py +0 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/auto_suggest.py +66 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/basic.py +257 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/completion.py +206 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/cpr.py +30 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/emacs.py +563 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/focus.py +26 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/mouse.py +348 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/named_commands.py +691 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/open_in_editor.py +52 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/page_navigation.py +85 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/scroll.py +190 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/search.py +96 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/vi.py +2233 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/defaults.py +63 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/digraphs.py +1378 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/emacs_state.py +36 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/key_bindings.py +670 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/key_processor.py +526 -0
- package/omni_ai/vendor/prompt_toolkit/key_binding/vi_state.py +107 -0
- package/omni_ai/vendor/prompt_toolkit/keys.py +222 -0
- package/omni_ai/vendor/prompt_toolkit/layout/__init__.py +147 -0
- package/omni_ai/vendor/prompt_toolkit/layout/containers.py +2738 -0
- package/omni_ai/vendor/prompt_toolkit/layout/controls.py +945 -0
- package/omni_ai/vendor/prompt_toolkit/layout/dimension.py +220 -0
- package/omni_ai/vendor/prompt_toolkit/layout/dummy.py +40 -0
- package/omni_ai/vendor/prompt_toolkit/layout/layout.py +412 -0
- package/omni_ai/vendor/prompt_toolkit/layout/margins.py +304 -0
- package/omni_ai/vendor/prompt_toolkit/layout/menus.py +748 -0
- package/omni_ai/vendor/prompt_toolkit/layout/mouse_handlers.py +56 -0
- package/omni_ai/vendor/prompt_toolkit/layout/processors.py +1010 -0
- package/omni_ai/vendor/prompt_toolkit/layout/screen.py +323 -0
- package/omni_ai/vendor/prompt_toolkit/layout/scrollable_pane.py +494 -0
- package/omni_ai/vendor/prompt_toolkit/layout/utils.py +80 -0
- package/omni_ai/vendor/prompt_toolkit/lexers/__init__.py +21 -0
- package/omni_ai/vendor/prompt_toolkit/lexers/base.py +85 -0
- package/omni_ai/vendor/prompt_toolkit/lexers/pygments.py +328 -0
- package/omni_ai/vendor/prompt_toolkit/log.py +13 -0
- package/omni_ai/vendor/prompt_toolkit/mouse_events.py +85 -0
- package/omni_ai/vendor/prompt_toolkit/output/__init__.py +15 -0
- package/omni_ai/vendor/prompt_toolkit/output/base.py +332 -0
- package/omni_ai/vendor/prompt_toolkit/output/color_depth.py +64 -0
- package/omni_ai/vendor/prompt_toolkit/output/conemu.py +65 -0
- package/omni_ai/vendor/prompt_toolkit/output/defaults.py +106 -0
- package/omni_ai/vendor/prompt_toolkit/output/flush_stdout.py +87 -0
- package/omni_ai/vendor/prompt_toolkit/output/plain_text.py +143 -0
- package/omni_ai/vendor/prompt_toolkit/output/vt100.py +748 -0
- package/omni_ai/vendor/prompt_toolkit/output/win32.py +683 -0
- package/omni_ai/vendor/prompt_toolkit/output/windows10.py +128 -0
- package/omni_ai/vendor/prompt_toolkit/patch_stdout.py +297 -0
- package/omni_ai/vendor/prompt_toolkit/py.typed +0 -0
- package/omni_ai/vendor/prompt_toolkit/renderer.py +814 -0
- package/omni_ai/vendor/prompt_toolkit/search.py +226 -0
- package/omni_ai/vendor/prompt_toolkit/selection.py +58 -0
- package/omni_ai/vendor/prompt_toolkit/shortcuts/__init__.py +46 -0
- package/omni_ai/vendor/prompt_toolkit/shortcuts/dialogs.py +330 -0
- package/omni_ai/vendor/prompt_toolkit/shortcuts/progress_bar/__init__.py +33 -0
- package/omni_ai/vendor/prompt_toolkit/shortcuts/progress_bar/base.py +449 -0
- package/omni_ai/vendor/prompt_toolkit/shortcuts/progress_bar/formatters.py +431 -0
- package/omni_ai/vendor/prompt_toolkit/shortcuts/prompt.py +1513 -0
- package/omni_ai/vendor/prompt_toolkit/shortcuts/utils.py +239 -0
- package/omni_ai/vendor/prompt_toolkit/styles/__init__.py +67 -0
- package/omni_ai/vendor/prompt_toolkit/styles/base.py +184 -0
- package/omni_ai/vendor/prompt_toolkit/styles/defaults.py +236 -0
- package/omni_ai/vendor/prompt_toolkit/styles/named_colors.py +162 -0
- package/omni_ai/vendor/prompt_toolkit/styles/pygments.py +70 -0
- package/omni_ai/vendor/prompt_toolkit/styles/style.py +401 -0
- package/omni_ai/vendor/prompt_toolkit/styles/style_transformation.py +374 -0
- package/omni_ai/vendor/prompt_toolkit/token.py +9 -0
- package/omni_ai/vendor/prompt_toolkit/utils.py +327 -0
- package/omni_ai/vendor/prompt_toolkit/validation.py +192 -0
- package/omni_ai/vendor/prompt_toolkit/widgets/__init__.py +63 -0
- package/omni_ai/vendor/prompt_toolkit/widgets/base.py +995 -0
- package/omni_ai/vendor/prompt_toolkit/widgets/dialogs.py +108 -0
- package/omni_ai/vendor/prompt_toolkit/widgets/menus.py +374 -0
- package/omni_ai/vendor/prompt_toolkit/widgets/toolbars.py +370 -0
- package/omni_ai/vendor/prompt_toolkit/win32_types.py +229 -0
- package/omni_ai/vendor/wcwidth/LICENSE +27 -0
- package/omni_ai/vendor/wcwidth/__init__.py +29 -0
- package/omni_ai/vendor/wcwidth/table_vs16.py +125 -0
- package/omni_ai/vendor/wcwidth/table_wide.py +1493 -0
- package/omni_ai/vendor/wcwidth/table_zero.py +4843 -0
- package/omni_ai/vendor/wcwidth/unicode_versions.py +38 -0
- package/omni_ai/vendor/wcwidth/wcwidth.py +345 -0
- package/package.json +40 -4
|
@@ -0,0 +1,945 @@
|
|
|
1
|
+
"""
|
|
2
|
+
User interface Controls for the layout.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import time
|
|
8
|
+
from abc import ABCMeta, abstractmethod
|
|
9
|
+
from typing import TYPE_CHECKING, Callable, Hashable, Iterable, NamedTuple
|
|
10
|
+
|
|
11
|
+
from prompt_toolkit.application.current import get_app
|
|
12
|
+
from prompt_toolkit.buffer import Buffer
|
|
13
|
+
from prompt_toolkit.cache import SimpleCache
|
|
14
|
+
from prompt_toolkit.data_structures import Point
|
|
15
|
+
from prompt_toolkit.document import Document
|
|
16
|
+
from prompt_toolkit.filters import FilterOrBool, to_filter
|
|
17
|
+
from prompt_toolkit.formatted_text import (
|
|
18
|
+
AnyFormattedText,
|
|
19
|
+
StyleAndTextTuples,
|
|
20
|
+
to_formatted_text,
|
|
21
|
+
)
|
|
22
|
+
from prompt_toolkit.formatted_text.utils import (
|
|
23
|
+
fragment_list_to_text,
|
|
24
|
+
fragment_list_width,
|
|
25
|
+
split_lines,
|
|
26
|
+
)
|
|
27
|
+
from prompt_toolkit.lexers import Lexer, SimpleLexer
|
|
28
|
+
from prompt_toolkit.mouse_events import MouseButton, MouseEvent, MouseEventType
|
|
29
|
+
from prompt_toolkit.search import SearchState
|
|
30
|
+
from prompt_toolkit.selection import SelectionType
|
|
31
|
+
from prompt_toolkit.utils import get_cwidth
|
|
32
|
+
|
|
33
|
+
from .processors import (
|
|
34
|
+
DisplayMultipleCursors,
|
|
35
|
+
HighlightIncrementalSearchProcessor,
|
|
36
|
+
HighlightSearchProcessor,
|
|
37
|
+
HighlightSelectionProcessor,
|
|
38
|
+
Processor,
|
|
39
|
+
TransformationInput,
|
|
40
|
+
merge_processors,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
if TYPE_CHECKING:
|
|
44
|
+
from prompt_toolkit.key_binding.key_bindings import (
|
|
45
|
+
KeyBindingsBase,
|
|
46
|
+
NotImplementedOrNone,
|
|
47
|
+
)
|
|
48
|
+
from prompt_toolkit.utils import Event
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
__all__ = [
|
|
52
|
+
"BufferControl",
|
|
53
|
+
"SearchBufferControl",
|
|
54
|
+
"DummyControl",
|
|
55
|
+
"FormattedTextControl",
|
|
56
|
+
"UIControl",
|
|
57
|
+
"UIContent",
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
GetLinePrefixCallable = Callable[[int, int], AnyFormattedText]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class UIControl(metaclass=ABCMeta):
|
|
64
|
+
"""
|
|
65
|
+
Base class for all user interface controls.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
def reset(self) -> None:
|
|
69
|
+
# Default reset. (Doesn't have to be implemented.)
|
|
70
|
+
pass
|
|
71
|
+
|
|
72
|
+
def preferred_width(self, max_available_width: int) -> int | None:
|
|
73
|
+
return None
|
|
74
|
+
|
|
75
|
+
def preferred_height(
|
|
76
|
+
self,
|
|
77
|
+
width: int,
|
|
78
|
+
max_available_height: int,
|
|
79
|
+
wrap_lines: bool,
|
|
80
|
+
get_line_prefix: GetLinePrefixCallable | None,
|
|
81
|
+
) -> int | None:
|
|
82
|
+
return None
|
|
83
|
+
|
|
84
|
+
def is_focusable(self) -> bool:
|
|
85
|
+
"""
|
|
86
|
+
Tell whether this user control is focusable.
|
|
87
|
+
"""
|
|
88
|
+
return False
|
|
89
|
+
|
|
90
|
+
@abstractmethod
|
|
91
|
+
def create_content(self, width: int, height: int) -> UIContent:
|
|
92
|
+
"""
|
|
93
|
+
Generate the content for this user control.
|
|
94
|
+
|
|
95
|
+
Returns a :class:`.UIContent` instance.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
def mouse_handler(self, mouse_event: MouseEvent) -> NotImplementedOrNone:
|
|
99
|
+
"""
|
|
100
|
+
Handle mouse events.
|
|
101
|
+
|
|
102
|
+
When `NotImplemented` is returned, it means that the given event is not
|
|
103
|
+
handled by the `UIControl` itself. The `Window` or key bindings can
|
|
104
|
+
decide to handle this event as scrolling or changing focus.
|
|
105
|
+
|
|
106
|
+
:param mouse_event: `MouseEvent` instance.
|
|
107
|
+
"""
|
|
108
|
+
return NotImplemented
|
|
109
|
+
|
|
110
|
+
def move_cursor_down(self) -> None:
|
|
111
|
+
"""
|
|
112
|
+
Request to move the cursor down.
|
|
113
|
+
This happens when scrolling down and the cursor is completely at the
|
|
114
|
+
top.
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
def move_cursor_up(self) -> None:
|
|
118
|
+
"""
|
|
119
|
+
Request to move the cursor up.
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
def get_key_bindings(self) -> KeyBindingsBase | None:
|
|
123
|
+
"""
|
|
124
|
+
The key bindings that are specific for this user control.
|
|
125
|
+
|
|
126
|
+
Return a :class:`.KeyBindings` object if some key bindings are
|
|
127
|
+
specified, or `None` otherwise.
|
|
128
|
+
"""
|
|
129
|
+
|
|
130
|
+
def get_invalidate_events(self) -> Iterable[Event[object]]:
|
|
131
|
+
"""
|
|
132
|
+
Return a list of `Event` objects. This can be a generator.
|
|
133
|
+
(The application collects all these events, in order to bind redraw
|
|
134
|
+
handlers to these events.)
|
|
135
|
+
"""
|
|
136
|
+
return []
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class UIContent:
|
|
140
|
+
"""
|
|
141
|
+
Content generated by a user control. This content consists of a list of
|
|
142
|
+
lines.
|
|
143
|
+
|
|
144
|
+
:param get_line: Callable that takes a line number and returns the current
|
|
145
|
+
line. This is a list of (style_str, text) tuples.
|
|
146
|
+
:param line_count: The number of lines.
|
|
147
|
+
:param cursor_position: a :class:`.Point` for the cursor position.
|
|
148
|
+
:param menu_position: a :class:`.Point` for the menu position.
|
|
149
|
+
:param show_cursor: Make the cursor visible.
|
|
150
|
+
"""
|
|
151
|
+
|
|
152
|
+
def __init__(
|
|
153
|
+
self,
|
|
154
|
+
get_line: Callable[[int], StyleAndTextTuples] = (lambda i: []),
|
|
155
|
+
line_count: int = 0,
|
|
156
|
+
cursor_position: Point | None = None,
|
|
157
|
+
menu_position: Point | None = None,
|
|
158
|
+
show_cursor: bool = True,
|
|
159
|
+
):
|
|
160
|
+
self.get_line = get_line
|
|
161
|
+
self.line_count = line_count
|
|
162
|
+
self.cursor_position = cursor_position or Point(x=0, y=0)
|
|
163
|
+
self.menu_position = menu_position
|
|
164
|
+
self.show_cursor = show_cursor
|
|
165
|
+
|
|
166
|
+
# Cache for line heights. Maps cache key -> height
|
|
167
|
+
self._line_heights_cache: dict[Hashable, int] = {}
|
|
168
|
+
|
|
169
|
+
def __getitem__(self, lineno: int) -> StyleAndTextTuples:
|
|
170
|
+
"Make it iterable (iterate line by line)."
|
|
171
|
+
if lineno < self.line_count:
|
|
172
|
+
return self.get_line(lineno)
|
|
173
|
+
else:
|
|
174
|
+
raise IndexError
|
|
175
|
+
|
|
176
|
+
def get_height_for_line(
|
|
177
|
+
self,
|
|
178
|
+
lineno: int,
|
|
179
|
+
width: int,
|
|
180
|
+
get_line_prefix: GetLinePrefixCallable | None,
|
|
181
|
+
slice_stop: int | None = None,
|
|
182
|
+
) -> int:
|
|
183
|
+
"""
|
|
184
|
+
Return the height that a given line would need if it is rendered in a
|
|
185
|
+
space with the given width (using line wrapping).
|
|
186
|
+
|
|
187
|
+
:param get_line_prefix: None or a `Window.get_line_prefix` callable
|
|
188
|
+
that returns the prefix to be inserted before this line.
|
|
189
|
+
:param slice_stop: Wrap only "line[:slice_stop]" and return that
|
|
190
|
+
partial result. This is needed for scrolling the window correctly
|
|
191
|
+
when line wrapping.
|
|
192
|
+
:returns: The computed height.
|
|
193
|
+
"""
|
|
194
|
+
# Instead of using `get_line_prefix` as key, we use render_counter
|
|
195
|
+
# instead. This is more reliable, because this function could still be
|
|
196
|
+
# the same, while the content would change over time.
|
|
197
|
+
key = get_app().render_counter, lineno, width, slice_stop
|
|
198
|
+
|
|
199
|
+
try:
|
|
200
|
+
return self._line_heights_cache[key]
|
|
201
|
+
except KeyError:
|
|
202
|
+
if width == 0:
|
|
203
|
+
height = 10**8
|
|
204
|
+
else:
|
|
205
|
+
# Calculate line width first.
|
|
206
|
+
line = fragment_list_to_text(self.get_line(lineno))[:slice_stop]
|
|
207
|
+
text_width = get_cwidth(line)
|
|
208
|
+
|
|
209
|
+
if get_line_prefix:
|
|
210
|
+
# Add prefix width.
|
|
211
|
+
text_width += fragment_list_width(
|
|
212
|
+
to_formatted_text(get_line_prefix(lineno, 0))
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
# Slower path: compute path when there's a line prefix.
|
|
216
|
+
height = 1
|
|
217
|
+
|
|
218
|
+
# Keep wrapping as long as the line doesn't fit.
|
|
219
|
+
# Keep adding new prefixes for every wrapped line.
|
|
220
|
+
while text_width > width:
|
|
221
|
+
height += 1
|
|
222
|
+
text_width -= width
|
|
223
|
+
|
|
224
|
+
fragments2 = to_formatted_text(
|
|
225
|
+
get_line_prefix(lineno, height - 1)
|
|
226
|
+
)
|
|
227
|
+
prefix_width = get_cwidth(fragment_list_to_text(fragments2))
|
|
228
|
+
|
|
229
|
+
if prefix_width >= width: # Prefix doesn't fit.
|
|
230
|
+
height = 10**8
|
|
231
|
+
break
|
|
232
|
+
|
|
233
|
+
text_width += prefix_width
|
|
234
|
+
else:
|
|
235
|
+
# Fast path: compute height when there's no line prefix.
|
|
236
|
+
try:
|
|
237
|
+
quotient, remainder = divmod(text_width, width)
|
|
238
|
+
except ZeroDivisionError:
|
|
239
|
+
height = 10**8
|
|
240
|
+
else:
|
|
241
|
+
if remainder:
|
|
242
|
+
quotient += 1 # Like math.ceil.
|
|
243
|
+
height = max(1, quotient)
|
|
244
|
+
|
|
245
|
+
# Cache and return
|
|
246
|
+
self._line_heights_cache[key] = height
|
|
247
|
+
return height
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
class FormattedTextControl(UIControl):
|
|
251
|
+
"""
|
|
252
|
+
Control that displays formatted text. This can be either plain text, an
|
|
253
|
+
:class:`~prompt_toolkit.formatted_text.HTML` object an
|
|
254
|
+
:class:`~prompt_toolkit.formatted_text.ANSI` object, a list of ``(style_str,
|
|
255
|
+
text)`` tuples or a callable that takes no argument and returns one of
|
|
256
|
+
those, depending on how you prefer to do the formatting. See
|
|
257
|
+
``prompt_toolkit.layout.formatted_text`` for more information.
|
|
258
|
+
|
|
259
|
+
(It's mostly optimized for rather small widgets, like toolbars, menus, etc...)
|
|
260
|
+
|
|
261
|
+
When this UI control has the focus, the cursor will be shown in the upper
|
|
262
|
+
left corner of this control by default. There are two ways for specifying
|
|
263
|
+
the cursor position:
|
|
264
|
+
|
|
265
|
+
- Pass a `get_cursor_position` function which returns a `Point` instance
|
|
266
|
+
with the current cursor position.
|
|
267
|
+
|
|
268
|
+
- If the (formatted) text is passed as a list of ``(style, text)`` tuples
|
|
269
|
+
and there is one that looks like ``('[SetCursorPosition]', '')``, then
|
|
270
|
+
this will specify the cursor position.
|
|
271
|
+
|
|
272
|
+
Mouse support:
|
|
273
|
+
|
|
274
|
+
The list of fragments can also contain tuples of three items, looking like:
|
|
275
|
+
(style_str, text, handler). When mouse support is enabled and the user
|
|
276
|
+
clicks on this fragment, then the given handler is called. That handler
|
|
277
|
+
should accept two inputs: (Application, MouseEvent) and it should
|
|
278
|
+
either handle the event or return `NotImplemented` in case we want the
|
|
279
|
+
containing Window to handle this event.
|
|
280
|
+
|
|
281
|
+
:param focusable: `bool` or :class:`.Filter`: Tell whether this control is
|
|
282
|
+
focusable.
|
|
283
|
+
|
|
284
|
+
:param text: Text or formatted text to be displayed.
|
|
285
|
+
:param style: Style string applied to the content. (If you want to style
|
|
286
|
+
the whole :class:`~prompt_toolkit.layout.Window`, pass the style to the
|
|
287
|
+
:class:`~prompt_toolkit.layout.Window` instead.)
|
|
288
|
+
:param key_bindings: a :class:`.KeyBindings` object.
|
|
289
|
+
:param get_cursor_position: A callable that returns the cursor position as
|
|
290
|
+
a `Point` instance.
|
|
291
|
+
"""
|
|
292
|
+
|
|
293
|
+
def __init__(
|
|
294
|
+
self,
|
|
295
|
+
text: AnyFormattedText = "",
|
|
296
|
+
style: str = "",
|
|
297
|
+
focusable: FilterOrBool = False,
|
|
298
|
+
key_bindings: KeyBindingsBase | None = None,
|
|
299
|
+
show_cursor: bool = True,
|
|
300
|
+
modal: bool = False,
|
|
301
|
+
get_cursor_position: Callable[[], Point | None] | None = None,
|
|
302
|
+
) -> None:
|
|
303
|
+
self.text = text # No type check on 'text'. This is done dynamically.
|
|
304
|
+
self.style = style
|
|
305
|
+
self.focusable = to_filter(focusable)
|
|
306
|
+
|
|
307
|
+
# Key bindings.
|
|
308
|
+
self.key_bindings = key_bindings
|
|
309
|
+
self.show_cursor = show_cursor
|
|
310
|
+
self.modal = modal
|
|
311
|
+
self.get_cursor_position = get_cursor_position
|
|
312
|
+
|
|
313
|
+
#: Cache for the content.
|
|
314
|
+
self._content_cache: SimpleCache[Hashable, UIContent] = SimpleCache(maxsize=18)
|
|
315
|
+
self._fragment_cache: SimpleCache[int, StyleAndTextTuples] = SimpleCache(
|
|
316
|
+
maxsize=1
|
|
317
|
+
)
|
|
318
|
+
# Only cache one fragment list. We don't need the previous item.
|
|
319
|
+
|
|
320
|
+
# Render info for the mouse support.
|
|
321
|
+
self._fragments: StyleAndTextTuples | None = None
|
|
322
|
+
|
|
323
|
+
def reset(self) -> None:
|
|
324
|
+
self._fragments = None
|
|
325
|
+
|
|
326
|
+
def is_focusable(self) -> bool:
|
|
327
|
+
return self.focusable()
|
|
328
|
+
|
|
329
|
+
def __repr__(self) -> str:
|
|
330
|
+
return f"{self.__class__.__name__}({self.text!r})"
|
|
331
|
+
|
|
332
|
+
def _get_formatted_text_cached(self) -> StyleAndTextTuples:
|
|
333
|
+
"""
|
|
334
|
+
Get fragments, but only retrieve fragments once during one render run.
|
|
335
|
+
(This function is called several times during one rendering, because
|
|
336
|
+
we also need those for calculating the dimensions.)
|
|
337
|
+
"""
|
|
338
|
+
return self._fragment_cache.get(
|
|
339
|
+
get_app().render_counter, lambda: to_formatted_text(self.text, self.style)
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
def preferred_width(self, max_available_width: int) -> int:
|
|
343
|
+
"""
|
|
344
|
+
Return the preferred width for this control.
|
|
345
|
+
That is the width of the longest line.
|
|
346
|
+
"""
|
|
347
|
+
text = fragment_list_to_text(self._get_formatted_text_cached())
|
|
348
|
+
line_lengths = [get_cwidth(l) for l in text.split("\n")]
|
|
349
|
+
return max(line_lengths)
|
|
350
|
+
|
|
351
|
+
def preferred_height(
|
|
352
|
+
self,
|
|
353
|
+
width: int,
|
|
354
|
+
max_available_height: int,
|
|
355
|
+
wrap_lines: bool,
|
|
356
|
+
get_line_prefix: GetLinePrefixCallable | None,
|
|
357
|
+
) -> int | None:
|
|
358
|
+
"""
|
|
359
|
+
Return the preferred height for this control.
|
|
360
|
+
"""
|
|
361
|
+
content = self.create_content(width, None)
|
|
362
|
+
if wrap_lines:
|
|
363
|
+
height = 0
|
|
364
|
+
for i in range(content.line_count):
|
|
365
|
+
height += content.get_height_for_line(i, width, get_line_prefix)
|
|
366
|
+
if height >= max_available_height:
|
|
367
|
+
return max_available_height
|
|
368
|
+
return height
|
|
369
|
+
else:
|
|
370
|
+
return content.line_count
|
|
371
|
+
|
|
372
|
+
def create_content(self, width: int, height: int | None) -> UIContent:
|
|
373
|
+
# Get fragments
|
|
374
|
+
fragments_with_mouse_handlers = self._get_formatted_text_cached()
|
|
375
|
+
fragment_lines_with_mouse_handlers = list(
|
|
376
|
+
split_lines(fragments_with_mouse_handlers)
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
# Strip mouse handlers from fragments.
|
|
380
|
+
fragment_lines: list[StyleAndTextTuples] = [
|
|
381
|
+
[(item[0], item[1]) for item in line]
|
|
382
|
+
for line in fragment_lines_with_mouse_handlers
|
|
383
|
+
]
|
|
384
|
+
|
|
385
|
+
# Keep track of the fragments with mouse handler, for later use in
|
|
386
|
+
# `mouse_handler`.
|
|
387
|
+
self._fragments = fragments_with_mouse_handlers
|
|
388
|
+
|
|
389
|
+
# If there is a `[SetCursorPosition]` in the fragment list, set the
|
|
390
|
+
# cursor position here.
|
|
391
|
+
def get_cursor_position(
|
|
392
|
+
fragment: str = "[SetCursorPosition]",
|
|
393
|
+
) -> Point | None:
|
|
394
|
+
for y, line in enumerate(fragment_lines):
|
|
395
|
+
x = 0
|
|
396
|
+
for style_str, text, *_ in line:
|
|
397
|
+
if fragment in style_str:
|
|
398
|
+
return Point(x=x, y=y)
|
|
399
|
+
x += len(text)
|
|
400
|
+
return None
|
|
401
|
+
|
|
402
|
+
# If there is a `[SetMenuPosition]`, set the menu over here.
|
|
403
|
+
def get_menu_position() -> Point | None:
|
|
404
|
+
return get_cursor_position("[SetMenuPosition]")
|
|
405
|
+
|
|
406
|
+
cursor_position = (self.get_cursor_position or get_cursor_position)()
|
|
407
|
+
|
|
408
|
+
# Create content, or take it from the cache.
|
|
409
|
+
key = (tuple(fragments_with_mouse_handlers), width, cursor_position)
|
|
410
|
+
|
|
411
|
+
def get_content() -> UIContent:
|
|
412
|
+
return UIContent(
|
|
413
|
+
get_line=lambda i: fragment_lines[i],
|
|
414
|
+
line_count=len(fragment_lines),
|
|
415
|
+
show_cursor=self.show_cursor,
|
|
416
|
+
cursor_position=cursor_position,
|
|
417
|
+
menu_position=get_menu_position(),
|
|
418
|
+
)
|
|
419
|
+
|
|
420
|
+
return self._content_cache.get(key, get_content)
|
|
421
|
+
|
|
422
|
+
def mouse_handler(self, mouse_event: MouseEvent) -> NotImplementedOrNone:
|
|
423
|
+
"""
|
|
424
|
+
Handle mouse events.
|
|
425
|
+
|
|
426
|
+
(When the fragment list contained mouse handlers and the user clicked on
|
|
427
|
+
on any of these, the matching handler is called. This handler can still
|
|
428
|
+
return `NotImplemented` in case we want the
|
|
429
|
+
:class:`~prompt_toolkit.layout.Window` to handle this particular
|
|
430
|
+
event.)
|
|
431
|
+
"""
|
|
432
|
+
if self._fragments:
|
|
433
|
+
# Read the generator.
|
|
434
|
+
fragments_for_line = list(split_lines(self._fragments))
|
|
435
|
+
|
|
436
|
+
try:
|
|
437
|
+
fragments = fragments_for_line[mouse_event.position.y]
|
|
438
|
+
except IndexError:
|
|
439
|
+
return NotImplemented
|
|
440
|
+
else:
|
|
441
|
+
# Find position in the fragment list.
|
|
442
|
+
xpos = mouse_event.position.x
|
|
443
|
+
|
|
444
|
+
# Find mouse handler for this character.
|
|
445
|
+
count = 0
|
|
446
|
+
for item in fragments:
|
|
447
|
+
count += len(item[1])
|
|
448
|
+
if count > xpos:
|
|
449
|
+
if len(item) >= 3:
|
|
450
|
+
# Handler found. Call it.
|
|
451
|
+
# (Handler can return NotImplemented, so return
|
|
452
|
+
# that result.)
|
|
453
|
+
handler = item[2]
|
|
454
|
+
return handler(mouse_event)
|
|
455
|
+
else:
|
|
456
|
+
break
|
|
457
|
+
|
|
458
|
+
# Otherwise, don't handle here.
|
|
459
|
+
return NotImplemented
|
|
460
|
+
|
|
461
|
+
def is_modal(self) -> bool:
|
|
462
|
+
return self.modal
|
|
463
|
+
|
|
464
|
+
def get_key_bindings(self) -> KeyBindingsBase | None:
|
|
465
|
+
return self.key_bindings
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
class DummyControl(UIControl):
|
|
469
|
+
"""
|
|
470
|
+
A dummy control object that doesn't paint any content.
|
|
471
|
+
|
|
472
|
+
Useful for filling a :class:`~prompt_toolkit.layout.Window`. (The
|
|
473
|
+
`fragment` and `char` attributes of the `Window` class can be used to
|
|
474
|
+
define the filling.)
|
|
475
|
+
"""
|
|
476
|
+
|
|
477
|
+
def create_content(self, width: int, height: int) -> UIContent:
|
|
478
|
+
def get_line(i: int) -> StyleAndTextTuples:
|
|
479
|
+
return []
|
|
480
|
+
|
|
481
|
+
return UIContent(get_line=get_line, line_count=100**100) # Something very big.
|
|
482
|
+
|
|
483
|
+
def is_focusable(self) -> bool:
|
|
484
|
+
return False
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
class _ProcessedLine(NamedTuple):
|
|
488
|
+
fragments: StyleAndTextTuples
|
|
489
|
+
source_to_display: Callable[[int], int]
|
|
490
|
+
display_to_source: Callable[[int], int]
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
class BufferControl(UIControl):
|
|
494
|
+
"""
|
|
495
|
+
Control for visualizing the content of a :class:`.Buffer`.
|
|
496
|
+
|
|
497
|
+
:param buffer: The :class:`.Buffer` object to be displayed.
|
|
498
|
+
:param input_processors: A list of
|
|
499
|
+
:class:`~prompt_toolkit.layout.processors.Processor` objects.
|
|
500
|
+
:param include_default_input_processors: When True, include the default
|
|
501
|
+
processors for highlighting of selection, search and displaying of
|
|
502
|
+
multiple cursors.
|
|
503
|
+
:param lexer: :class:`.Lexer` instance for syntax highlighting.
|
|
504
|
+
:param preview_search: `bool` or :class:`.Filter`: Show search while
|
|
505
|
+
typing. When this is `True`, probably you want to add a
|
|
506
|
+
``HighlightIncrementalSearchProcessor`` as well. Otherwise only the
|
|
507
|
+
cursor position will move, but the text won't be highlighted.
|
|
508
|
+
:param focusable: `bool` or :class:`.Filter`: Tell whether this control is focusable.
|
|
509
|
+
:param focus_on_click: Focus this buffer when it's click, but not yet focused.
|
|
510
|
+
:param key_bindings: a :class:`.KeyBindings` object.
|
|
511
|
+
"""
|
|
512
|
+
|
|
513
|
+
def __init__(
|
|
514
|
+
self,
|
|
515
|
+
buffer: Buffer | None = None,
|
|
516
|
+
input_processors: list[Processor] | None = None,
|
|
517
|
+
include_default_input_processors: bool = True,
|
|
518
|
+
lexer: Lexer | None = None,
|
|
519
|
+
preview_search: FilterOrBool = False,
|
|
520
|
+
focusable: FilterOrBool = True,
|
|
521
|
+
search_buffer_control: (
|
|
522
|
+
None | SearchBufferControl | Callable[[], SearchBufferControl]
|
|
523
|
+
) = None,
|
|
524
|
+
menu_position: Callable[[], int | None] | None = None,
|
|
525
|
+
focus_on_click: FilterOrBool = False,
|
|
526
|
+
key_bindings: KeyBindingsBase | None = None,
|
|
527
|
+
):
|
|
528
|
+
self.input_processors = input_processors
|
|
529
|
+
self.include_default_input_processors = include_default_input_processors
|
|
530
|
+
|
|
531
|
+
self.default_input_processors = [
|
|
532
|
+
HighlightSearchProcessor(),
|
|
533
|
+
HighlightIncrementalSearchProcessor(),
|
|
534
|
+
HighlightSelectionProcessor(),
|
|
535
|
+
DisplayMultipleCursors(),
|
|
536
|
+
]
|
|
537
|
+
|
|
538
|
+
self.preview_search = to_filter(preview_search)
|
|
539
|
+
self.focusable = to_filter(focusable)
|
|
540
|
+
self.focus_on_click = to_filter(focus_on_click)
|
|
541
|
+
|
|
542
|
+
self.buffer = buffer or Buffer()
|
|
543
|
+
self.menu_position = menu_position
|
|
544
|
+
self.lexer = lexer or SimpleLexer()
|
|
545
|
+
self.key_bindings = key_bindings
|
|
546
|
+
self._search_buffer_control = search_buffer_control
|
|
547
|
+
|
|
548
|
+
#: Cache for the lexer.
|
|
549
|
+
#: Often, due to cursor movement, undo/redo and window resizing
|
|
550
|
+
#: operations, it happens that a short time, the same document has to be
|
|
551
|
+
#: lexed. This is a fairly easy way to cache such an expensive operation.
|
|
552
|
+
self._fragment_cache: SimpleCache[
|
|
553
|
+
Hashable, Callable[[int], StyleAndTextTuples]
|
|
554
|
+
] = SimpleCache(maxsize=8)
|
|
555
|
+
|
|
556
|
+
self._last_click_timestamp: float | None = None
|
|
557
|
+
self._last_get_processed_line: Callable[[int], _ProcessedLine] | None = None
|
|
558
|
+
|
|
559
|
+
def __repr__(self) -> str:
|
|
560
|
+
return f"<{self.__class__.__name__} buffer={self.buffer!r} at {id(self)!r}>"
|
|
561
|
+
|
|
562
|
+
@property
|
|
563
|
+
def search_buffer_control(self) -> SearchBufferControl | None:
|
|
564
|
+
result: SearchBufferControl | None
|
|
565
|
+
|
|
566
|
+
if callable(self._search_buffer_control):
|
|
567
|
+
result = self._search_buffer_control()
|
|
568
|
+
else:
|
|
569
|
+
result = self._search_buffer_control
|
|
570
|
+
|
|
571
|
+
assert result is None or isinstance(result, SearchBufferControl)
|
|
572
|
+
return result
|
|
573
|
+
|
|
574
|
+
@property
|
|
575
|
+
def search_buffer(self) -> Buffer | None:
|
|
576
|
+
control = self.search_buffer_control
|
|
577
|
+
if control is not None:
|
|
578
|
+
return control.buffer
|
|
579
|
+
return None
|
|
580
|
+
|
|
581
|
+
@property
|
|
582
|
+
def search_state(self) -> SearchState:
|
|
583
|
+
"""
|
|
584
|
+
Return the `SearchState` for searching this `BufferControl`. This is
|
|
585
|
+
always associated with the search control. If one search bar is used
|
|
586
|
+
for searching multiple `BufferControls`, then they share the same
|
|
587
|
+
`SearchState`.
|
|
588
|
+
"""
|
|
589
|
+
search_buffer_control = self.search_buffer_control
|
|
590
|
+
if search_buffer_control:
|
|
591
|
+
return search_buffer_control.searcher_search_state
|
|
592
|
+
else:
|
|
593
|
+
return SearchState()
|
|
594
|
+
|
|
595
|
+
def is_focusable(self) -> bool:
|
|
596
|
+
return self.focusable()
|
|
597
|
+
|
|
598
|
+
def preferred_width(self, max_available_width: int) -> int | None:
|
|
599
|
+
"""
|
|
600
|
+
This should return the preferred width.
|
|
601
|
+
|
|
602
|
+
Note: We don't specify a preferred width according to the content,
|
|
603
|
+
because it would be too expensive. Calculating the preferred
|
|
604
|
+
width can be done by calculating the longest line, but this would
|
|
605
|
+
require applying all the processors to each line. This is
|
|
606
|
+
unfeasible for a larger document, and doing it for small
|
|
607
|
+
documents only would result in inconsistent behavior.
|
|
608
|
+
"""
|
|
609
|
+
return None
|
|
610
|
+
|
|
611
|
+
def preferred_height(
|
|
612
|
+
self,
|
|
613
|
+
width: int,
|
|
614
|
+
max_available_height: int,
|
|
615
|
+
wrap_lines: bool,
|
|
616
|
+
get_line_prefix: GetLinePrefixCallable | None,
|
|
617
|
+
) -> int | None:
|
|
618
|
+
# Calculate the content height, if it was drawn on a screen with the
|
|
619
|
+
# given width.
|
|
620
|
+
height = 0
|
|
621
|
+
content = self.create_content(width, height=1) # Pass a dummy '1' as height.
|
|
622
|
+
|
|
623
|
+
# When line wrapping is off, the height should be equal to the amount
|
|
624
|
+
# of lines.
|
|
625
|
+
if not wrap_lines:
|
|
626
|
+
return content.line_count
|
|
627
|
+
|
|
628
|
+
# When the number of lines exceeds the max_available_height, just
|
|
629
|
+
# return max_available_height. No need to calculate anything.
|
|
630
|
+
if content.line_count >= max_available_height:
|
|
631
|
+
return max_available_height
|
|
632
|
+
|
|
633
|
+
for i in range(content.line_count):
|
|
634
|
+
height += content.get_height_for_line(i, width, get_line_prefix)
|
|
635
|
+
|
|
636
|
+
if height >= max_available_height:
|
|
637
|
+
return max_available_height
|
|
638
|
+
|
|
639
|
+
return height
|
|
640
|
+
|
|
641
|
+
def _get_formatted_text_for_line_func(
|
|
642
|
+
self, document: Document
|
|
643
|
+
) -> Callable[[int], StyleAndTextTuples]:
|
|
644
|
+
"""
|
|
645
|
+
Create a function that returns the fragments for a given line.
|
|
646
|
+
"""
|
|
647
|
+
|
|
648
|
+
# Cache using `document.text`.
|
|
649
|
+
def get_formatted_text_for_line() -> Callable[[int], StyleAndTextTuples]:
|
|
650
|
+
return self.lexer.lex_document(document)
|
|
651
|
+
|
|
652
|
+
key = (document.text, self.lexer.invalidation_hash())
|
|
653
|
+
return self._fragment_cache.get(key, get_formatted_text_for_line)
|
|
654
|
+
|
|
655
|
+
def _create_get_processed_line_func(
|
|
656
|
+
self, document: Document, width: int, height: int
|
|
657
|
+
) -> Callable[[int], _ProcessedLine]:
|
|
658
|
+
"""
|
|
659
|
+
Create a function that takes a line number of the current document and
|
|
660
|
+
returns a _ProcessedLine(processed_fragments, source_to_display, display_to_source)
|
|
661
|
+
tuple.
|
|
662
|
+
"""
|
|
663
|
+
# Merge all input processors together.
|
|
664
|
+
input_processors = self.input_processors or []
|
|
665
|
+
if self.include_default_input_processors:
|
|
666
|
+
input_processors = self.default_input_processors + input_processors
|
|
667
|
+
|
|
668
|
+
merged_processor = merge_processors(input_processors)
|
|
669
|
+
|
|
670
|
+
def transform(lineno: int, fragments: StyleAndTextTuples) -> _ProcessedLine:
|
|
671
|
+
"Transform the fragments for a given line number."
|
|
672
|
+
|
|
673
|
+
# Get cursor position at this line.
|
|
674
|
+
def source_to_display(i: int) -> int:
|
|
675
|
+
"""X position from the buffer to the x position in the
|
|
676
|
+
processed fragment list. By default, we start from the 'identity'
|
|
677
|
+
operation."""
|
|
678
|
+
return i
|
|
679
|
+
|
|
680
|
+
transformation = merged_processor.apply_transformation(
|
|
681
|
+
TransformationInput(
|
|
682
|
+
self, document, lineno, source_to_display, fragments, width, height
|
|
683
|
+
)
|
|
684
|
+
)
|
|
685
|
+
|
|
686
|
+
return _ProcessedLine(
|
|
687
|
+
transformation.fragments,
|
|
688
|
+
transformation.source_to_display,
|
|
689
|
+
transformation.display_to_source,
|
|
690
|
+
)
|
|
691
|
+
|
|
692
|
+
def create_func() -> Callable[[int], _ProcessedLine]:
|
|
693
|
+
get_line = self._get_formatted_text_for_line_func(document)
|
|
694
|
+
cache: dict[int, _ProcessedLine] = {}
|
|
695
|
+
|
|
696
|
+
def get_processed_line(i: int) -> _ProcessedLine:
|
|
697
|
+
try:
|
|
698
|
+
return cache[i]
|
|
699
|
+
except KeyError:
|
|
700
|
+
processed_line = transform(i, get_line(i))
|
|
701
|
+
cache[i] = processed_line
|
|
702
|
+
return processed_line
|
|
703
|
+
|
|
704
|
+
return get_processed_line
|
|
705
|
+
|
|
706
|
+
return create_func()
|
|
707
|
+
|
|
708
|
+
def create_content(
|
|
709
|
+
self, width: int, height: int, preview_search: bool = False
|
|
710
|
+
) -> UIContent:
|
|
711
|
+
"""
|
|
712
|
+
Create a UIContent.
|
|
713
|
+
"""
|
|
714
|
+
buffer = self.buffer
|
|
715
|
+
|
|
716
|
+
# Trigger history loading of the buffer. We do this during the
|
|
717
|
+
# rendering of the UI here, because it needs to happen when an
|
|
718
|
+
# `Application` with its event loop is running. During the rendering of
|
|
719
|
+
# the buffer control is the earliest place we can achieve this, where
|
|
720
|
+
# we're sure the right event loop is active, and don't require user
|
|
721
|
+
# interaction (like in a key binding).
|
|
722
|
+
buffer.load_history_if_not_yet_loaded()
|
|
723
|
+
|
|
724
|
+
# Get the document to be shown. If we are currently searching (the
|
|
725
|
+
# search buffer has focus, and the preview_search filter is enabled),
|
|
726
|
+
# then use the search document, which has possibly a different
|
|
727
|
+
# text/cursor position.)
|
|
728
|
+
search_control = self.search_buffer_control
|
|
729
|
+
preview_now = preview_search or bool(
|
|
730
|
+
# Only if this feature is enabled.
|
|
731
|
+
self.preview_search()
|
|
732
|
+
and
|
|
733
|
+
# And something was typed in the associated search field.
|
|
734
|
+
search_control
|
|
735
|
+
and search_control.buffer.text
|
|
736
|
+
and
|
|
737
|
+
# And we are searching in this control. (Many controls can point to
|
|
738
|
+
# the same search field, like in Pyvim.)
|
|
739
|
+
get_app().layout.search_target_buffer_control == self
|
|
740
|
+
)
|
|
741
|
+
|
|
742
|
+
if preview_now and search_control is not None:
|
|
743
|
+
ss = self.search_state
|
|
744
|
+
|
|
745
|
+
document = buffer.document_for_search(
|
|
746
|
+
SearchState(
|
|
747
|
+
text=search_control.buffer.text,
|
|
748
|
+
direction=ss.direction,
|
|
749
|
+
ignore_case=ss.ignore_case,
|
|
750
|
+
)
|
|
751
|
+
)
|
|
752
|
+
else:
|
|
753
|
+
document = buffer.document
|
|
754
|
+
|
|
755
|
+
get_processed_line = self._create_get_processed_line_func(
|
|
756
|
+
document, width, height
|
|
757
|
+
)
|
|
758
|
+
self._last_get_processed_line = get_processed_line
|
|
759
|
+
|
|
760
|
+
def translate_rowcol(row: int, col: int) -> Point:
|
|
761
|
+
"Return the content column for this coordinate."
|
|
762
|
+
return Point(x=get_processed_line(row).source_to_display(col), y=row)
|
|
763
|
+
|
|
764
|
+
def get_line(i: int) -> StyleAndTextTuples:
|
|
765
|
+
"Return the fragments for a given line number."
|
|
766
|
+
fragments = get_processed_line(i).fragments
|
|
767
|
+
|
|
768
|
+
# Add a space at the end, because that is a possible cursor
|
|
769
|
+
# position. (When inserting after the input.) We should do this on
|
|
770
|
+
# all the lines, not just the line containing the cursor. (Because
|
|
771
|
+
# otherwise, line wrapping/scrolling could change when moving the
|
|
772
|
+
# cursor around.)
|
|
773
|
+
fragments = fragments + [("", " ")]
|
|
774
|
+
return fragments
|
|
775
|
+
|
|
776
|
+
content = UIContent(
|
|
777
|
+
get_line=get_line,
|
|
778
|
+
line_count=document.line_count,
|
|
779
|
+
cursor_position=translate_rowcol(
|
|
780
|
+
document.cursor_position_row, document.cursor_position_col
|
|
781
|
+
),
|
|
782
|
+
)
|
|
783
|
+
|
|
784
|
+
# If there is an auto completion going on, use that start point for a
|
|
785
|
+
# pop-up menu position. (But only when this buffer has the focus --
|
|
786
|
+
# there is only one place for a menu, determined by the focused buffer.)
|
|
787
|
+
if get_app().layout.current_control == self:
|
|
788
|
+
menu_position = self.menu_position() if self.menu_position else None
|
|
789
|
+
if menu_position is not None:
|
|
790
|
+
assert isinstance(menu_position, int)
|
|
791
|
+
menu_row, menu_col = buffer.document.translate_index_to_position(
|
|
792
|
+
menu_position
|
|
793
|
+
)
|
|
794
|
+
content.menu_position = translate_rowcol(menu_row, menu_col)
|
|
795
|
+
elif buffer.complete_state:
|
|
796
|
+
# Position for completion menu.
|
|
797
|
+
# Note: We use 'min', because the original cursor position could be
|
|
798
|
+
# behind the input string when the actual completion is for
|
|
799
|
+
# some reason shorter than the text we had before. (A completion
|
|
800
|
+
# can change and shorten the input.)
|
|
801
|
+
menu_row, menu_col = buffer.document.translate_index_to_position(
|
|
802
|
+
min(
|
|
803
|
+
buffer.cursor_position,
|
|
804
|
+
buffer.complete_state.original_document.cursor_position,
|
|
805
|
+
)
|
|
806
|
+
)
|
|
807
|
+
content.menu_position = translate_rowcol(menu_row, menu_col)
|
|
808
|
+
else:
|
|
809
|
+
content.menu_position = None
|
|
810
|
+
|
|
811
|
+
return content
|
|
812
|
+
|
|
813
|
+
def mouse_handler(self, mouse_event: MouseEvent) -> NotImplementedOrNone:
|
|
814
|
+
"""
|
|
815
|
+
Mouse handler for this control.
|
|
816
|
+
"""
|
|
817
|
+
buffer = self.buffer
|
|
818
|
+
position = mouse_event.position
|
|
819
|
+
|
|
820
|
+
# Focus buffer when clicked.
|
|
821
|
+
if get_app().layout.current_control == self:
|
|
822
|
+
if self._last_get_processed_line:
|
|
823
|
+
processed_line = self._last_get_processed_line(position.y)
|
|
824
|
+
|
|
825
|
+
# Translate coordinates back to the cursor position of the
|
|
826
|
+
# original input.
|
|
827
|
+
xpos = processed_line.display_to_source(position.x)
|
|
828
|
+
index = buffer.document.translate_row_col_to_index(position.y, xpos)
|
|
829
|
+
|
|
830
|
+
# Set the cursor position.
|
|
831
|
+
if mouse_event.event_type == MouseEventType.MOUSE_DOWN:
|
|
832
|
+
buffer.exit_selection()
|
|
833
|
+
buffer.cursor_position = index
|
|
834
|
+
|
|
835
|
+
elif (
|
|
836
|
+
mouse_event.event_type == MouseEventType.MOUSE_MOVE
|
|
837
|
+
and mouse_event.button != MouseButton.NONE
|
|
838
|
+
):
|
|
839
|
+
# Click and drag to highlight a selection
|
|
840
|
+
if (
|
|
841
|
+
buffer.selection_state is None
|
|
842
|
+
and abs(buffer.cursor_position - index) > 0
|
|
843
|
+
):
|
|
844
|
+
buffer.start_selection(selection_type=SelectionType.CHARACTERS)
|
|
845
|
+
buffer.cursor_position = index
|
|
846
|
+
|
|
847
|
+
elif mouse_event.event_type == MouseEventType.MOUSE_UP:
|
|
848
|
+
# When the cursor was moved to another place, select the text.
|
|
849
|
+
# (The >1 is actually a small but acceptable workaround for
|
|
850
|
+
# selecting text in Vi navigation mode. In navigation mode,
|
|
851
|
+
# the cursor can never be after the text, so the cursor
|
|
852
|
+
# will be repositioned automatically.)
|
|
853
|
+
if abs(buffer.cursor_position - index) > 1:
|
|
854
|
+
if buffer.selection_state is None:
|
|
855
|
+
buffer.start_selection(
|
|
856
|
+
selection_type=SelectionType.CHARACTERS
|
|
857
|
+
)
|
|
858
|
+
buffer.cursor_position = index
|
|
859
|
+
|
|
860
|
+
# Select word around cursor on double click.
|
|
861
|
+
# Two MOUSE_UP events in a short timespan are considered a double click.
|
|
862
|
+
double_click = (
|
|
863
|
+
self._last_click_timestamp
|
|
864
|
+
and time.time() - self._last_click_timestamp < 0.3
|
|
865
|
+
)
|
|
866
|
+
self._last_click_timestamp = time.time()
|
|
867
|
+
|
|
868
|
+
if double_click:
|
|
869
|
+
start, end = buffer.document.find_boundaries_of_current_word()
|
|
870
|
+
buffer.cursor_position += start
|
|
871
|
+
buffer.start_selection(selection_type=SelectionType.CHARACTERS)
|
|
872
|
+
buffer.cursor_position += end - start
|
|
873
|
+
else:
|
|
874
|
+
# Don't handle scroll events here.
|
|
875
|
+
return NotImplemented
|
|
876
|
+
|
|
877
|
+
# Not focused, but focusing on click events.
|
|
878
|
+
else:
|
|
879
|
+
if (
|
|
880
|
+
self.focus_on_click()
|
|
881
|
+
and mouse_event.event_type == MouseEventType.MOUSE_UP
|
|
882
|
+
):
|
|
883
|
+
# Focus happens on mouseup. (If we did this on mousedown, the
|
|
884
|
+
# up event will be received at the point where this widget is
|
|
885
|
+
# focused and be handled anyway.)
|
|
886
|
+
get_app().layout.current_control = self
|
|
887
|
+
else:
|
|
888
|
+
return NotImplemented
|
|
889
|
+
|
|
890
|
+
return None
|
|
891
|
+
|
|
892
|
+
def move_cursor_down(self) -> None:
|
|
893
|
+
b = self.buffer
|
|
894
|
+
b.cursor_position += b.document.get_cursor_down_position()
|
|
895
|
+
|
|
896
|
+
def move_cursor_up(self) -> None:
|
|
897
|
+
b = self.buffer
|
|
898
|
+
b.cursor_position += b.document.get_cursor_up_position()
|
|
899
|
+
|
|
900
|
+
def get_key_bindings(self) -> KeyBindingsBase | None:
|
|
901
|
+
"""
|
|
902
|
+
When additional key bindings are given. Return these.
|
|
903
|
+
"""
|
|
904
|
+
return self.key_bindings
|
|
905
|
+
|
|
906
|
+
def get_invalidate_events(self) -> Iterable[Event[object]]:
|
|
907
|
+
"""
|
|
908
|
+
Return the Window invalidate events.
|
|
909
|
+
"""
|
|
910
|
+
# Whenever the buffer changes, the UI has to be updated.
|
|
911
|
+
yield self.buffer.on_text_changed
|
|
912
|
+
yield self.buffer.on_cursor_position_changed
|
|
913
|
+
|
|
914
|
+
yield self.buffer.on_completions_changed
|
|
915
|
+
yield self.buffer.on_suggestion_set
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
class SearchBufferControl(BufferControl):
|
|
919
|
+
"""
|
|
920
|
+
:class:`.BufferControl` which is used for searching another
|
|
921
|
+
:class:`.BufferControl`.
|
|
922
|
+
|
|
923
|
+
:param ignore_case: Search case insensitive.
|
|
924
|
+
"""
|
|
925
|
+
|
|
926
|
+
def __init__(
|
|
927
|
+
self,
|
|
928
|
+
buffer: Buffer | None = None,
|
|
929
|
+
input_processors: list[Processor] | None = None,
|
|
930
|
+
lexer: Lexer | None = None,
|
|
931
|
+
focus_on_click: FilterOrBool = False,
|
|
932
|
+
key_bindings: KeyBindingsBase | None = None,
|
|
933
|
+
ignore_case: FilterOrBool = False,
|
|
934
|
+
):
|
|
935
|
+
super().__init__(
|
|
936
|
+
buffer=buffer,
|
|
937
|
+
input_processors=input_processors,
|
|
938
|
+
lexer=lexer,
|
|
939
|
+
focus_on_click=focus_on_click,
|
|
940
|
+
key_bindings=key_bindings,
|
|
941
|
+
)
|
|
942
|
+
|
|
943
|
+
# If this BufferControl is used as a search field for one or more other
|
|
944
|
+
# BufferControls, then represents the search state.
|
|
945
|
+
self.searcher_search_state = SearchState(ignore_case=ignore_case)
|