-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextualHelper.py
376 lines (310 loc) · 10 KB
/
TextualHelper.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from textual.app import App, ComposeResult
from textual.widgets import Header, Footer
from textual.containers import Horizontal, VerticalScroll, Container
from textual.widgets import Button, Input, Markdown, RadioSet, RadioButton, TextArea, RichLog, Log
from textual.events import Print
import importlib
from rich.syntax import Syntax
from textual import work
class TextAreaDialog(App):
CSS = """
Button {
width: 100%;
height: auto;
dock: bottom;
}
.message {
margin: 1 0;
text-style: bold;
min-height: 2;
}
.header {
margin: 2 2 2 2;
text-style: bold;
}
"""
def __init__(self, message='Place *your* message here, markdown mode *on*', title='Title', ok_button_text='OK', default_value='', theme='vscode_dark', language='markdown'):
super().__init__()
self.message = message
self.title = title
self.ok_button_text = ok_button_text
self.default_value = default_value
self.language = language
self.theme = theme
def compose(self):
"""Create child widgets for the app."""
yield Header(self.title)
yield Footer()
yield VerticalScroll(
Markdown(self.message, classes="message"),
TextArea(self.default_value, language=self.language, theme=self.theme, id='input_area'),
Button(self.ok_button_text,variant="primary", id='confirm')
)
def on_button_pressed(self, event: Button.Pressed) -> None:
self.exit(self.query_one("#input_area").text)
class InputDialog(App):
CSS = """
Button {
width: 100%;
height: auto;
dock: bottom;
}
.message {
margin: 1 0;
text-style: bold;
min-height: 2;
}
.header {
margin: 2 2 2 2;
text-style: bold;
}
"""
def __init__(self, message='Place *your* message here, markdown mode *on*', title='Title', place_holder='Inserisci il valore qui..', ok_button_text='OK', default_value=''):
super().__init__()
self.message = message
self.title = title
self.ok_button_text = ok_button_text
self.place_holder = place_holder
self.default_value = default_value
def compose(self):
"""Create child widgets for the app."""
yield Header(self.title)
yield Footer()
yield VerticalScroll(
Markdown(self.message, classes="message"),
Input(
placeholder=self.place_holder, id='input_box', value=self.default_value,
),
Button(self.ok_button_text,variant="primary", id='confirm')
)
def on_button_pressed(self, event: Button.Pressed) -> None:
self.exit(self.query_one("#input_box").value)
class MessageDialog(App):
CSS = """
Button {
width: 100%;
height: auto;
dock: bottom;
}
.message {
margin: 1 0;
text-style: bold;
height: 100%;
}
.header {
margin: 2 2 2 2;
text-style: bold;
}
"""
def __init__(self, message='Place *your* message here, markdown mode *on*', title='Title', ok_button_text='OK'):
super().__init__()
self.message = message
self.title = title
self.ok_button_text = ok_button_text
def compose(self):
"""Create child widgets for the app."""
yield Header(self.title)
yield Footer()
yield Container(
Markdown(self.message, classes="message"),
Button(self.ok_button_text,variant="primary", id='confirm')
)
def on_button_pressed(self, event: Button.Pressed) -> None:
# self.exit(str(event.button))
self.exit(True)
class RadioDialog(App):
CSS = """
Button {
width: 100%;
height: auto;
dock: bottom;
}
.message {
margin: 1 0;
text-style: bold;
min-height: 2;
}
RadioSet {
width: 100%;
}
RadioButton {
margin: 1;
}
.header {
margin: 2 2 2 2;
text-style: bold;
}
"""
def __init__(self, message='Place *your* message here, markdown mode *on*', title='Title', ok_button_text='OK', options = [("opzione1","opzione1"), ("opzione2","opzione2"), ("opzione3","opzione3")]):
super().__init__()
self.message = message
self.title = title
self.ok_button_text = ok_button_text
self.options = options
def compose(self):
"""Create child widgets for the app."""
yield Header(self.title)
yield Footer()
yield VerticalScroll(
Markdown(self.message, classes='message'),
RadioSet(
*[ RadioButton(desc, id=f"{val}") for desc, val in self.options ],
id='radioset'),
Button(self.ok_button_text,variant='primary', id='confirm')
)
def on_button_pressed(self, event: Button.Pressed) -> None:
self.exit(self.query_one("#radioset").pressed_button.id)
class YesNoDialog(App):
CSS = """
.header {
margin: 2 2 2 2;
text-style: bold;
}
.message {
margin: 1 0;
width: 100%;
column-span: 2;
content-align: center middle;
}
.buttons {
width: 100%;
height: auto;
dock: bottom;
}
Button {
width: 50%;
}
"""
def __init__(self, message='Place *your* message here, markdown mode *on*', title='Title', yes_button_text='OK', no_button_text='NO'):
super().__init__()
self.message = message
self.title = title
self.yes_button_text = yes_button_text
self.no_button_text = no_button_text
def compose(self) -> ComposeResult:
yield Header(self.title)
yield Footer()
yield VerticalScroll(
Markdown(self.message, classes="message"),
Horizontal(
Button(self.yes_button_text, id="yes", variant="primary"),
Button(self.no_button_text, id="no", variant="error"),
classes="buttons"),
id = 'container',
)
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == 'yes':
self.exit(True)
elif event.button.id == 'no':
self.exit(False)
class PrintLogDialog(App):
CSS = """
Button {
width: 100%;
height: auto;
dock: bottom;
}
.header {
margin: 2 2 2 2;
text-style: bold;
}
"""
def __init__(self, run_class, title='', run_method='run', data={}, ok_button_text='OK') -> None:
super().__init__()
self.run_class = run_class
self.run_method = run_method
self.data = data
self.ok_button_text = ok_button_text
self.title = title
self.ret_code = None
def compose(self) -> ComposeResult:
yield Header(self.title)
yield Footer()
# yield VerticalScroll(
# Log()
# )
yield Log()
yield Button(self.ok_button_text,variant="primary", id='confirm', disabled=True)
def on_print(self, event: Print) -> None:
self.query_one(Log).write(event.text)
def on_mount(self) -> None:
self.run_my_worker()
@work(thread=True)
def run_my_worker(self):
self.begin_capture_print(self)
module = importlib.import_module(self.run_class)
func = getattr(module, self.run_method)
if self.data == {}:
self.ret_code = func()
else:
self.ret_code = func(self.data)
self.query_one("#confirm").disabled = False
def on_button_pressed(self, event: Button.Pressed) -> None:
# self.exit(str(event.button))
self.exit(self.ret_code)
class PrintRichlogDialog(App):
CSS = """
Button {
width: 100%;
height: auto;
dock: bottom;
}
.header {
margin: 2 2 2 2;
text-style: bold;
}
"""
def __init__(self, run_class, title='', syntax='', run_method='run', data={}, ok_button_text='OK') -> None:
super().__init__()
self.run_class = run_class
self.run_method = run_method
self.data = data
self.ok_button_text = ok_button_text
self.title = title
self.ret_code = None
self.syntax = syntax
def compose(self) -> ComposeResult:
yield Header(self.title)
yield Footer()
yield VerticalScroll(
RichLog(highlight=True, markup=True)
)
yield Button(self.ok_button_text,variant="primary", id='confirm', disabled=True)
def on_print(self, event: Print) -> None:
if self.syntax == '':
self.query_one(RichLog).write(event.text)
else:
self.query_one(RichLog).write(Syntax(event.text, self.syntax))
def on_mount(self) -> None:
self.run_my_worker()
@work(thread=True)
def run_my_worker(self):
self.begin_capture_print(self)
module = importlib.import_module(self.run_class)
func = getattr(module, self.run_method)
if self.data == {}:
self.ret_code = func()
else:
self.ret_code = func(self.data)
self.query_one("#confirm").disabled = False
def on_button_pressed(self, event: Button.Pressed) -> None:
# self.exit(str(event.button))
self.exit(self.ret_code)
if __name__ == "__main__":
"""
ret = RadioDialog().run()
print(ret)
ret = YesNoDialog().run()
print(ret)
ret = MessageDialog().run()
print(ret)
ret = InputDialog(default_value='valore di default').run()
print(ret)
"""
MESSAGE_DESCRIPTION = f"""
# message row 1
## message row 2
message row 3
message row 4
"""
#print(ret)