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,108 @@
1
+ """
2
+ Collection of reusable components for building full screen applications.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from typing import Sequence
8
+
9
+ from prompt_toolkit.filters import has_completions, has_focus
10
+ from prompt_toolkit.formatted_text import AnyFormattedText
11
+ from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
12
+ from prompt_toolkit.key_binding.key_bindings import KeyBindings
13
+ from prompt_toolkit.layout.containers import (
14
+ AnyContainer,
15
+ DynamicContainer,
16
+ HSplit,
17
+ VSplit,
18
+ )
19
+ from prompt_toolkit.layout.dimension import AnyDimension
20
+ from prompt_toolkit.layout.dimension import Dimension as D
21
+
22
+ from .base import Box, Button, Frame, Shadow
23
+
24
+ __all__ = [
25
+ "Dialog",
26
+ ]
27
+
28
+
29
+ class Dialog:
30
+ """
31
+ Simple dialog window. This is the base for input dialogs, message dialogs
32
+ and confirmation dialogs.
33
+
34
+ Changing the title and body of the dialog is possible at runtime by
35
+ assigning to the `body` and `title` attributes of this class.
36
+
37
+ :param body: Child container object.
38
+ :param title: Text to be displayed in the heading of the dialog.
39
+ :param buttons: A list of `Button` widgets, displayed at the bottom.
40
+ """
41
+
42
+ def __init__(
43
+ self,
44
+ body: AnyContainer,
45
+ title: AnyFormattedText = "",
46
+ buttons: Sequence[Button] | None = None,
47
+ modal: bool = True,
48
+ width: AnyDimension = None,
49
+ with_background: bool = False,
50
+ ) -> None:
51
+ self.body = body
52
+ self.title = title
53
+
54
+ buttons = buttons or []
55
+
56
+ # When a button is selected, handle left/right key bindings.
57
+ buttons_kb = KeyBindings()
58
+ if len(buttons) > 1:
59
+ first_selected = has_focus(buttons[0])
60
+ last_selected = has_focus(buttons[-1])
61
+
62
+ buttons_kb.add("left", filter=~first_selected)(focus_previous)
63
+ buttons_kb.add("right", filter=~last_selected)(focus_next)
64
+
65
+ frame_body: AnyContainer
66
+ if buttons:
67
+ frame_body = HSplit(
68
+ [
69
+ # Add optional padding around the body.
70
+ Box(
71
+ body=DynamicContainer(lambda: self.body),
72
+ padding=D(preferred=1, max=1),
73
+ padding_bottom=0,
74
+ ),
75
+ # The buttons.
76
+ Box(
77
+ body=VSplit(buttons, padding=1, key_bindings=buttons_kb),
78
+ height=D(min=1, max=3, preferred=3),
79
+ ),
80
+ ]
81
+ )
82
+ else:
83
+ frame_body = body
84
+
85
+ # Key bindings for whole dialog.
86
+ kb = KeyBindings()
87
+ kb.add("tab", filter=~has_completions)(focus_next)
88
+ kb.add("s-tab", filter=~has_completions)(focus_previous)
89
+
90
+ frame = Shadow(
91
+ body=Frame(
92
+ title=lambda: self.title,
93
+ body=frame_body,
94
+ style="class:dialog.body",
95
+ width=(None if with_background is None else width),
96
+ key_bindings=kb,
97
+ modal=modal,
98
+ )
99
+ )
100
+
101
+ self.container: Box | Shadow
102
+ if with_background:
103
+ self.container = Box(body=frame, style="class:dialog", width=width)
104
+ else:
105
+ self.container = frame
106
+
107
+ def __pt_container__(self) -> AnyContainer:
108
+ return self.container
@@ -0,0 +1,374 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Callable, Iterable, Sequence
4
+
5
+ from prompt_toolkit.application.current import get_app
6
+ from prompt_toolkit.filters import Condition
7
+ from prompt_toolkit.formatted_text.base import OneStyleAndTextTuple, StyleAndTextTuples
8
+ from prompt_toolkit.key_binding.key_bindings import KeyBindings, KeyBindingsBase
9
+ from prompt_toolkit.key_binding.key_processor import KeyPressEvent
10
+ from prompt_toolkit.keys import Keys
11
+ from prompt_toolkit.layout.containers import (
12
+ AnyContainer,
13
+ ConditionalContainer,
14
+ Container,
15
+ Float,
16
+ FloatContainer,
17
+ HSplit,
18
+ Window,
19
+ )
20
+ from prompt_toolkit.layout.controls import FormattedTextControl
21
+ from prompt_toolkit.mouse_events import MouseEvent, MouseEventType
22
+ from prompt_toolkit.utils import get_cwidth
23
+ from prompt_toolkit.widgets import Shadow
24
+
25
+ from .base import Border
26
+
27
+ __all__ = [
28
+ "MenuContainer",
29
+ "MenuItem",
30
+ ]
31
+
32
+ E = KeyPressEvent
33
+
34
+
35
+ class MenuContainer:
36
+ """
37
+ :param floats: List of extra Float objects to display.
38
+ :param menu_items: List of `MenuItem` objects.
39
+ """
40
+
41
+ def __init__(
42
+ self,
43
+ body: AnyContainer,
44
+ menu_items: list[MenuItem],
45
+ floats: list[Float] | None = None,
46
+ key_bindings: KeyBindingsBase | None = None,
47
+ ) -> None:
48
+ self.body = body
49
+ self.menu_items = menu_items
50
+ self.selected_menu = [0]
51
+
52
+ # Key bindings.
53
+ kb = KeyBindings()
54
+
55
+ @Condition
56
+ def in_main_menu() -> bool:
57
+ return len(self.selected_menu) == 1
58
+
59
+ @Condition
60
+ def in_sub_menu() -> bool:
61
+ return len(self.selected_menu) > 1
62
+
63
+ # Navigation through the main menu.
64
+
65
+ @kb.add("left", filter=in_main_menu)
66
+ def _left(event: E) -> None:
67
+ self.selected_menu[0] = max(0, self.selected_menu[0] - 1)
68
+
69
+ @kb.add("right", filter=in_main_menu)
70
+ def _right(event: E) -> None:
71
+ self.selected_menu[0] = min(
72
+ len(self.menu_items) - 1, self.selected_menu[0] + 1
73
+ )
74
+
75
+ @kb.add("down", filter=in_main_menu)
76
+ def _down(event: E) -> None:
77
+ self.selected_menu.append(0)
78
+
79
+ @kb.add("c-c", filter=in_main_menu)
80
+ @kb.add("c-g", filter=in_main_menu)
81
+ def _cancel(event: E) -> None:
82
+ "Leave menu."
83
+ event.app.layout.focus_last()
84
+
85
+ # Sub menu navigation.
86
+
87
+ @kb.add("left", filter=in_sub_menu)
88
+ @kb.add("c-g", filter=in_sub_menu)
89
+ @kb.add("c-c", filter=in_sub_menu)
90
+ def _back(event: E) -> None:
91
+ "Go back to parent menu."
92
+ if len(self.selected_menu) > 1:
93
+ self.selected_menu.pop()
94
+
95
+ @kb.add("right", filter=in_sub_menu)
96
+ def _submenu(event: E) -> None:
97
+ "go into sub menu."
98
+ if self._get_menu(len(self.selected_menu) - 1).children:
99
+ self.selected_menu.append(0)
100
+
101
+ # If This item does not have a sub menu. Go up in the parent menu.
102
+ elif (
103
+ len(self.selected_menu) == 2
104
+ and self.selected_menu[0] < len(self.menu_items) - 1
105
+ ):
106
+ self.selected_menu = [
107
+ min(len(self.menu_items) - 1, self.selected_menu[0] + 1)
108
+ ]
109
+ if self.menu_items[self.selected_menu[0]].children:
110
+ self.selected_menu.append(0)
111
+
112
+ @kb.add("up", filter=in_sub_menu)
113
+ def _up_in_submenu(event: E) -> None:
114
+ "Select previous (enabled) menu item or return to main menu."
115
+ # Look for previous enabled items in this sub menu.
116
+ menu = self._get_menu(len(self.selected_menu) - 2)
117
+ index = self.selected_menu[-1]
118
+
119
+ previous_indexes = [
120
+ i
121
+ for i, item in enumerate(menu.children)
122
+ if i < index and not item.disabled
123
+ ]
124
+
125
+ if previous_indexes:
126
+ self.selected_menu[-1] = previous_indexes[-1]
127
+ elif len(self.selected_menu) == 2:
128
+ # Return to main menu.
129
+ self.selected_menu.pop()
130
+
131
+ @kb.add("down", filter=in_sub_menu)
132
+ def _down_in_submenu(event: E) -> None:
133
+ "Select next (enabled) menu item."
134
+ menu = self._get_menu(len(self.selected_menu) - 2)
135
+ index = self.selected_menu[-1]
136
+
137
+ next_indexes = [
138
+ i
139
+ for i, item in enumerate(menu.children)
140
+ if i > index and not item.disabled
141
+ ]
142
+
143
+ if next_indexes:
144
+ self.selected_menu[-1] = next_indexes[0]
145
+
146
+ @kb.add("enter")
147
+ def _click(event: E) -> None:
148
+ "Click the selected menu item."
149
+ item = self._get_menu(len(self.selected_menu) - 1)
150
+ if item.handler:
151
+ event.app.layout.focus_last()
152
+ item.handler()
153
+
154
+ # Controls.
155
+ self.control = FormattedTextControl(
156
+ self._get_menu_fragments, key_bindings=kb, focusable=True, show_cursor=False
157
+ )
158
+
159
+ self.window = Window(height=1, content=self.control, style="class:menu-bar")
160
+
161
+ submenu = self._submenu(0)
162
+ submenu2 = self._submenu(1)
163
+ submenu3 = self._submenu(2)
164
+
165
+ @Condition
166
+ def has_focus() -> bool:
167
+ return get_app().layout.current_window == self.window
168
+
169
+ self.container = FloatContainer(
170
+ content=HSplit(
171
+ [
172
+ # The titlebar.
173
+ self.window,
174
+ # The 'body', like defined above.
175
+ body,
176
+ ]
177
+ ),
178
+ floats=[
179
+ Float(
180
+ xcursor=True,
181
+ ycursor=True,
182
+ content=ConditionalContainer(
183
+ content=Shadow(body=submenu), filter=has_focus
184
+ ),
185
+ ),
186
+ Float(
187
+ attach_to_window=submenu,
188
+ xcursor=True,
189
+ ycursor=True,
190
+ allow_cover_cursor=True,
191
+ content=ConditionalContainer(
192
+ content=Shadow(body=submenu2),
193
+ filter=has_focus
194
+ & Condition(lambda: len(self.selected_menu) >= 1),
195
+ ),
196
+ ),
197
+ Float(
198
+ attach_to_window=submenu2,
199
+ xcursor=True,
200
+ ycursor=True,
201
+ allow_cover_cursor=True,
202
+ content=ConditionalContainer(
203
+ content=Shadow(body=submenu3),
204
+ filter=has_focus
205
+ & Condition(lambda: len(self.selected_menu) >= 2),
206
+ ),
207
+ ),
208
+ # --
209
+ ]
210
+ + (floats or []),
211
+ key_bindings=key_bindings,
212
+ )
213
+
214
+ def _get_menu(self, level: int) -> MenuItem:
215
+ menu = self.menu_items[self.selected_menu[0]]
216
+
217
+ for i, index in enumerate(self.selected_menu[1:]):
218
+ if i < level:
219
+ try:
220
+ menu = menu.children[index]
221
+ except IndexError:
222
+ return MenuItem("debug")
223
+
224
+ return menu
225
+
226
+ def _get_menu_fragments(self) -> StyleAndTextTuples:
227
+ focused = get_app().layout.has_focus(self.window)
228
+
229
+ # This is called during the rendering. When we discover that this
230
+ # widget doesn't have the focus anymore. Reset menu state.
231
+ if not focused:
232
+ self.selected_menu = [0]
233
+
234
+ # Generate text fragments for the main menu.
235
+ def one_item(i: int, item: MenuItem) -> Iterable[OneStyleAndTextTuple]:
236
+ def mouse_handler(mouse_event: MouseEvent) -> None:
237
+ hover = mouse_event.event_type == MouseEventType.MOUSE_MOVE
238
+ if (
239
+ mouse_event.event_type == MouseEventType.MOUSE_DOWN
240
+ or hover
241
+ and focused
242
+ ):
243
+ # Toggle focus.
244
+ app = get_app()
245
+ if not hover:
246
+ if app.layout.has_focus(self.window):
247
+ if self.selected_menu == [i]:
248
+ app.layout.focus_last()
249
+ else:
250
+ app.layout.focus(self.window)
251
+ self.selected_menu = [i]
252
+
253
+ yield ("class:menu-bar", " ", mouse_handler)
254
+ if i == self.selected_menu[0] and focused:
255
+ yield ("[SetMenuPosition]", "", mouse_handler)
256
+ style = "class:menu-bar.selected-item"
257
+ else:
258
+ style = "class:menu-bar"
259
+ yield style, item.text, mouse_handler
260
+
261
+ result: StyleAndTextTuples = []
262
+ for i, item in enumerate(self.menu_items):
263
+ result.extend(one_item(i, item))
264
+
265
+ return result
266
+
267
+ def _submenu(self, level: int = 0) -> Window:
268
+ def get_text_fragments() -> StyleAndTextTuples:
269
+ result: StyleAndTextTuples = []
270
+ if level < len(self.selected_menu):
271
+ menu = self._get_menu(level)
272
+ if menu.children:
273
+ result.append(("class:menu", Border.TOP_LEFT))
274
+ result.append(("class:menu", Border.HORIZONTAL * (menu.width + 4)))
275
+ result.append(("class:menu", Border.TOP_RIGHT))
276
+ result.append(("", "\n"))
277
+ try:
278
+ selected_item = self.selected_menu[level + 1]
279
+ except IndexError:
280
+ selected_item = -1
281
+
282
+ def one_item(
283
+ i: int, item: MenuItem
284
+ ) -> Iterable[OneStyleAndTextTuple]:
285
+ def mouse_handler(mouse_event: MouseEvent) -> None:
286
+ if item.disabled:
287
+ # The arrow keys can't interact with menu items that are disabled.
288
+ # The mouse shouldn't be able to either.
289
+ return
290
+ hover = mouse_event.event_type == MouseEventType.MOUSE_MOVE
291
+ if (
292
+ mouse_event.event_type == MouseEventType.MOUSE_UP
293
+ or hover
294
+ ):
295
+ app = get_app()
296
+ if not hover and item.handler:
297
+ app.layout.focus_last()
298
+ item.handler()
299
+ else:
300
+ self.selected_menu = self.selected_menu[
301
+ : level + 1
302
+ ] + [i]
303
+
304
+ if i == selected_item:
305
+ yield ("[SetCursorPosition]", "")
306
+ style = "class:menu-bar.selected-item"
307
+ else:
308
+ style = ""
309
+
310
+ yield ("class:menu", Border.VERTICAL)
311
+ if item.text == "-":
312
+ yield (
313
+ style + "class:menu-border",
314
+ f"{Border.HORIZONTAL * (menu.width + 3)}",
315
+ mouse_handler,
316
+ )
317
+ else:
318
+ yield (
319
+ style,
320
+ f" {item.text}".ljust(menu.width + 3),
321
+ mouse_handler,
322
+ )
323
+
324
+ if item.children:
325
+ yield (style, ">", mouse_handler)
326
+ else:
327
+ yield (style, " ", mouse_handler)
328
+
329
+ if i == selected_item:
330
+ yield ("[SetMenuPosition]", "")
331
+ yield ("class:menu", Border.VERTICAL)
332
+
333
+ yield ("", "\n")
334
+
335
+ for i, item in enumerate(menu.children):
336
+ result.extend(one_item(i, item))
337
+
338
+ result.append(("class:menu", Border.BOTTOM_LEFT))
339
+ result.append(("class:menu", Border.HORIZONTAL * (menu.width + 4)))
340
+ result.append(("class:menu", Border.BOTTOM_RIGHT))
341
+ return result
342
+
343
+ return Window(FormattedTextControl(get_text_fragments), style="class:menu")
344
+
345
+ @property
346
+ def floats(self) -> list[Float] | None:
347
+ return self.container.floats
348
+
349
+ def __pt_container__(self) -> Container:
350
+ return self.container
351
+
352
+
353
+ class MenuItem:
354
+ def __init__(
355
+ self,
356
+ text: str = "",
357
+ handler: Callable[[], None] | None = None,
358
+ children: list[MenuItem] | None = None,
359
+ shortcut: Sequence[Keys | str] | None = None,
360
+ disabled: bool = False,
361
+ ) -> None:
362
+ self.text = text
363
+ self.handler = handler
364
+ self.children = children or []
365
+ self.shortcut = shortcut
366
+ self.disabled = disabled
367
+ self.selected_item = 0
368
+
369
+ @property
370
+ def width(self) -> int:
371
+ if self.children:
372
+ return max(get_cwidth(c.text) for c in self.children)
373
+ else:
374
+ return 0