-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui_utils.py
More file actions
294 lines (259 loc) · 9.72 KB
/
tui_utils.py
File metadata and controls
294 lines (259 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""Small curses helpers shared by the TUIs in this repo."""
from __future__ import annotations
import curses
from dataclasses import dataclass
from typing import Optional, Tuple
@dataclass
class LineEditResult:
value: str
accepted: bool
@dataclass
class MultiSelectResult:
indices: list[int]
accepted: bool
def _safe_curs_set(visibility: int) -> None:
try:
curses.curs_set(visibility)
except Exception:
pass
def edit_line_dialog(
stdscr: "curses._CursesWindow",
*,
title: str,
initial: str = "",
instructions: str | None = None,
max_width: int = 90,
allow_empty: bool = True,
) -> LineEditResult:
"""Edit a single line of text.
- Enter: accept
- Esc: cancel
- Ctrl+U: clear
"""
dialog_height = 5
field_x = 2
field_y = 2
title_y = 1
instructions_y = 3
def layout() -> tuple["curses._CursesWindow", "curses._CursesWindow", int, int]:
h, w = stdscr.getmaxyx()
if h < dialog_height or w < 20:
raise RuntimeError("terminal too small")
dialog_width = min(max_width, w)
dialog_width = max(20, dialog_width)
dialog_width = min(dialog_width, w)
start_y = max(0, (h - dialog_height) // 2)
start_x = max(0, (w - dialog_width) // 2)
win = curses.newwin(dialog_height, dialog_width, start_y, start_x)
win.keypad(True)
win.border()
clean_title = (title or "").strip() or "Enter value"
try:
win.addnstr(title_y, 2, clean_title, max(1, dialog_width - 4), curses.A_BOLD)
except curses.error:
pass
help_line = instructions
if help_line is None:
help_line = "Enter: save Esc: cancel Ctrl+U: clear"
try:
win.addnstr(instructions_y, 2, help_line, max(1, dialog_width - 4), curses.A_DIM)
except curses.error:
pass
field_width = max(1, dialog_width - 4)
field = win.derwin(1, field_width, field_y, field_x)
field.keypad(True)
field.bkgd(" ", curses.A_REVERSE)
return win, field, dialog_width, field_width
try:
win, field, dialog_width, field_width = layout()
except Exception:
return LineEditResult(value=initial, accepted=False)
buffer = list(str(initial or ""))
cursor = len(buffer)
scroll = 0
_safe_curs_set(1)
try:
while True:
if cursor < scroll:
scroll = cursor
render_width = max(1, field_width - 1)
visible_capacity = max(1, render_width - 1)
if cursor > scroll + visible_capacity:
scroll = cursor - visible_capacity
if scroll < 0:
scroll = 0
visible = "".join(buffer[scroll : scroll + render_width])
try:
field.erase()
try:
field.addnstr(0, 0, visible.ljust(render_width), render_width, curses.A_REVERSE)
except curses.error:
# Ignore last-column write failures; keep the dialog usable.
pass
field.move(0, max(0, min(render_width - 1, cursor - scroll)))
field.refresh()
win.refresh()
except curses.error:
try:
win, field, dialog_width, field_width = layout()
scroll = min(scroll, max(0, len(buffer) - 1))
except Exception:
return LineEditResult(value=initial, accepted=False)
continue
key = win.getch()
if key in (curses.KEY_ENTER, ord("\n"), ord("\r")):
value = "".join(buffer)
value = value.strip()
if value or allow_empty:
return LineEditResult(value=value, accepted=True)
return LineEditResult(value=initial, accepted=False)
if key == 27: # Esc
return LineEditResult(value=initial, accepted=False)
if key in (curses.KEY_RESIZE,):
try:
win, field, dialog_width, field_width = layout()
scroll = min(scroll, max(0, len(buffer) - 1))
except Exception:
return LineEditResult(value=initial, accepted=False)
continue
if key in (curses.KEY_BACKSPACE, 127, 8):
if cursor > 0:
buffer.pop(cursor - 1)
cursor -= 1
continue
if key in (curses.KEY_DC,):
if cursor < len(buffer):
buffer.pop(cursor)
continue
if key in (curses.KEY_LEFT,):
cursor = max(0, cursor - 1)
continue
if key in (curses.KEY_RIGHT,):
cursor = min(len(buffer), cursor + 1)
continue
if key in (curses.KEY_HOME,):
cursor = 0
continue
if key in (curses.KEY_END,):
cursor = len(buffer)
continue
if key == 21: # Ctrl+U
buffer.clear()
cursor = 0
scroll = 0
continue
if 0 <= key <= 255:
ch = chr(key)
if ch.isprintable():
buffer.insert(cursor, ch)
cursor += 1
continue
finally:
_safe_curs_set(0)
return LineEditResult(value=initial, accepted=False)
def multi_select_dialog(
stdscr: "curses._CursesWindow",
*,
title: str,
items: list[str],
selected: list[int] | None = None,
instructions: str | None = None,
max_width: int = 90,
max_height: int = 18,
) -> MultiSelectResult:
"""Select multiple items from a list.
- Up/Down: move
- Space: toggle
- A: select all
- N: select none
- Enter: accept
- Esc: cancel
"""
selected_set = set(selected or [])
cursor = 0
offset = 0
def layout() -> tuple["curses._CursesWindow", int, int]:
h, w = stdscr.getmaxyx()
dialog_height = min(max_height, max(6, h - 2))
dialog_width = min(max_width, max(20, w - 2))
start_y = max(0, (h - dialog_height) // 2)
start_x = max(0, (w - dialog_width) // 2)
win = curses.newwin(dialog_height, dialog_width, start_y, start_x)
win.keypad(True)
win.border()
return win, dialog_height, dialog_width
try:
win, dialog_height, dialog_width = layout()
except Exception:
return MultiSelectResult(indices=selected or [], accepted=False)
header = (title or "Select items").strip()
help_line = instructions or "Up/Down: move Space: toggle A: all N: none Enter: save Esc: cancel"
_safe_curs_set(0)
try:
while True:
try:
win.erase()
win.border()
win.addnstr(1, 2, header, max(1, dialog_width - 4), curses.A_BOLD)
win.addnstr(2, 2, help_line, max(1, dialog_width - 4), curses.A_DIM)
list_top = 3
list_height = max(1, dialog_height - 4)
if cursor < 0:
cursor = 0
if cursor >= len(items):
cursor = max(0, len(items) - 1)
if cursor < offset:
offset = cursor
if cursor >= offset + list_height:
offset = cursor - list_height + 1
if offset < 0:
offset = 0
visible = items[offset : offset + list_height]
for row, label in enumerate(visible):
idx = offset + row
prefix = "[x]" if idx in selected_set else "[ ]"
line = f"{prefix} {label}"
attr = curses.A_REVERSE if idx == cursor else curses.A_NORMAL
win.addnstr(list_top + row, 2, line, max(1, dialog_width - 4), attr)
win.refresh()
except Exception:
return MultiSelectResult(indices=selected or [], accepted=False)
key = win.getch()
if key in (curses.KEY_ENTER, ord("\n"), ord("\r")):
return MultiSelectResult(indices=sorted(selected_set), accepted=True)
if key == 27: # Esc
return MultiSelectResult(indices=selected or [], accepted=False)
if key in (curses.KEY_RESIZE,):
try:
win, dialog_height, dialog_width = layout()
except Exception:
return MultiSelectResult(indices=selected or [], accepted=False)
continue
if key in (curses.KEY_UP, ord("k")):
cursor = max(0, cursor - 1)
continue
if key in (curses.KEY_DOWN, ord("j")):
cursor = min(len(items) - 1, cursor + 1) if items else 0
continue
if key in (curses.KEY_PPAGE,):
cursor = max(0, cursor - max(1, list_height))
continue
if key in (curses.KEY_NPAGE,):
cursor = min(len(items) - 1, cursor + max(1, list_height)) if items else 0
continue
if key in (ord(" "),):
if cursor in selected_set:
selected_set.remove(cursor)
else:
selected_set.add(cursor)
continue
if key in (ord("a"), ord("A")):
selected_set = set(range(len(items)))
continue
if key in (ord("n"), ord("N")):
selected_set.clear()
continue
finally:
_safe_curs_set(0)
return MultiSelectResult(indices=selected or [], accepted=False)