-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_editor.py
306 lines (261 loc) · 9.29 KB
/
table_editor.py
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
295
296
297
298
299
300
301
302
303
304
305
306
from io import StringIO
from typing import Generator, Sequence, SupportsInt
from itertools import zip_longest
import keyboard
from terminal import Font, terminal_cursor
def replace_none_with[
T
](*vals: Sequence[T | None], fillvalue: T = "") -> Generator[tuple[T, ...], None, None]:
for val in vals:
yield tuple((fillvalue if col is None else col) for col in val)
def replace_none[
T
](*vals: T | None | Sequence[T | None], fillvalue: T = "") -> Generator[T, None, None]:
if isinstance(vals[0], Sequence):
for val in vals:
yield type(val)((fillvalue if col is None else col) for col in val)
else:
yield from ((fillvalue if col is None else col) for col in vals)
string = "─│┌┐└┘├┤┬┴┼═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬"
class Table:
def __init__(self, *values) -> None:
self.values = values
self.headings = None
self.padding = 2
self.empty_default = " "
self.empty_heading = "Untitled"
def with_headings(self, *headings):
self.headings = headings
return self
def with_padding(self, padding: int = 2):
self.padding = padding or self.padding
return self
def with_default(self, empty_default=""):
self.empty_default = empty_default or self.empty_default
return self
def with_default_heading(self, empty_heading: str = ""):
self.empty_heading = empty_heading
return self
def __str__(self) -> str:
return tabulates(
*self.values,
padding=self.padding,
headings=self.headings,
empty_default=self.empty_default,
empty_heading=self.empty_heading,
).strip()
def tabulate(*values: Sequence[str | None]):
return Table(*values)
def _tabulate(
*_values: Sequence[str | None],
selected_cell: tuple[int, int] | None = (0, 0),
highlight_cell: tuple[int, int] | None = (0, 0),
padding: SupportsInt = 2,
headings: Sequence[str | None] | None = None,
empty_default: str = " ",
empty_heading: str = "Untitled",
file=None,
):
# from t import print
headings = headings or []
vals: Sequence[Sequence[str]] = list(
zip_longest(
*zip_longest(
*replace_none(*_values, fillvalue=empty_default),
fillvalue=empty_default,
),
fillvalue=empty_default,
)
)
if headings:
vals: Sequence[Sequence[str]] = list(
zip_longest(
replace_none(*headings, fillvalue=empty_heading),
*vals,
fillvalue=empty_default,
)
)
t_values: Sequence[Sequence[str]] = list(zip(*vals))
sizes = _calculate_sizes(*vals, padding=padding)
print("╔" + "╤".join(["═" * size for size in sizes]) + "╗", file=file)
for row_idx, row in enumerate(t_values):
print("║", end="", file=file)
for col_idx, (size, col) in enumerate(zip(sizes, row)):
if row_idx < 1 and headings:
print("\033[1m" + col.center(size), end="\033[0m", file=file)
elif (col_idx, row_idx) == selected_cell:
print(
"\033[45m"
+ Font.bold_font
+ Font.underline_font
+ col.center(size, " ")
+ "",
end="\033[0m",
file=file,
)
elif (col_idx, row_idx) == highlight_cell:
print(
"\033[41m" + Font.bold_font + col.center(size),
end="\033[0m",
file=file,
)
else:
print(col.center(size), end="", file=file)
if col_idx < (len(t_values[row_idx]) - 1):
print("│", end="", file=file)
print("║", file=file)
if row_idx < 1 and headings:
print("╠" + "╪".join(["═" * size for size in sizes]), end="╣\n", file=file)
elif row_idx < len(vals[0]) - 1:
print("╟" + "┼".join(["─" * size for size in sizes]), end="╢\n", file=file)
print("╚" + "╧".join(["═" * size for size in sizes]) + "╝", file=file)
return file
def tabulates(
*values: Sequence[str | None],
padding: SupportsInt = 2,
headings: Sequence[str | None] | None = None,
empty_default: str = " ",
**kwargs,
) -> str:
print(values)
highlight_cell: tuple[int, int] | None = (0, 0)
selected_cell: tuple[int, int] | None = None
key_pressed = False
selected = False
value_entered = False
closed = False
max_lens = len(values), max(len(value) for value in values)
values: Sequence[Sequence[str]] = list(
zip_longest(
*zip_longest(
*replace_none(*values, fillvalue=None),
fillvalue=None,
),
fillvalue=None,
)
)
if headings:
values: Sequence[Sequence[str]] = list(
zip_longest(
replace_none(*headings, fillvalue=None),
*values,
fillvalue=None,
)
)
list_values = []
for val in values:
list_value = []
for col in val:
list_value.append(col)
list_values.append(list_value)
values = list_values
def up_arrow_event():
nonlocal key_pressed, highlight_cell
if not highlight_cell:
return
highlight_cell = (highlight_cell[0], (highlight_cell[1] - 1) % max_lens[1])
key_pressed = True
def down_arrow_event():
nonlocal key_pressed, highlight_cell
if not highlight_cell:
return
highlight_cell = (highlight_cell[0], (highlight_cell[1] + 1) % max_lens[1])
key_pressed = True
def left_arrow_event():
nonlocal key_pressed, highlight_cell
if not highlight_cell:
return
highlight_cell = ((highlight_cell[0] - 1) % max_lens[0], highlight_cell[1])
key_pressed = True
def right_arrow_event():
nonlocal key_pressed, highlight_cell
if not highlight_cell:
return
highlight_cell = ((highlight_cell[0] + 1) % max_lens[0], highlight_cell[1])
key_pressed = True
def enter_event():
nonlocal values, key_pressed, selected_cell, highlight_cell, selected, value_entered
if selected and selected_cell and not value_entered:
value_entered = True
key_pressed = True
value: str = ""
ch = ""
print(keyboard.read_key(True))
values[selected_cell[0]][selected_cell[1]] = value + "col"
value_entered = False
return
elif not selected or selected_cell:
selected_cell = highlight_cell
highlight_cell = None
selected = True
key_pressed = True
print("Test")
def escape_event():
nonlocal key_pressed, selected_cell, highlight_cell, selected, closed
if not selected:
closed = True
highlight_cell = selected_cell
selected_cell = None
selected = False
key_pressed = True
keyboard.add_hotkey("up", up_arrow_event, suppress=True)
keyboard.add_hotkey("down", down_arrow_event, suppress=True)
keyboard.add_hotkey("left", left_arrow_event, suppress=True)
keyboard.add_hotkey("right", right_arrow_event, suppress=True)
keyboard.add_hotkey("enter", enter_event, suppress=True)
keyboard.add_hotkey("escape", escape_event, suppress=True)
with StringIO() as buf, terminal_cursor() as crsr:
while not closed:
for _ in range(max_lens[1] * 2 + 2):
print(" " * 100)
crsr.move_up(max_lens[1] * 2 + 3)
_tabulate(
*values,
selected_cell=selected_cell,
highlight_cell=highlight_cell,
padding=padding,
headings=headings,
empty_default=empty_default,
# file=buf,
**kwargs,
)
buf.seek(0)
# print(buf.getvalue().strip())
key_pressed = False
while not key_pressed:
pass
crsr.move_up(max_lens[1] * 2 + 2 + (2 if headings else 0))
_tabulate(
*values,
selected_cell=None,
highlight_cell=None,
padding=padding,
headings=headings,
empty_default=empty_default,
# file=buf,
**kwargs,
)
return ""
# return buf.getvalue()
def _calculate_sizes(*values: Sequence[str], padding: SupportsInt = 2) -> Sequence[int]:
sizes = []
for value in values:
longest = len(max(value, key=len))
cell_size = int(padding) + longest + int(padding)
sizes.append(cell_size)
return sizes
if __name__ == "__main__":
# from t import print as p
# print(
str(
tabulate(
["Hello", "This is a test.", "Cool"],
["This is cool stuff", "Test Cool"],
[None, None, "Cool stuff"],
)
# .with_headings("Systems", "Invitations", None)
.with_padding(5)
.with_default("N/A")
.with_default_heading("Untitled")
)
# )