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.
Files changed (177) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +615 -2
  3. package/bin/ai.js +84 -0
  4. package/docs/README.en.md +615 -0
  5. package/docs/README.es.md +618 -0
  6. package/docs/README.fr.md +630 -0
  7. package/omni_ai/__init__.py +1 -0
  8. package/omni_ai/__main__.py +6 -0
  9. package/omni_ai/chat.py +1458 -0
  10. package/omni_ai/cli.py +991 -0
  11. package/omni_ai/config.py +531 -0
  12. package/omni_ai/deepseek.py +57 -0
  13. package/omni_ai/grok.py +111 -0
  14. package/omni_ai/onboarding.py +196 -0
  15. package/omni_ai/openrouter.py +335 -0
  16. package/omni_ai/osutils.py +86 -0
  17. package/omni_ai/pii.py +281 -0
  18. package/omni_ai/profiles.json +69 -0
  19. package/omni_ai/skills.py +124 -0
  20. package/omni_ai/sync.py +199 -0
  21. package/omni_ai/tools.py +75 -0
  22. package/omni_ai/ui.py +595 -0
  23. package/omni_ai/usage.py +305 -0
  24. package/omni_ai/vendor/prompt_toolkit/LICENSE +27 -0
  25. package/omni_ai/vendor/prompt_toolkit/__init__.py +52 -0
  26. package/omni_ai/vendor/prompt_toolkit/application/__init__.py +32 -0
  27. package/omni_ai/vendor/prompt_toolkit/application/application.py +1626 -0
  28. package/omni_ai/vendor/prompt_toolkit/application/current.py +195 -0
  29. package/omni_ai/vendor/prompt_toolkit/application/dummy.py +55 -0
  30. package/omni_ai/vendor/prompt_toolkit/application/run_in_terminal.py +114 -0
  31. package/omni_ai/vendor/prompt_toolkit/auto_suggest.py +177 -0
  32. package/omni_ai/vendor/prompt_toolkit/buffer.py +2029 -0
  33. package/omni_ai/vendor/prompt_toolkit/cache.py +127 -0
  34. package/omni_ai/vendor/prompt_toolkit/clipboard/__init__.py +17 -0
  35. package/omni_ai/vendor/prompt_toolkit/clipboard/base.py +109 -0
  36. package/omni_ai/vendor/prompt_toolkit/clipboard/in_memory.py +44 -0
  37. package/omni_ai/vendor/prompt_toolkit/clipboard/pyperclip.py +42 -0
  38. package/omni_ai/vendor/prompt_toolkit/completion/__init__.py +43 -0
  39. package/omni_ai/vendor/prompt_toolkit/completion/base.py +438 -0
  40. package/omni_ai/vendor/prompt_toolkit/completion/deduplicate.py +45 -0
  41. package/omni_ai/vendor/prompt_toolkit/completion/filesystem.py +118 -0
  42. package/omni_ai/vendor/prompt_toolkit/completion/fuzzy_completer.py +213 -0
  43. package/omni_ai/vendor/prompt_toolkit/completion/nested.py +109 -0
  44. package/omni_ai/vendor/prompt_toolkit/completion/word_completer.py +94 -0
  45. package/omni_ai/vendor/prompt_toolkit/contrib/__init__.py +0 -0
  46. package/omni_ai/vendor/prompt_toolkit/contrib/completers/__init__.py +5 -0
  47. package/omni_ai/vendor/prompt_toolkit/contrib/completers/system.py +64 -0
  48. package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/__init__.py +80 -0
  49. package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/compiler.py +579 -0
  50. package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/completion.py +100 -0
  51. package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/lexer.py +94 -0
  52. package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/regex_parser.py +279 -0
  53. package/omni_ai/vendor/prompt_toolkit/contrib/regular_languages/validation.py +60 -0
  54. package/omni_ai/vendor/prompt_toolkit/contrib/ssh/__init__.py +8 -0
  55. package/omni_ai/vendor/prompt_toolkit/contrib/ssh/server.py +178 -0
  56. package/omni_ai/vendor/prompt_toolkit/contrib/telnet/__init__.py +7 -0
  57. package/omni_ai/vendor/prompt_toolkit/contrib/telnet/log.py +13 -0
  58. package/omni_ai/vendor/prompt_toolkit/contrib/telnet/protocol.py +209 -0
  59. package/omni_ai/vendor/prompt_toolkit/contrib/telnet/server.py +428 -0
  60. package/omni_ai/vendor/prompt_toolkit/cursor_shapes.py +117 -0
  61. package/omni_ai/vendor/prompt_toolkit/data_structures.py +18 -0
  62. package/omni_ai/vendor/prompt_toolkit/document.py +1182 -0
  63. package/omni_ai/vendor/prompt_toolkit/enums.py +19 -0
  64. package/omni_ai/vendor/prompt_toolkit/eventloop/__init__.py +31 -0
  65. package/omni_ai/vendor/prompt_toolkit/eventloop/async_generator.py +125 -0
  66. package/omni_ai/vendor/prompt_toolkit/eventloop/inputhook.py +191 -0
  67. package/omni_ai/vendor/prompt_toolkit/eventloop/utils.py +101 -0
  68. package/omni_ai/vendor/prompt_toolkit/eventloop/win32.py +72 -0
  69. package/omni_ai/vendor/prompt_toolkit/filters/__init__.py +71 -0
  70. package/omni_ai/vendor/prompt_toolkit/filters/app.py +419 -0
  71. package/omni_ai/vendor/prompt_toolkit/filters/base.py +261 -0
  72. package/omni_ai/vendor/prompt_toolkit/filters/cli.py +65 -0
  73. package/omni_ai/vendor/prompt_toolkit/filters/utils.py +41 -0
  74. package/omni_ai/vendor/prompt_toolkit/formatted_text/__init__.py +59 -0
  75. package/omni_ai/vendor/prompt_toolkit/formatted_text/ansi.py +297 -0
  76. package/omni_ai/vendor/prompt_toolkit/formatted_text/base.py +179 -0
  77. package/omni_ai/vendor/prompt_toolkit/formatted_text/html.py +145 -0
  78. package/omni_ai/vendor/prompt_toolkit/formatted_text/pygments.py +32 -0
  79. package/omni_ai/vendor/prompt_toolkit/formatted_text/utils.py +103 -0
  80. package/omni_ai/vendor/prompt_toolkit/history.py +306 -0
  81. package/omni_ai/vendor/prompt_toolkit/input/__init__.py +14 -0
  82. package/omni_ai/vendor/prompt_toolkit/input/ansi_escape_sequences.py +344 -0
  83. package/omni_ai/vendor/prompt_toolkit/input/base.py +153 -0
  84. package/omni_ai/vendor/prompt_toolkit/input/defaults.py +79 -0
  85. package/omni_ai/vendor/prompt_toolkit/input/posix_pipe.py +118 -0
  86. package/omni_ai/vendor/prompt_toolkit/input/posix_utils.py +97 -0
  87. package/omni_ai/vendor/prompt_toolkit/input/typeahead.py +78 -0
  88. package/omni_ai/vendor/prompt_toolkit/input/vt100.py +309 -0
  89. package/omni_ai/vendor/prompt_toolkit/input/vt100_parser.py +250 -0
  90. package/omni_ai/vendor/prompt_toolkit/input/win32.py +749 -0
  91. package/omni_ai/vendor/prompt_toolkit/input/win32_pipe.py +156 -0
  92. package/omni_ai/vendor/prompt_toolkit/key_binding/__init__.py +22 -0
  93. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/__init__.py +0 -0
  94. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/auto_suggest.py +66 -0
  95. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/basic.py +257 -0
  96. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/completion.py +206 -0
  97. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/cpr.py +30 -0
  98. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/emacs.py +563 -0
  99. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/focus.py +26 -0
  100. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/mouse.py +348 -0
  101. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/named_commands.py +691 -0
  102. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/open_in_editor.py +52 -0
  103. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/page_navigation.py +85 -0
  104. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/scroll.py +190 -0
  105. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/search.py +96 -0
  106. package/omni_ai/vendor/prompt_toolkit/key_binding/bindings/vi.py +2233 -0
  107. package/omni_ai/vendor/prompt_toolkit/key_binding/defaults.py +63 -0
  108. package/omni_ai/vendor/prompt_toolkit/key_binding/digraphs.py +1378 -0
  109. package/omni_ai/vendor/prompt_toolkit/key_binding/emacs_state.py +36 -0
  110. package/omni_ai/vendor/prompt_toolkit/key_binding/key_bindings.py +670 -0
  111. package/omni_ai/vendor/prompt_toolkit/key_binding/key_processor.py +526 -0
  112. package/omni_ai/vendor/prompt_toolkit/key_binding/vi_state.py +107 -0
  113. package/omni_ai/vendor/prompt_toolkit/keys.py +222 -0
  114. package/omni_ai/vendor/prompt_toolkit/layout/__init__.py +147 -0
  115. package/omni_ai/vendor/prompt_toolkit/layout/containers.py +2738 -0
  116. package/omni_ai/vendor/prompt_toolkit/layout/controls.py +945 -0
  117. package/omni_ai/vendor/prompt_toolkit/layout/dimension.py +220 -0
  118. package/omni_ai/vendor/prompt_toolkit/layout/dummy.py +40 -0
  119. package/omni_ai/vendor/prompt_toolkit/layout/layout.py +412 -0
  120. package/omni_ai/vendor/prompt_toolkit/layout/margins.py +304 -0
  121. package/omni_ai/vendor/prompt_toolkit/layout/menus.py +748 -0
  122. package/omni_ai/vendor/prompt_toolkit/layout/mouse_handlers.py +56 -0
  123. package/omni_ai/vendor/prompt_toolkit/layout/processors.py +1010 -0
  124. package/omni_ai/vendor/prompt_toolkit/layout/screen.py +323 -0
  125. package/omni_ai/vendor/prompt_toolkit/layout/scrollable_pane.py +494 -0
  126. package/omni_ai/vendor/prompt_toolkit/layout/utils.py +80 -0
  127. package/omni_ai/vendor/prompt_toolkit/lexers/__init__.py +21 -0
  128. package/omni_ai/vendor/prompt_toolkit/lexers/base.py +85 -0
  129. package/omni_ai/vendor/prompt_toolkit/lexers/pygments.py +328 -0
  130. package/omni_ai/vendor/prompt_toolkit/log.py +13 -0
  131. package/omni_ai/vendor/prompt_toolkit/mouse_events.py +85 -0
  132. package/omni_ai/vendor/prompt_toolkit/output/__init__.py +15 -0
  133. package/omni_ai/vendor/prompt_toolkit/output/base.py +332 -0
  134. package/omni_ai/vendor/prompt_toolkit/output/color_depth.py +64 -0
  135. package/omni_ai/vendor/prompt_toolkit/output/conemu.py +65 -0
  136. package/omni_ai/vendor/prompt_toolkit/output/defaults.py +106 -0
  137. package/omni_ai/vendor/prompt_toolkit/output/flush_stdout.py +87 -0
  138. package/omni_ai/vendor/prompt_toolkit/output/plain_text.py +143 -0
  139. package/omni_ai/vendor/prompt_toolkit/output/vt100.py +748 -0
  140. package/omni_ai/vendor/prompt_toolkit/output/win32.py +683 -0
  141. package/omni_ai/vendor/prompt_toolkit/output/windows10.py +128 -0
  142. package/omni_ai/vendor/prompt_toolkit/patch_stdout.py +297 -0
  143. package/omni_ai/vendor/prompt_toolkit/py.typed +0 -0
  144. package/omni_ai/vendor/prompt_toolkit/renderer.py +814 -0
  145. package/omni_ai/vendor/prompt_toolkit/search.py +226 -0
  146. package/omni_ai/vendor/prompt_toolkit/selection.py +58 -0
  147. package/omni_ai/vendor/prompt_toolkit/shortcuts/__init__.py +46 -0
  148. package/omni_ai/vendor/prompt_toolkit/shortcuts/dialogs.py +330 -0
  149. package/omni_ai/vendor/prompt_toolkit/shortcuts/progress_bar/__init__.py +33 -0
  150. package/omni_ai/vendor/prompt_toolkit/shortcuts/progress_bar/base.py +449 -0
  151. package/omni_ai/vendor/prompt_toolkit/shortcuts/progress_bar/formatters.py +431 -0
  152. package/omni_ai/vendor/prompt_toolkit/shortcuts/prompt.py +1513 -0
  153. package/omni_ai/vendor/prompt_toolkit/shortcuts/utils.py +239 -0
  154. package/omni_ai/vendor/prompt_toolkit/styles/__init__.py +67 -0
  155. package/omni_ai/vendor/prompt_toolkit/styles/base.py +184 -0
  156. package/omni_ai/vendor/prompt_toolkit/styles/defaults.py +236 -0
  157. package/omni_ai/vendor/prompt_toolkit/styles/named_colors.py +162 -0
  158. package/omni_ai/vendor/prompt_toolkit/styles/pygments.py +70 -0
  159. package/omni_ai/vendor/prompt_toolkit/styles/style.py +401 -0
  160. package/omni_ai/vendor/prompt_toolkit/styles/style_transformation.py +374 -0
  161. package/omni_ai/vendor/prompt_toolkit/token.py +9 -0
  162. package/omni_ai/vendor/prompt_toolkit/utils.py +327 -0
  163. package/omni_ai/vendor/prompt_toolkit/validation.py +192 -0
  164. package/omni_ai/vendor/prompt_toolkit/widgets/__init__.py +63 -0
  165. package/omni_ai/vendor/prompt_toolkit/widgets/base.py +995 -0
  166. package/omni_ai/vendor/prompt_toolkit/widgets/dialogs.py +108 -0
  167. package/omni_ai/vendor/prompt_toolkit/widgets/menus.py +374 -0
  168. package/omni_ai/vendor/prompt_toolkit/widgets/toolbars.py +370 -0
  169. package/omni_ai/vendor/prompt_toolkit/win32_types.py +229 -0
  170. package/omni_ai/vendor/wcwidth/LICENSE +27 -0
  171. package/omni_ai/vendor/wcwidth/__init__.py +29 -0
  172. package/omni_ai/vendor/wcwidth/table_vs16.py +125 -0
  173. package/omni_ai/vendor/wcwidth/table_wide.py +1493 -0
  174. package/omni_ai/vendor/wcwidth/table_zero.py +4843 -0
  175. package/omni_ai/vendor/wcwidth/unicode_versions.py +38 -0
  176. package/omni_ai/vendor/wcwidth/wcwidth.py +345 -0
  177. package/package.json +40 -4
