-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidgets.py
307 lines (258 loc) · 7.74 KB
/
widgets.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
307
# vault.py
import os
from typing import TypeVar
import pyperclip as pc
from rich.align import Align
from rich.box import HEAVY
from rich.color import Color
from rich.console import RenderableType
from rich.panel import Panel
from rich.style import Style
from rich.text import Text
from textual import events
from textual.scrollbar import ScrollBar, ScrollBarRender
from textual.views import GridView
from textual.widget import Widget
from textual.widgets import (
Button, Footer, TreeControl, TreeNode
)
from mixins import ButtonMixin, InputTextMixin
from settings import (
ACTION_TIME,
BRIGHT_GREEN,
COPY,
DONE,
GRAY,
GREEN,
KEY,
LOCAL_STYLE,
NOTIFICATION_TIME,
YELLOW
)
NodeDataType = TypeVar('NodeDataType')
class CellGrid(GridView):
def __init__(self, *args, cells, **kwargs):
super().__init__(*args, **kwargs)
self.cells = cells
async def on_mount(self) -> None:
self.grid.add_column(
'col',
fraction=1,
min_size=8,
max_size=24,
repeat=2
)
self.grid.add_row(
'row',
fraction=1,
max_size=3
)
self.grid.set_repeat(True, True)
self.grid.set_align('center', 'center')
self.update_cells(self.cells)
def update_cells(self, cells, repeat_grid=(True, True)) -> None:
self.grid.set_repeat(*repeat_grid)
self.grid.widgets.clear()
self.grid.place(*cells)
self.refresh()
class CellButton(ButtonMixin, Button):
def __init__(
self, *args, encoder, title, value, action, **kwargs
):
super().__init__(*args, **kwargs)
self.encoder = encoder
self.title = self.encoder.decode(title)
self.label = self.encoder.decode(self.label)
self.value = value
self.on_click_label = KEY
self.action = (
action
or (lambda title, label, value: pc.copy(value))
)
def on_click(self) -> None:
super().on_click()
self.action(
self.title, self.label, self.encoder.decode(self.value)
)
class CopyButton(ButtonMixin, Button):
def __init__(
self, *args, title, sec=ACTION_TIME, action=None, **kwargs
):
super().__init__(*args, **kwargs)
self.title = title
self.on_click_label = COPY
self.action = action
self.visible = False
self.sec = sec
def hide(self):
self.visible = False
def on_click(self) -> None:
super().on_click()
if self.action:
loc = self.action()
if loc:
pc.copy(loc)
if self.sec:
self.set_timer(self.sec, lambda: self.hide())
else:
self.hide()
class ActionButton(ButtonMixin, Button):
def __init__(
self, *args, title, sec=ACTION_TIME, action=None, **kwargs
):
super().__init__(*args, **kwargs)
self.title = title
self.on_click_label = DONE
self.action = action
self.visible = False
self.sec = sec
def hide(self):
self.visible = False
def _hide_action(self):
self.hide()
if self.action:
self.action()
def on_click(self) -> None:
super().on_click()
if self.sec:
self.set_timer(self.sec, lambda: self._hide_action())
else:
self._hide_action()
class Notification(Widget):
def __init__(self, title: str, label: str):
super().__init__(title)
self.title = title
self.label = label
self.border_color = GREEN
self.visible = False
def render(self) -> Panel:
return Panel(
Align.center(
Text(self.label),
vertical='middle',
style=GREEN
),
title=self.title,
title_align='left',
border_style=Style(color=self.border_color),
box=HEAVY
)
def on_click(self) -> None:
self.visible = False
def hide(self):
self.visible = False
def show(self, title, label, color=GREEN, sec=NOTIFICATION_TIME):
self.title = title
self.label = label
self.border_color = color
self.visible = True
if sec:
self.set_timer(sec, lambda: self.hide())
class LoadScroll(ScrollBar):
def render(self) -> RenderableType:
style = Style(
bgcolor=Color.parse(GRAY),
color=GREEN,
)
return ScrollBarRender(
virtual_size=self.virtual_size,
window_size=self.window_size,
position=self.position,
vertical=self.vertical,
style=style,
)
class LoadTree(TreeControl):
async def update_dirs(self, cwd='.'):
for folder in os.listdir(cwd):
if folder.startswith('.'):
continue
if os.path.isdir(folder):
await self.add(
self.root.id, folder, {'dir': folder}
)
else:
await self.add(
self.root.id, folder, {'path': folder}
)
self.refresh(layout=True)
def render_node(
self, node: TreeNode[NodeDataType]
) -> RenderableType:
color = GRAY
is_clickable = False
if os.path.isdir(node.label):
try:
os.listdir(node.label)
color = BRIGHT_GREEN
is_clickable = True
except PermissionError:
pass
elif os.path.splitext(node.label)[1] == '.json':
color = YELLOW
is_clickable = True
label = (
Text(
node.label,
no_wrap=True,
style=color,
overflow='ellipsis'
)
if isinstance(node.label, str)
else node.label
)
if is_clickable:
if node.id == self.hover_node:
label.stylize('underline')
label.apply_meta(
{
'@click': f'click_label({node.id})',
'tree_node': node.id
}
)
return label
class InputText(InputTextMixin, Widget):
def __init__(self, title: str, label: str):
super().__init__()
self.title = title
self.label = label
self.visible = False
def on_key(self, event: events.Key) -> None:
if self.mouse_over and self.clicked:
if str(event.key) == 'ctrl+v':
self.label = ''
self.label += pc.paste()
self.refresh()
def hide(self):
self.visible = False
class HighlightFooter(Footer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._style = LOCAL_STYLE
@property
def style(self):
return self._style
@style.setter
def style(self, style):
self._style = style
def make_key_text(self) -> Text:
text = Text(
style=self.style,
no_wrap=True,
overflow='ellipsis',
justify='left',
end='',
)
for binding in self.app.bindings.shown_keys:
key_display = (
binding.key.upper()
if binding.key_display is None
else binding.key_display
)
hovered = self.highlight_key == binding.key
key_text = Text.assemble(
(f' {key_display} ', 'reverse' if hovered else 'default on default'),
f' {binding.description} ',
meta={'@click': f"app.press('{binding.key}')", 'key': binding.key},
)
text.append_text(key_text)
return text