networka 0.1.9__py3-none-any.whl
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.
- network_toolkit/__about__.py +4 -0
- network_toolkit/__init__.py +8 -0
- network_toolkit/__main__.py +10 -0
- network_toolkit/builtin_sequences/arista_eos/common.yml +53 -0
- network_toolkit/builtin_sequences/mikrotik_routeros/common.yml +52 -0
- network_toolkit/cli.py +265 -0
- network_toolkit/commands/__init__.py +8 -0
- network_toolkit/commands/backup.py +441 -0
- network_toolkit/commands/complete.py +172 -0
- network_toolkit/commands/config.py +1068 -0
- network_toolkit/commands/diff.py +485 -0
- network_toolkit/commands/download.py +197 -0
- network_toolkit/commands/firmware.py +780 -0
- network_toolkit/commands/info.py +377 -0
- network_toolkit/commands/list.py +345 -0
- network_toolkit/commands/routerboard_upgrade.py +204 -0
- network_toolkit/commands/run.py +839 -0
- network_toolkit/commands/schema_unified.py +91 -0
- network_toolkit/commands/sequence.py +17 -0
- network_toolkit/commands/ssh.py +540 -0
- network_toolkit/commands/ssh_engines.py +0 -0
- network_toolkit/commands/ssh_fallback.py +97 -0
- network_toolkit/commands/ssh_platform.py +120 -0
- network_toolkit/commands/upload.py +237 -0
- network_toolkit/commands/vendor_config_backup.py +378 -0
- network_toolkit/common/color_magic.py +177 -0
- network_toolkit/common/command.py +135 -0
- network_toolkit/common/command_base.py +135 -0
- network_toolkit/common/command_helpers.py +214 -0
- network_toolkit/common/credentials.py +86 -0
- network_toolkit/common/defaults.py +17 -0
- network_toolkit/common/errors.py +130 -0
- network_toolkit/common/interactive_confirmation.py +144 -0
- network_toolkit/common/logging.py +47 -0
- network_toolkit/common/output.py +594 -0
- network_toolkit/common/output_clean.py +244 -0
- network_toolkit/common/paths.py +86 -0
- network_toolkit/common/resolver.py +207 -0
- network_toolkit/common/styles.py +199 -0
- network_toolkit/common/table_generator.py +169 -0
- network_toolkit/common/table_providers.py +716 -0
- network_toolkit/config.py +1506 -0
- network_toolkit/credentials.py +288 -0
- network_toolkit/device.py +1658 -0
- network_toolkit/device_transfers.py +170 -0
- network_toolkit/exceptions.py +78 -0
- network_toolkit/ip_device.py +241 -0
- network_toolkit/platforms/__init__.py +21 -0
- network_toolkit/platforms/base.py +267 -0
- network_toolkit/platforms/cisco_ios/__init__.py +6 -0
- network_toolkit/platforms/cisco_ios/constants.py +15 -0
- network_toolkit/platforms/cisco_ios/operations.py +426 -0
- network_toolkit/platforms/cisco_iosxe/__init__.py +6 -0
- network_toolkit/platforms/cisco_iosxe/constants.py +15 -0
- network_toolkit/platforms/cisco_iosxe/operations.py +580 -0
- network_toolkit/platforms/cisco_shared/__init__.py +0 -0
- network_toolkit/platforms/cisco_shared/operations.py +0 -0
- network_toolkit/platforms/factory.py +190 -0
- network_toolkit/platforms/mikrotik_routeros/__init__.py +8 -0
- network_toolkit/platforms/mikrotik_routeros/confirmation_patterns.py +24 -0
- network_toolkit/platforms/mikrotik_routeros/constants.py +31 -0
- network_toolkit/platforms/mikrotik_routeros/operations.py +686 -0
- network_toolkit/results_enhanced.py +364 -0
- network_toolkit/schemas/__init__.py +8 -0
- network_toolkit/sequence_manager.py +239 -0
- network_toolkit/shell_completion/bash_completion_nw.sh +339 -0
- network_toolkit/shell_completion/zsh_completion_nw.zsh +136 -0
- network_toolkit/transport/__init__.py +15 -0
- network_toolkit/transport/factory.py +113 -0
- network_toolkit/transport/interfaces.py +46 -0
- network_toolkit/transport/nornir_inventory.py +173 -0
- network_toolkit/transport/nornir_netmiko.py +277 -0
- network_toolkit/transport/scrapli_sync.py +36 -0
- network_toolkit/tui/__init__.py +18 -0
- network_toolkit/tui/app.py +1275 -0
- network_toolkit/tui/clipboard.py +46 -0
- network_toolkit/tui/compat.py +111 -0
- network_toolkit/tui/constants.py +111 -0
- network_toolkit/tui/controller.py +550 -0
- network_toolkit/tui/data.py +149 -0
- network_toolkit/tui/dialogs.py +196 -0
- network_toolkit/tui/helpers.py +55 -0
- network_toolkit/tui/keymap.py +31 -0
- network_toolkit/tui/layout.py +111 -0
- network_toolkit/tui/models.py +93 -0
- network_toolkit/tui/output_manager.py +372 -0
- network_toolkit/tui/prompt.py +172 -0
- network_toolkit/tui/services.py +285 -0
- networka-0.1.9.dist-info/METADATA +316 -0
- networka-0.1.9.dist-info/RECORD +93 -0
- networka-0.1.9.dist-info/WHEEL +4 -0
- networka-0.1.9.dist-info/entry_points.txt +2 -0
- networka-0.1.9.dist-info/licenses/LICENSE +73 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from network_toolkit.tui.helpers import log_write
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class OutputPanelManager:
|
|
10
|
+
"""Manages the Output panel: All log, per-device tabs, buffers, and filtering.
|
|
11
|
+
|
|
12
|
+
This class encapsulates interaction with TabbedContent/TabPane across
|
|
13
|
+
Textual versions and maintains in-memory buffers so the UI can be
|
|
14
|
+
reconstructed at any time.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, app: Any, compat: Any) -> None:
|
|
18
|
+
self.app = app
|
|
19
|
+
self.compat = compat
|
|
20
|
+
# Expose buffers/maps on the app for legacy access/tests
|
|
21
|
+
if not hasattr(app, "_output_device_logs"):
|
|
22
|
+
app._output_device_logs = {}
|
|
23
|
+
if not hasattr(app, "_output_device_lines"):
|
|
24
|
+
app._output_device_lines = {}
|
|
25
|
+
if not hasattr(app, "_output_device_panes"):
|
|
26
|
+
app._output_device_panes = {}
|
|
27
|
+
if not hasattr(app, "_output_lines"):
|
|
28
|
+
app._output_lines = []
|
|
29
|
+
|
|
30
|
+
# ----- public API -----
|
|
31
|
+
def reset(self) -> None:
|
|
32
|
+
"""Clear All output, remove dynamic device tabs, and reset buffers."""
|
|
33
|
+
# Clear All
|
|
34
|
+
try:
|
|
35
|
+
out_all = self.app.query_one("#output-log")
|
|
36
|
+
if hasattr(out_all, "clear"):
|
|
37
|
+
out_all.clear()
|
|
38
|
+
except Exception:
|
|
39
|
+
pass
|
|
40
|
+
try:
|
|
41
|
+
self.app._output_lines = []
|
|
42
|
+
except Exception:
|
|
43
|
+
pass
|
|
44
|
+
# Remove all device tabs (prefer removing actual TabPane objects)
|
|
45
|
+
tabs = self._get_tabs()
|
|
46
|
+
device_keys = list(getattr(self.app, "_output_device_panes", {}).keys())
|
|
47
|
+
for dev_key in device_keys:
|
|
48
|
+
pane = self.app._output_device_panes.get(dev_key)
|
|
49
|
+
if pane is not None and tabs is not None:
|
|
50
|
+
removed = False
|
|
51
|
+
for method in ("remove_pane", "remove", "remove_tabs"):
|
|
52
|
+
try:
|
|
53
|
+
if hasattr(tabs, method):
|
|
54
|
+
if method == "remove_tabs":
|
|
55
|
+
getattr(tabs, method)([pane])
|
|
56
|
+
else:
|
|
57
|
+
getattr(tabs, method)(pane)
|
|
58
|
+
removed = True
|
|
59
|
+
break
|
|
60
|
+
except Exception as exc:
|
|
61
|
+
logging.debug(f"Removing pane via {method} failed: {exc}")
|
|
62
|
+
if not removed:
|
|
63
|
+
# Try remove_tab by label and by id
|
|
64
|
+
tab_id = self._tab_id_for_device(dev_key)
|
|
65
|
+
for meth in ("remove_tab",):
|
|
66
|
+
try:
|
|
67
|
+
if hasattr(tabs, meth):
|
|
68
|
+
# Attempt with label
|
|
69
|
+
try:
|
|
70
|
+
getattr(tabs, meth)(dev_key)
|
|
71
|
+
removed = True
|
|
72
|
+
break
|
|
73
|
+
except Exception:
|
|
74
|
+
# Attempt with id
|
|
75
|
+
getattr(tabs, meth)(tab_id)
|
|
76
|
+
removed = True
|
|
77
|
+
break
|
|
78
|
+
except Exception as exc:
|
|
79
|
+
logging.debug(f"Removing tab via {meth} failed: {exc}")
|
|
80
|
+
if not removed:
|
|
81
|
+
# Fallback: remove content child if we can reach it
|
|
82
|
+
try:
|
|
83
|
+
content = self.app.query_one(
|
|
84
|
+
f"#{self._tab_id_for_device(dev_key)}"
|
|
85
|
+
)
|
|
86
|
+
except Exception:
|
|
87
|
+
content = None
|
|
88
|
+
if content is not None:
|
|
89
|
+
self._remove_child(tabs, content)
|
|
90
|
+
else:
|
|
91
|
+
self._remove_child(tabs, pane)
|
|
92
|
+
# Fallback sweep
|
|
93
|
+
if tabs is not None:
|
|
94
|
+
try:
|
|
95
|
+
children = list(getattr(tabs, "children", []) or [])
|
|
96
|
+
except Exception:
|
|
97
|
+
children = []
|
|
98
|
+
for child in children:
|
|
99
|
+
try:
|
|
100
|
+
cid = getattr(child, "id", "") or ""
|
|
101
|
+
except Exception:
|
|
102
|
+
cid = ""
|
|
103
|
+
if cid.startswith("output-tab-") and cid != "output-tab-all":
|
|
104
|
+
self._remove_child(tabs, child)
|
|
105
|
+
# Reset maps
|
|
106
|
+
self.app._output_device_logs = {}
|
|
107
|
+
self.app._output_device_lines = {}
|
|
108
|
+
self.app._output_device_panes = {}
|
|
109
|
+
# Activate All
|
|
110
|
+
try:
|
|
111
|
+
if tabs is not None and hasattr(tabs, "active"):
|
|
112
|
+
tabs.active = "output-tab-all"
|
|
113
|
+
except Exception:
|
|
114
|
+
pass
|
|
115
|
+
|
|
116
|
+
def ensure_device_tab(self, device: str) -> Any | None:
|
|
117
|
+
dev_key = str(device)
|
|
118
|
+
if dev_key in self.app._output_device_logs:
|
|
119
|
+
return self.app._output_device_logs[dev_key]
|
|
120
|
+
tabs = self._get_tabs()
|
|
121
|
+
if tabs is None:
|
|
122
|
+
return None
|
|
123
|
+
tab_id = self._tab_id_for_device(dev_key)
|
|
124
|
+
# Create content log (id must match pane id for ContentSwitcher)
|
|
125
|
+
try:
|
|
126
|
+
log = self.compat.TextLogClass(id=tab_id, classes="scroll")
|
|
127
|
+
except Exception:
|
|
128
|
+
log = None
|
|
129
|
+
if log is None:
|
|
130
|
+
return None
|
|
131
|
+
# Create pane and mount content
|
|
132
|
+
pane = None
|
|
133
|
+
try:
|
|
134
|
+
pane = self.compat.TabPane(dev_key, log, id=tab_id)
|
|
135
|
+
except Exception:
|
|
136
|
+
try:
|
|
137
|
+
pane = self.compat.TabPane(dev_key, id=tab_id)
|
|
138
|
+
except Exception:
|
|
139
|
+
pane = None
|
|
140
|
+
if pane is not None and hasattr(pane, "mount"):
|
|
141
|
+
try:
|
|
142
|
+
pane.mount(log)
|
|
143
|
+
except Exception:
|
|
144
|
+
pass
|
|
145
|
+
if pane is None:
|
|
146
|
+
return None
|
|
147
|
+
# Add it
|
|
148
|
+
added = False
|
|
149
|
+
for method in ("add_pane", "add", "add_panes"):
|
|
150
|
+
try:
|
|
151
|
+
if hasattr(tabs, method):
|
|
152
|
+
if method == "add_panes":
|
|
153
|
+
getattr(tabs, method)([pane])
|
|
154
|
+
else:
|
|
155
|
+
getattr(tabs, method)(pane)
|
|
156
|
+
added = True
|
|
157
|
+
break
|
|
158
|
+
except Exception as exc:
|
|
159
|
+
logging.debug(f"Adding tab pane via {method} failed: {exc}")
|
|
160
|
+
if not added and hasattr(tabs, "add_tab"):
|
|
161
|
+
try:
|
|
162
|
+
tabs.add_tab(dev_key, log, id=tab_id)
|
|
163
|
+
added = True
|
|
164
|
+
except Exception as exc:
|
|
165
|
+
logging.debug(f"Adding tab via add_tab failed: {exc}")
|
|
166
|
+
if not added:
|
|
167
|
+
return None
|
|
168
|
+
self.app._output_device_logs[dev_key] = log
|
|
169
|
+
self.app._output_device_panes[dev_key] = pane
|
|
170
|
+
self.app._output_device_lines.setdefault(dev_key, [])
|
|
171
|
+
return log
|
|
172
|
+
|
|
173
|
+
def append_all(self, text: str) -> None:
|
|
174
|
+
lines = self._split_lines(text)
|
|
175
|
+
if not lines:
|
|
176
|
+
return
|
|
177
|
+
try:
|
|
178
|
+
self.app._maybe_show_output_panel()
|
|
179
|
+
except Exception:
|
|
180
|
+
pass
|
|
181
|
+
try:
|
|
182
|
+
self.app._output_lines.extend(lines)
|
|
183
|
+
except Exception:
|
|
184
|
+
self.app._output_lines = list(lines)
|
|
185
|
+
# Render depending on filter
|
|
186
|
+
try:
|
|
187
|
+
out_log = self.app.query_one("#output-log")
|
|
188
|
+
except Exception:
|
|
189
|
+
out_log = None
|
|
190
|
+
if out_log is None:
|
|
191
|
+
return
|
|
192
|
+
filt = (getattr(self.app, "_output_filter", "") or "").strip().lower()
|
|
193
|
+
if filt:
|
|
194
|
+
self._render_filtered(out_log, self.app._output_lines, filt)
|
|
195
|
+
else:
|
|
196
|
+
for line in lines:
|
|
197
|
+
log_write(out_log, line)
|
|
198
|
+
|
|
199
|
+
def append_device(self, device: str, text: str) -> None:
|
|
200
|
+
lines = self._split_lines(text)
|
|
201
|
+
if not lines:
|
|
202
|
+
return
|
|
203
|
+
try:
|
|
204
|
+
self.app._maybe_show_output_panel()
|
|
205
|
+
except Exception:
|
|
206
|
+
pass
|
|
207
|
+
dev_key = str(device)
|
|
208
|
+
try:
|
|
209
|
+
self.app._output_lines.extend(lines)
|
|
210
|
+
except Exception:
|
|
211
|
+
self.app._output_lines = list(lines)
|
|
212
|
+
buf = self.app._output_device_lines.setdefault(dev_key, [])
|
|
213
|
+
buf.extend(lines)
|
|
214
|
+
log_dev = self.ensure_device_tab(dev_key)
|
|
215
|
+
filt = (getattr(self.app, "_output_filter", "") or "").strip().lower()
|
|
216
|
+
if filt:
|
|
217
|
+
try:
|
|
218
|
+
self.apply_filter(filt)
|
|
219
|
+
except Exception:
|
|
220
|
+
pass
|
|
221
|
+
return
|
|
222
|
+
# Append to All and device logs
|
|
223
|
+
try:
|
|
224
|
+
out_all = self.app.query_one("#output-log")
|
|
225
|
+
for line in lines:
|
|
226
|
+
log_write(out_all, line)
|
|
227
|
+
except Exception:
|
|
228
|
+
pass
|
|
229
|
+
try:
|
|
230
|
+
if log_dev is not None:
|
|
231
|
+
for line in lines:
|
|
232
|
+
log_write(log_dev, line)
|
|
233
|
+
except Exception:
|
|
234
|
+
pass
|
|
235
|
+
|
|
236
|
+
def apply_filter(self, value: str) -> None:
|
|
237
|
+
self.app._output_filter = value
|
|
238
|
+
filt = (value or "").strip().lower()
|
|
239
|
+
# Determine active tab
|
|
240
|
+
try:
|
|
241
|
+
tabs = self._get_tabs()
|
|
242
|
+
active = getattr(tabs, "active", None)
|
|
243
|
+
active_id = getattr(active, "id", None) or str(active or "")
|
|
244
|
+
except Exception:
|
|
245
|
+
active_id = None
|
|
246
|
+
log_widget: Any | None = None
|
|
247
|
+
lines_src: list[str] = []
|
|
248
|
+
try:
|
|
249
|
+
if not active_id or "output-tab-all" in str(active_id):
|
|
250
|
+
log_widget = self.app.query_one("#output-log")
|
|
251
|
+
lines_src = list(self.app._output_lines)
|
|
252
|
+
elif str(active_id).startswith("output-tab-"):
|
|
253
|
+
dev_part = str(active_id)[len("output-tab-") :]
|
|
254
|
+
dev_key = self._find_device_key(dev_part)
|
|
255
|
+
log_widget = self.app._output_device_logs.get(dev_key)
|
|
256
|
+
lines_src = list(self.app._output_device_lines.get(dev_key, []))
|
|
257
|
+
except Exception:
|
|
258
|
+
pass
|
|
259
|
+
if log_widget is None:
|
|
260
|
+
return
|
|
261
|
+
self._render_filtered(log_widget, lines_src, filt)
|
|
262
|
+
|
|
263
|
+
def recreate(self) -> None:
|
|
264
|
+
"""Recreate tabs and logs from in-memory buffers."""
|
|
265
|
+
tabs = self._get_tabs()
|
|
266
|
+
if tabs is None:
|
|
267
|
+
return
|
|
268
|
+
# Clear and re-add per-device tabs
|
|
269
|
+
self.reset()
|
|
270
|
+
for dev_key in list(self.app._output_device_lines.keys()):
|
|
271
|
+
log = self.ensure_device_tab(dev_key)
|
|
272
|
+
if log is None:
|
|
273
|
+
continue
|
|
274
|
+
for line in self.app._output_device_lines.get(dev_key, []):
|
|
275
|
+
log_write(log, line)
|
|
276
|
+
# Re-render All
|
|
277
|
+
try:
|
|
278
|
+
out_all = self.app.query_one("#output-log")
|
|
279
|
+
filt = (getattr(self.app, "_output_filter", "") or "").strip().lower()
|
|
280
|
+
if filt:
|
|
281
|
+
self._render_filtered(out_all, self.app._output_lines, filt)
|
|
282
|
+
else:
|
|
283
|
+
if hasattr(out_all, "clear"):
|
|
284
|
+
out_all.clear()
|
|
285
|
+
for line in self.app._output_lines:
|
|
286
|
+
log_write(out_all, line)
|
|
287
|
+
except Exception:
|
|
288
|
+
pass
|
|
289
|
+
|
|
290
|
+
# ----- helpers -----
|
|
291
|
+
def _get_tabs(self) -> Any | None:
|
|
292
|
+
try:
|
|
293
|
+
return self.app.query_one("#output-tabs")
|
|
294
|
+
except Exception:
|
|
295
|
+
return None
|
|
296
|
+
|
|
297
|
+
def _tab_id_for_device(self, device_key: str) -> str:
|
|
298
|
+
dev_id = self._sanitize_id(device_key)
|
|
299
|
+
return f"output-tab-{dev_id}"
|
|
300
|
+
|
|
301
|
+
def _split_lines(self, text: str) -> list[str]:
|
|
302
|
+
try:
|
|
303
|
+
s = str(text)
|
|
304
|
+
except Exception:
|
|
305
|
+
s = f"{text}"
|
|
306
|
+
return list(s.splitlines())
|
|
307
|
+
|
|
308
|
+
def _render_filtered(self, log_widget: Any, lines: list[str], filt: str) -> None:
|
|
309
|
+
try:
|
|
310
|
+
if hasattr(log_widget, "clear"):
|
|
311
|
+
log_widget.clear()
|
|
312
|
+
except Exception:
|
|
313
|
+
pass
|
|
314
|
+
for line in lines:
|
|
315
|
+
try:
|
|
316
|
+
if not filt or (filt in line.lower()):
|
|
317
|
+
log_write(log_widget, line)
|
|
318
|
+
except Exception:
|
|
319
|
+
pass
|
|
320
|
+
|
|
321
|
+
def _find_device_key(self, dev_id_part: str) -> str:
|
|
322
|
+
# Match sanitized id first
|
|
323
|
+
for k in self.app._output_device_lines.keys():
|
|
324
|
+
if self._sanitize_id(k) == dev_id_part:
|
|
325
|
+
return str(k)
|
|
326
|
+
return str(dev_id_part)
|
|
327
|
+
|
|
328
|
+
def _sanitize_id(self, name: str) -> str:
|
|
329
|
+
# Prefer app helper if present
|
|
330
|
+
try:
|
|
331
|
+
return str(self.app._sanitize_id(name))
|
|
332
|
+
except Exception:
|
|
333
|
+
try:
|
|
334
|
+
import re
|
|
335
|
+
|
|
336
|
+
return re.sub(r"[^A-Za-z0-9_.-]", "-", name)
|
|
337
|
+
except Exception:
|
|
338
|
+
return name.replace(" ", "-")
|
|
339
|
+
|
|
340
|
+
def _remove_tab_by_id(self, tabs: Any | None, pane_id: str) -> None:
|
|
341
|
+
if tabs is None:
|
|
342
|
+
return
|
|
343
|
+
pane = None
|
|
344
|
+
try:
|
|
345
|
+
pane = self.app.query_one(f"#{pane_id}")
|
|
346
|
+
except Exception:
|
|
347
|
+
pane = None
|
|
348
|
+
if pane is None:
|
|
349
|
+
return
|
|
350
|
+
for method in ("remove_pane", "remove", "remove_tabs"):
|
|
351
|
+
try:
|
|
352
|
+
if hasattr(tabs, method):
|
|
353
|
+
if method == "remove_tabs":
|
|
354
|
+
getattr(tabs, method)([pane])
|
|
355
|
+
else:
|
|
356
|
+
getattr(tabs, method)(pane)
|
|
357
|
+
return
|
|
358
|
+
except Exception as exc:
|
|
359
|
+
logging.debug(f"Removing device tab via {method} failed: {exc}")
|
|
360
|
+
# Fallback remove on child
|
|
361
|
+
self._remove_child(tabs, pane)
|
|
362
|
+
|
|
363
|
+
def _remove_child(self, tabs: Any, child: Any) -> None:
|
|
364
|
+
try:
|
|
365
|
+
if hasattr(tabs, "remove"):
|
|
366
|
+
tabs.remove(child)
|
|
367
|
+
elif hasattr(child, "remove"):
|
|
368
|
+
child.remove()
|
|
369
|
+
elif hasattr(child, "unmount"):
|
|
370
|
+
child.unmount()
|
|
371
|
+
except Exception:
|
|
372
|
+
pass
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""Unified prompt component for TUI info and yes/no questions.
|
|
2
|
+
|
|
3
|
+
Provides a single, consistent ModalScreen-based UI for:
|
|
4
|
+
- show_info: transient informational notices (optionally auto-dismiss)
|
|
5
|
+
- ask_yes_no: simple yes/no questions with keyboard shortcuts
|
|
6
|
+
|
|
7
|
+
Falls back gracefully (returns False) if Textual is not available.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from typing import Any, Literal
|
|
14
|
+
|
|
15
|
+
try: # Optional at import time; provide stubs if unavailable
|
|
16
|
+
from textual.containers import Vertical as _Vertical # type: ignore
|
|
17
|
+
from textual.screen import ModalScreen as _ModalScreenBase # type: ignore
|
|
18
|
+
from textual.widgets import Button as _Button # type: ignore
|
|
19
|
+
from textual.widgets import Static as _Static
|
|
20
|
+
except Exception: # pragma: no cover - textual not installed
|
|
21
|
+
_ModalScreenBase = object # type: ignore
|
|
22
|
+
|
|
23
|
+
class _Button: # type: ignore
|
|
24
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
|
|
25
|
+
|
|
26
|
+
class _Static: # type: ignore
|
|
27
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
|
|
28
|
+
|
|
29
|
+
class _Vertical: # type: ignore
|
|
30
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
|
|
31
|
+
|
|
32
|
+
def __enter__(self) -> Any: # Return Any to match imported Vertical
|
|
33
|
+
return self
|
|
34
|
+
|
|
35
|
+
def __exit__(self, *_: Any) -> Literal[False]:
|
|
36
|
+
return False
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class _PromptScreen(_ModalScreenBase): # type: ignore[misc]
|
|
40
|
+
DEFAULT_CSS = (
|
|
41
|
+
"#nw-prompt { width: 70; border: round $surface; padding: 1 2; }\n"
|
|
42
|
+
"#nw-prompt Static.title { content-align: center middle; text-style: bold; }\n"
|
|
43
|
+
"#nw-prompt .buttons { content-align: center middle; height: 3; }\n"
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
message: str,
|
|
49
|
+
*,
|
|
50
|
+
mode: str = "info",
|
|
51
|
+
on_yes: Callable[[], None] | None = None,
|
|
52
|
+
on_no: Callable[[], None] | None = None,
|
|
53
|
+
on_close: Callable[[], None] | None = None,
|
|
54
|
+
) -> None:
|
|
55
|
+
super().__init__() # type: ignore[misc]
|
|
56
|
+
self._message = message
|
|
57
|
+
self._mode = mode
|
|
58
|
+
self._on_yes = on_yes
|
|
59
|
+
self._on_no = on_no
|
|
60
|
+
self._on_close = on_close
|
|
61
|
+
|
|
62
|
+
def compose(self) -> Any:
|
|
63
|
+
with _Vertical(id="nw-prompt"): # type: ignore[misc]
|
|
64
|
+
yield _Static(self._message, classes="title")
|
|
65
|
+
with _Vertical(classes="buttons"): # type: ignore[misc]
|
|
66
|
+
if self._mode == "yesno":
|
|
67
|
+
yield _Button("Yes [y]", id="btn-yes")
|
|
68
|
+
yield _Button("No [N]/Esc", id="btn-no")
|
|
69
|
+
else:
|
|
70
|
+
yield _Button("OK [Enter]", id="btn-ok")
|
|
71
|
+
|
|
72
|
+
def on_button_pressed(self, event: Any) -> None:
|
|
73
|
+
bid = getattr(getattr(event, "button", None), "id", "")
|
|
74
|
+
if bid == "btn-yes" and self._on_yes:
|
|
75
|
+
self._on_yes()
|
|
76
|
+
elif bid in {"btn-no", "btn-ok"}:
|
|
77
|
+
if self._on_no and self._mode == "yesno":
|
|
78
|
+
self._on_no()
|
|
79
|
+
elif self._on_close and self._mode != "yesno":
|
|
80
|
+
self._on_close()
|
|
81
|
+
try:
|
|
82
|
+
self.dismiss(None) # type: ignore[call-arg]
|
|
83
|
+
except Exception:
|
|
84
|
+
pass
|
|
85
|
+
|
|
86
|
+
def on_key(self, event: Any) -> None:
|
|
87
|
+
key = str(getattr(event, "key", "")).lower()
|
|
88
|
+
if self._mode == "yesno":
|
|
89
|
+
if key == "y" and self._on_yes:
|
|
90
|
+
self._on_yes()
|
|
91
|
+
try:
|
|
92
|
+
self.dismiss(None) # type: ignore[call-arg]
|
|
93
|
+
except Exception:
|
|
94
|
+
pass
|
|
95
|
+
elif key in {"n", "escape", "enter", "return"}:
|
|
96
|
+
if self._on_no:
|
|
97
|
+
self._on_no()
|
|
98
|
+
try:
|
|
99
|
+
self.dismiss(None) # type: ignore[call-arg]
|
|
100
|
+
except Exception:
|
|
101
|
+
pass
|
|
102
|
+
elif key in {"enter", "return", "escape", "space"}:
|
|
103
|
+
if self._on_close:
|
|
104
|
+
self._on_close()
|
|
105
|
+
try:
|
|
106
|
+
self.dismiss(None) # type: ignore[call-arg]
|
|
107
|
+
except Exception:
|
|
108
|
+
pass
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class Prompt:
|
|
112
|
+
@staticmethod
|
|
113
|
+
def show_info(app: Any, message: str, *, timeout: float | None = 3.0) -> bool:
|
|
114
|
+
"""Show an informational prompt. Returns True if shown, False if not."""
|
|
115
|
+
if _ModalScreenBase is object:
|
|
116
|
+
return False
|
|
117
|
+
try:
|
|
118
|
+
screen = _PromptScreen(message, mode="info")
|
|
119
|
+
app.push_screen(screen)
|
|
120
|
+
if timeout:
|
|
121
|
+
try:
|
|
122
|
+
app.set_timer(timeout, lambda: app.pop_screen())
|
|
123
|
+
except Exception:
|
|
124
|
+
pass
|
|
125
|
+
return True
|
|
126
|
+
except Exception:
|
|
127
|
+
return False
|
|
128
|
+
|
|
129
|
+
@staticmethod
|
|
130
|
+
async def ask_yes_no(app: Any, message: str, *, default_no: bool = True) -> bool:
|
|
131
|
+
"""Ask a yes/no question; return True for Yes, False for No.
|
|
132
|
+
|
|
133
|
+
If Textual isn't available, returns not default_no.
|
|
134
|
+
"""
|
|
135
|
+
if _ModalScreenBase is object:
|
|
136
|
+
return not default_no
|
|
137
|
+
fut: Any = None
|
|
138
|
+
try:
|
|
139
|
+
import asyncio
|
|
140
|
+
|
|
141
|
+
loop = asyncio.get_running_loop()
|
|
142
|
+
fut = loop.create_future()
|
|
143
|
+
except Exception:
|
|
144
|
+
fut = None
|
|
145
|
+
|
|
146
|
+
result_box = {"val": not default_no}
|
|
147
|
+
|
|
148
|
+
def _yes() -> None:
|
|
149
|
+
result_box["val"] = True
|
|
150
|
+
if fut and not fut.done(): # type: ignore[truthy-bool]
|
|
151
|
+
fut.set_result(True)
|
|
152
|
+
|
|
153
|
+
def _no() -> None:
|
|
154
|
+
result_box["val"] = False
|
|
155
|
+
if fut and not fut.done(): # type: ignore[truthy-bool]
|
|
156
|
+
fut.set_result(False)
|
|
157
|
+
|
|
158
|
+
try:
|
|
159
|
+
app.push_screen(
|
|
160
|
+
_PromptScreen(message, mode="yesno", on_yes=_yes, on_no=_no)
|
|
161
|
+
)
|
|
162
|
+
except Exception:
|
|
163
|
+
return not default_no
|
|
164
|
+
|
|
165
|
+
if fut is None:
|
|
166
|
+
# No event loop available; rely on default
|
|
167
|
+
return result_box["val"]
|
|
168
|
+
|
|
169
|
+
try:
|
|
170
|
+
return bool(await fut)
|
|
171
|
+
except Exception:
|
|
172
|
+
return result_box["val"]
|