@@ -0,0 +1,156 @@
1
+ from __future__ import annotations
2
+
3
+ import sys
4
+
5
+ assert sys.platform == "win32"
6
+
7
+ from contextlib import contextmanager
8
+ from ctypes import windll
9
+ from ctypes.wintypes import HANDLE
10
+ from typing import Callable, ContextManager, Iterator
11
+
12
+ from prompt_toolkit.eventloop.win32 import create_win32_event
13
+
14
+ from ..key_binding import KeyPress
15
+ from ..utils import DummyContext
16
+ from .base import PipeInput
17
+ from .vt100_parser import Vt100Parser
18
+ from .win32 import _Win32InputBase, attach_win32_input, detach_win32_input
19
+
20
+ __all__ = ["Win32PipeInput"]
21
+
22
+
23
+ class Win32PipeInput(_Win32InputBase, PipeInput):
24
+ """
25
+ This is an input pipe that works on Windows.
26
+ Text or bytes can be feed into the pipe, and key strokes can be read from
27
+ the pipe. This is useful if we want to send the input programmatically into
28
+ the application. Mostly useful for unit testing.
29
+
30
+ Notice that even though it's Windows, we use vt100 escape sequences over
31
+ the pipe.
32
+
33
+ Usage::
34
+
35
+ input = Win32PipeInput()
36
+ input.send_text('inputdata')
37
+ """
38
+
39
+ _id = 0
40
+
41
+ def __init__(self, _event: HANDLE) -> None:
42
+ super().__init__()
43
+ # Event (handle) for registering this input in the event loop.
44
+ # This event is set when there is data available to read from the pipe.
45
+ # Note: We use this approach instead of using a regular pipe, like
46
+ # returned from `os.pipe()`, because making such a regular pipe
47
+ # non-blocking is tricky and this works really well.
48
+ self._event = create_win32_event()
49
+
50
+ self._closed = False
51
+
52
+ # Parser for incoming keys.
53
+ self._buffer: list[KeyPress] = [] # Buffer to collect the Key objects.
54
+ self.vt100_parser = Vt100Parser(lambda key: self._buffer.append(key))
55
+
56
+ # Identifier for every PipeInput for the hash.
57
+ self.__class__._id += 1
58
+ self._id = self.__class__._id
59
+
60
+ @classmethod
61
+ @contextmanager
62
+ def create(cls) -> Iterator[Win32PipeInput]:
63
+ event = create_win32_event()
64
+ try:
65
+ yield Win32PipeInput(_event=event)
66
+ finally:
67
+ windll.kernel32.CloseHandle(event)
68
+
69
+ @property
70
+ def closed(self) -> bool:
71
+ return self._closed
72
+
73
+ def fileno(self) -> int:
74
+ """
75
+ The windows pipe doesn't depend on the file handle.
76
+ """
77
+ raise NotImplementedError
78
+
79
+ @property
80
+ def handle(self) -> HANDLE:
81
+ "The handle used for registering this pipe in the event loop."
82
+ return self._event
83
+
84
+ def attach(self, input_ready_callback: Callable[[], None]) -> ContextManager[None]:
85
+ """
86
+ Return a context manager that makes this input active in the current
87
+ event loop.
88
+ """
89
+ return attach_win32_input(self, input_ready_callback)
90
+
91
+ def detach(self) -> ContextManager[None]:
92
+ """
93
+ Return a context manager that makes sure that this input is not active
94
+ in the current event loop.
95
+ """
96
+ return detach_win32_input(self)
97
+
98
+ def read_keys(self) -> list[KeyPress]:
99
+ "Read list of KeyPress."
100
+
101
+ # Return result.
102
+ result = self._buffer
103
+ self._buffer = []
104
+
105
+ # Reset event.
106
+ if not self._closed:
107
+ # (If closed, the event should not reset.)
108
+ windll.kernel32.ResetEvent(self._event)
109
+
110
+ return result
111
+
112
+ def flush_keys(self) -> list[KeyPress]:
113
+ """
114
+ Flush pending keys and return them.
115
+ (Used for flushing the 'escape' key.)
116
+ """
117
+ # Flush all pending keys. (This is most important to flush the vt100
118
+ # 'Escape' key early when nothing else follows.)
119
+ self.vt100_parser.flush()
120
+
121
+ # Return result.
122
+ result = self._buffer
123
+ self._buffer = []
124
+ return result
125
+
126
+ def send_bytes(self, data: bytes) -> None:
127
+ "Send bytes to the input."
128
+ self.send_text(data.decode("utf-8", "ignore"))
129
+
130
+ def send_text(self, text: str) -> None:
131
+ "Send text to the input."
132
+ if self._closed:
133
+ raise ValueError("Attempt to write into a closed pipe.")
134
+
135
+ # Pass it through our vt100 parser.
136
+ self.vt100_parser.feed(text)
137
+
138
+ # Set event.
139
+ windll.kernel32.SetEvent(self._event)
140
+
141
+ def raw_mode(self) -> ContextManager[None]:
142
+ return DummyContext()
143
+
144
+ def cooked_mode(self) -> ContextManager[None]:
145
+ return DummyContext()
146
+
147
+ def close(self) -> None:
148
+ "Close write-end of the pipe."
149
+ self._closed = True
150
+ windll.kernel32.SetEvent(self._event)
151
+
152
+ def typeahead_hash(self) -> str:
153
+ """
154
+ This needs to be unique for every `PipeInput`.
155
+ """
156
+ return f"pipe-input-{self._id}"
@@ -0,0 +1,22 @@
1
+ from __future__ import annotations
2
+
3
+ from .key_bindings import (
4
+ ConditionalKeyBindings,
5
+ DynamicKeyBindings,
6
+ KeyBindings,
7
+ KeyBindingsBase,
8
+ merge_key_bindings,
9
+ )
10
+ from .key_processor import KeyPress, KeyPressEvent
11
+
12
+ __all__ = [
13
+ # key_bindings.
14
+ "ConditionalKeyBindings",
15
+ "DynamicKeyBindings",
16
+ "KeyBindings",
17
+ "KeyBindingsBase",
18
+ "merge_key_bindings",
19
+ # key_processor
20
+ "KeyPress",
21
+ "KeyPressEvent",
22
+ ]
@@ -0,0 +1,66 @@
1
+ """
2
+ Key bindings for auto suggestion (for fish-style auto suggestion).
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import re
8
+
9
+ from prompt_toolkit.application.current import get_app
10
+ from prompt_toolkit.filters import Condition, emacs_mode
11
+ from prompt_toolkit.key_binding.key_bindings import KeyBindings
12
+ from prompt_toolkit.key_binding.key_processor import KeyPressEvent
13
+
14
+ __all__ = [
15
+ "load_auto_suggest_bindings",
16
+ ]
17
+
18
+ E = KeyPressEvent
19
+
20
+
21
+ def load_auto_suggest_bindings() -> KeyBindings:
22
+ """
23
+ Key bindings for accepting auto suggestion text.
24
+
25
+ (This has to come after the Vi bindings, because they also have an
26
+ implementation for the "right arrow", but we really want the suggestion
27
+ binding when a suggestion is available.)
28
+ """
29
+ key_bindings = KeyBindings()
30
+ handle = key_bindings.add
31
+
32
+ @Condition
33
+ def suggestion_available() -> bool:
34
+ app = get_app()
35
+ return (
36
+ app.current_buffer.suggestion is not None
37
+ and len(app.current_buffer.suggestion.text) > 0
38
+ and app.current_buffer.document.is_cursor_at_the_end
39
+ )
40
+
41
+ @handle("c-f", filter=suggestion_available)
42
+ @handle("c-e", filter=suggestion_available)
43
+ @handle("right", filter=suggestion_available)
44
+ def _accept(event: E) -> None:
45
+ """
46
+ Accept suggestion.
47
+ """
48
+ b = event.current_buffer
49
+ suggestion = b.suggestion
50
+
51
+ if suggestion:
52
+ b.insert_text(suggestion.text)
53
+
54
+ @handle("escape", "f", filter=suggestion_available & emacs_mode)
55
+ def _fill(event: E) -> None:
56
+ """
57
+ Fill partial suggestion.
58
+ """
59
+ b = event.current_buffer
60
+ suggestion = b.suggestion
61
+
62
+ if suggestion:
63
+ t = re.split(r"([^\s/]+(?:\s+|/))", suggestion.text)
64
+ b.insert_text(next(x for x in t if x))
65
+
66
+ return key_bindings
@@ -0,0 +1,257 @@
1
+ # pylint: disable=function-redefined
2
+ from __future__ import annotations
3
+
4
+ from prompt_toolkit.application.current import get_app
5
+ from prompt_toolkit.filters import (
6
+ Condition,
7
+ emacs_insert_mode,
8
+ has_selection,
9
+ in_paste_mode,
10
+ is_multiline,
11
+ vi_insert_mode,
12
+ )
13
+ from prompt_toolkit.key_binding.key_processor import KeyPress, KeyPressEvent
14
+ from prompt_toolkit.keys import Keys
15
+
16
+ from ..key_bindings import KeyBindings
17
+ from .named_commands import get_by_name
18
+
19
+ __all__ = [
20
+ "load_basic_bindings",
21
+ ]
22
+
23
+ E = KeyPressEvent
24
+
25
+
26
+ def if_no_repeat(event: E) -> bool:
27
+ """Callable that returns True when the previous event was delivered to
28
+ another handler."""
29
+ return not event.is_repeat
30
+
31
+
32
+ @Condition
33
+ def has_text_before_cursor() -> bool:
34
+ return bool(get_app().current_buffer.text)
35
+
36
+
37
+ @Condition
38
+ def in_quoted_insert() -> bool:
39
+ return get_app().quoted_insert
40
+
41
+
42
+ def load_basic_bindings() -> KeyBindings:
43
+ key_bindings = KeyBindings()
44
+ insert_mode = vi_insert_mode | emacs_insert_mode
45
+ handle = key_bindings.add
46
+
47
+ @handle("c-a")
48
+ @handle("c-b")
49
+ @handle("c-c")
50
+ @handle("c-d")
51
+ @handle("c-e")
52
+ @handle("c-f")
53
+ @handle("c-g")
54
+ @handle("c-h")
55
+ @handle("c-i")
56
+ @handle("c-j")
57
+ @handle("c-k")
58
+ @handle("c-l")
59
+ @handle("c-m")
60
+ @handle("c-n")
61
+ @handle("c-o")
62
+ @handle("c-p")
63
+ @handle("c-q")
64
+ @handle("c-r")
65
+ @handle("c-s")
66
+ @handle("c-t")
67
+ @handle("c-u")
68
+ @handle("c-v")
69
+ @handle("c-w")
70
+ @handle("c-x")
71
+ @handle("c-y")
72
+ @handle("c-z")
73
+ @handle("f1")
74
+ @handle("f2")
75
+ @handle("f3")
76
+ @handle("f4")
77
+ @handle("f5")
78
+ @handle("f6")
79
+ @handle("f7")
80
+ @handle("f8")
81
+ @handle("f9")
82
+ @handle("f10")
83
+ @handle("f11")
84
+ @handle("f12")
85
+ @handle("f13")
86
+ @handle("f14")
87
+ @handle("f15")
88
+ @handle("f16")
89
+ @handle("f17")
90
+ @handle("f18")
91
+ @handle("f19")
92
+ @handle("f20")
93
+ @handle("f21")
94
+ @handle("f22")
95
+ @handle("f23")
96
+ @handle("f24")
97
+ @handle("c-@") # Also c-space.
98
+ @handle("c-\\")
99
+ @handle("c-]")
100
+ @handle("c-^")
101
+ @handle("c-_")
102
+ @handle("backspace")
103
+ @handle("up")
104
+ @handle("down")
105
+ @handle("right")
106
+ @handle("left")
107
+ @handle("s-up")
108
+ @handle("s-down")
109
+ @handle("s-right")
110
+ @handle("s-left")
111
+ @handle("home")
112
+ @handle("end")
113
+ @handle("s-home")
114
+ @handle("s-end")
115
+ @handle("delete")
116
+ @handle("s-delete")
117
+ @handle("c-delete")
118
+ @handle("pageup")
119
+ @handle("pagedown")
120
+ @handle("s-tab")
121
+ @handle("tab")
122
+ @handle("c-s-left")
123
+ @handle("c-s-right")
124
+ @handle("c-s-home")
125
+ @handle("c-s-end")
126
+ @handle("c-left")
127
+ @handle("c-right")
128
+ @handle("c-up")
129
+ @handle("c-down")
130
+ @handle("c-home")
131
+ @handle("c-end")
132
+ @handle("insert")
133
+ @handle("s-insert")
134
+ @handle("c-insert")
135
+ @handle("<sigint>")
136
+ @handle(Keys.Ignore)
137
+ def _ignore(event: E) -> None:
138
+ """
139
+ First, for any of these keys, Don't do anything by default. Also don't
140
+ catch them in the 'Any' handler which will insert them as data.
141
+
142
+ If people want to insert these characters as a literal, they can always
143
+ do by doing a quoted insert. (ControlQ in emacs mode, ControlV in Vi
144
+ mode.)
145
+ """
146
+ pass
147
+
148
+ # Readline-style bindings.
149
+ handle("home")(get_by_name("beginning-of-line"))
150
+ handle("end")(get_by_name("end-of-line"))
151
+ handle("left")(get_by_name("backward-char"))
152
+ handle("right")(get_by_name("forward-char"))
153
+ handle("c-up")(get_by_name("previous-history"))
154
+ handle("c-down")(get_by_name("next-history"))
155
+ handle("c-l")(get_by_name("clear-screen"))
156
+
157
+ handle("c-k", filter=insert_mode)(get_by_name("kill-line"))
158
+ handle("c-u", filter=insert_mode)(get_by_name("unix-line-discard"))
159
+ handle("backspace", filter=insert_mode, save_before=if_no_repeat)(
160
+ get_by_name("backward-delete-char")
161
+ )
162
+ handle("delete", filter=insert_mode, save_before=if_no_repeat)(
163
+ get_by_name("delete-char")
164
+ )
165
+ handle("c-delete", filter=insert_mode, save_before=if_no_repeat)(
166
+ get_by_name("delete-char")
167
+ )
168
+ handle(Keys.Any, filter=insert_mode, save_before=if_no_repeat)(
169
+ get_by_name("self-insert")
170
+ )
171
+ handle("c-t", filter=insert_mode)(get_by_name("transpose-chars"))
172
+ handle("c-i", filter=insert_mode)(get_by_name("menu-complete"))
173
+ handle("s-tab", filter=insert_mode)(get_by_name("menu-complete-backward"))
174
+
175
+ # Control-W should delete, using whitespace as separator, while M-Del
176
+ # should delete using [^a-zA-Z0-9] as a boundary.
177
+ handle("c-w", filter=insert_mode)(get_by_name("unix-word-rubout"))
178
+
179
+ handle("pageup", filter=~has_selection)(get_by_name("previous-history"))
180
+ handle("pagedown", filter=~has_selection)(get_by_name("next-history"))
181
+
182
+ # CTRL keys.
183
+
184
+ handle("c-d", filter=has_text_before_cursor & insert_mode)(
185
+ get_by_name("delete-char")
186
+ )
187
+
188
+ @handle("enter", filter=insert_mode & is_multiline)
189
+ def _newline(event: E) -> None:
190
+ """
191
+ Newline (in case of multiline input.
192
+ """
193
+ event.current_buffer.newline(copy_margin=not in_paste_mode())
194
+
195
+ @handle("c-j")
196
+ def _newline2(event: E) -> None:
197
+ r"""
198
+ By default, handle \n as if it were a \r (enter).
199
+ (It appears that some terminals send \n instead of \r when pressing
200
+ enter. - at least the Linux subsystem for Windows.)
201
+ """
202
+ event.key_processor.feed(KeyPress(Keys.ControlM, "\r"), first=True)
203
+
204
+ # Delete the word before the cursor.
205
+
206
+ @handle("up")
207
+ def _go_up(event: E) -> None:
208
+ event.current_buffer.auto_up(count=event.arg)
209
+
210
+ @handle("down")
211
+ def _go_down(event: E) -> None:
212
+ event.current_buffer.auto_down(count=event.arg)
213
+
214
+ @handle("delete", filter=has_selection)
215
+ def _cut(event: E) -> None:
216
+ data = event.current_buffer.cut_selection()
217
+ event.app.clipboard.set_data(data)
218
+
219
+ # Global bindings.
220
+
221
+ @handle("c-z")
222
+ def _insert_ctrl_z(event: E) -> None:
223
+ """
224
+ By default, control-Z should literally insert Ctrl-Z.
225
+ (Ansi Ctrl-Z, code 26 in MSDOS means End-Of-File.
226
+ In a Python REPL for instance, it's possible to type
227
+ Control-Z followed by enter to quit.)
228
+
229
+ When the system bindings are loaded and suspend-to-background is
230
+ supported, that will override this binding.
231
+ """
232
+ event.current_buffer.insert_text(event.data)
233
+
234
+ @handle(Keys.BracketedPaste)
235
+ def _paste(event: E) -> None:
236
+ """
237
+ Pasting from clipboard.
238
+ """
239
+ data = event.data
240
+
241
+ # Be sure to use \n as line ending.
242
+ # Some terminals (Like iTerm2) seem to paste \r\n line endings in a
243
+ # bracketed paste. See: https://github.com/ipython/ipython/issues/9737
244
+ data = data.replace("\r\n", "\n")
245
+ data = data.replace("\r", "\n")
246
+
247
+ event.current_buffer.insert_text(data)
248
+
249
+ @handle(Keys.Any, filter=in_quoted_insert, eager=True)
250
+ def _insert_text(event: E) -> None:
251
+ """
252
+ Handle quoted insert.
253
+ """
254
+ event.current_buffer.insert_text(event.data, overwrite=False)
255
+ event.app.quoted_insert = False
256
+
257
+ return key_bindings