-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLIElem.py
465 lines (393 loc) · 13.4 KB
/
CLIElem.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# -*- coding: UTF-8 -*-
# 实现界面元素的相关类;
# 模块内应当有一个Elem的基类,和若干基于此的派生,用于实现界面元素的处理,
# 每个类应当实现对输入数据流的接收、响应、自我更新,提供绘制接口,返回用于绘制的字符串流;
# 实现内容如,一个响应键入操作,记录键入内容,提供绘制的文本框;
from ..OLDLog import logger;
from .CONSTS import *;
from .CLIDraw import *;
__all__ = [
"Elem",
"BSElem",
"ElemLabel",
"ElemCmage",
];
class Elem:
"""
An elem is an element on a page, a scene, an UI, etc;
"""
def __init__(self, y: int = 1, x: int = 1, h: int = 0, w: int = 0, value: any = None) -> None:
"""
Init to the elem;
y, x: relative coordinate of this elem;
h, w: size of this elem;
"""
self._rect = (y, x, h, w);
self._val = value;
return;
@property
def rect(self) -> tuple:
"""
The relative coordinate of this elem;
"""
return self._rect;
@rect.setter
def rect(self, val:tuple) -> None:
self._rect = val;
return;
@property
def y(self) -> tuple:
"""
The relative coordinate y of this elem;
"""
return self._rect[0];
@y.setter
def y(self, val:int) -> None:
self.rect = (val, self._rect[1], self._rect[2], self._rect[3]);
return;
@property
def x(self) -> tuple:
"""
The relative coordinate x of this elem;
"""
return self._rect[1];
@x.setter
def x(self, val:int) -> None:
self.rect = (self._rect[0], val, self._rect[2], self._rect[3]);
return;
@property
def h(self) -> tuple:
"""
The height of this elem;
"""
return self._rect[2];
@h.setter
def h(self, val:int) -> None:
self.rect = (self._rect[0], self._rect[1], val, self._rect[3]);
return;
@property
def w(self) -> tuple:
"""
The width of this elem;
"""
return self._rect[3];
@w.setter
def w(self, val:int) -> None:
self.rect = (self._rect[0], self._rect[1], self._rect[2], val);
return;
@property
def y0(self) -> int:
"""
The relative coordinate x0 of this elem;
"""
return self._rect[0];
@y0.setter
def y0(self, val:int) -> None:
self.rect = (val, self._rect[1], self._rect[2], self._rect[3]);
return;
@property
def x0(self) -> int:
"""
The relative coordinate x0 of this elem;
"""
return self._rect[1];
@x0.setter
def x0(self, val:int) -> None:
self.rect = (self._rect[0], val, self._rect[2], self._rect[3]);
return;
@property
def y1(self) -> tuple:
"""
The relative coordinate y1 of this elem;
"""
return self._rect[0] + self._rect[2] - 1;
@y1.setter
def y1(self, val:int) -> None:
if val >= self._rect[0]:
self.rect = (self._rect[0], self._rect[1], val - self._rect[0] + 1, self._rect[3]);
return;
@property
def x1(self) -> tuple:
"""
The relative coordinate x1 of this elem;
"""
return self._rect[1] + self._rect[3] - 1;
@x1.setter
def x1(self, val:int) -> None:
if val >= self._rect[1]:
self.rect = (self._rect[0], self._rect[1], self._rect[2], val - self._rect[1] + 1);
return;
@property
def value(self) -> any:
"""
The value of this elem;
"""
return self._val;
@value.setter
def value(self, val:any) -> None:
self._val = val;
return;
def on_act(self, sender) -> None:
"""
Callback to the action of the elem;
"""
return;
def update(self, input:str) -> None:
"""
Called to transmit the control flow to the elem;
input: control flow to the elem;
return none;
"""
return;
def draw(self, y: int = 1, x: int = 1, h: int = 0, w: int = 0, f: bool = False) -> str:
"""
Called to get the draw flow of the elem;
y, x: absolute coordinate to draw of this elem;
h, w: boundary to draw of this elem;
f: if this elem is focused;
return the string to draw the elem;
"""
return '';
class BSElem(Elem):
def __init__(self, y: int = 1, x: int = 1, h: int = 0, w: int = 0, value: any = None, boxstyle: str = '', colorstyle: dict = STYLE_CLASSIC) -> None:
self._Elem = super();
self._Elem.__init__(y = y, x = x, h = h, w = w, value = value);
self._boxstyle = boxstyle;
self._colstyle = colorstyle;
self._boxstyle_cc = '';
self._boxstyle_ch = '';
self._boxstyle_cv = '';
self._update_style();
def _update_style(self):
if self._boxstyle == '-':
self._boxstyle_cc = '+';
self._boxstyle_ch = '-';
self._boxstyle_cv = '|';
elif self._boxstyle == '=':
self._boxstyle_cc = '#';
self._boxstyle_ch = '=';
self._boxstyle_cv = '|';
elif self._boxstyle == ' ':
self._boxstyle_cc = ' ';
self._boxstyle_ch = ' ';
self._boxstyle_cv = ' ';
elif self._boxstyle == '':
self._boxstyle_cc = '';
self._boxstyle_ch = '';
self._boxstyle_cv = '';
return;
@property
def boxstyle(self) -> str:
"""
The box style of this elem;
"""
return self._boxstyle;
@boxstyle.setter
def boxstyle(self, val:str) -> None:
if val in ('-', '=', ' ', ''):
self._boxstyle = val;
self._update_style();
return;
@property
def colorstyle(self) -> dict:
"""
The color style of this elem;
"""
return self._colstyle;
@colorstyle.setter
def colorstyle(self, val:dict) -> None:
self._colstyle = val;
return;
def draw(self, y: int = 1, x: int = 1, h: int = 0, w: int = 0, f: bool = False) -> str:
if self._boxstyle == '':
return '';
elif (
(self.x1 + 1 < 1) or
(self.x0 - 1 > w) or
(self.y1 + 1 < 1) or
(self.y0 - 1 > h)
):
return '';
else:
_evu = 0 if (self.y0 - 1 < 1) else 1;
_evd = 0 if (self.y1 + 1 > h) else 1;
_evc = min(self.y1, w) - max(self.y0, 1) + 1;
_ehl = 0 if (self.x0 - 1 < 1) else 1;
_ehr = 0 if (self.x1 + 1 > w) else 1;
_ehc = min(self.x1, w) - max(self.x0, 1) + 1;
_y = y + max(self.y0 - 1, 1) - 1;
_x = y + max(self.x0 - 1, 1) - 1;
_su = self._boxstyle_cc * _ehl + self._boxstyle_ch * _ehc + self._boxstyle_cc * _ehr;
_sc = self._boxstyle_cv * _ehl + ' ' * _ehc + self._boxstyle_cv * _ehr
_sd = self._boxstyle_cc * _ehl + self._boxstyle_ch * _ehc + self._boxstyle_cc * _ehr;
_ss = [_su] * _evu + [_sc] * _evc + [_sd] * _evd;
_style = self._colstyle["FOCUSED"] if f else self._colstyle["NONACT"];
return putstrs(_y, _x, _ss, *_style);
class ElemLabel(BSElem):
def __init__(self, y: int = 1, x: int = 1, h: int = 1, w: int = 0, value: str = '', align: str = 'l', autoscale: bool = True, boxstyle: str = '', colorstyle: dict = STYLE_CLASSIC) -> None:
self._BSElem = super();
self._BSElem.__init__(y = y, x = x, h = 1, w = w, value = value, boxstyle = boxstyle, colorstyle = colorstyle);
self._rect = (y, x, 1, w);
self._autoscale = autoscale;
if align in ('l', 'c', 'r'):
self._align = align;
else:
self._align = 'l';
self._drawraw = '';
self._update_drawraw();
def _update_drawraw(self):
_s, _ = str_cut_return(self._val);
if self._autoscale:
self._rect = (self._rect[0], self._rect[1], 1, str_width(_s));
self._drawraw = _s;
elif self._align == 'l':
self._drawraw = str_trim_al(_s, self.w);
elif self._align == 'r':
self._drawraw = str_trim_ar(_s, self.w);
elif self._align == 'c':
self._drawraw = str_trim_ac(_s, self.w);
else:
self._drawraw = '';
return;
@property
def rect(self) -> tuple:
"""
The relative coordinate of this elem;
"""
return self._rect;
@rect.setter
def rect(self, val:tuple) -> None:
if self._autoscale:
self._rect = (val[0], val[1], 1, str_width(self._drawraw));
else:
self._rect = (val[0], val[1], 1, val[3]);
self._update_drawraw();
return;
@property
def value(self) -> str:
"""
The string of this elem;
"""
return self._val;
@value.setter
def value(self, val:str) -> None:
self._val = val;
self._update_drawraw();
return;
@property
def align(self) -> str:
"""
The alignment of the string of this elem;
"""
return self._align;
@align.setter
def align(self, val:str) -> None:
if val in ('l', 'c', 'r'):
self._align = val;
self._update_drawraw();
return;
@property
def autoscale(self) -> bool:
return self._autoscale;
@autoscale.setter
def autoscale(self, val:bool) -> None:
self._autoscale = val;
if self._autoscale:
self._update_drawraw();
return;
def draw(self, y:int = 1, x:int = 1, h:int = 0, w:int = 0, f:bool = False) -> str:
_s0 = self._BSElem.draw(y, x, h, w, f);
if (
(self.x1 < 1) or
(self.x0 > w) or
(self.y1 < 1) or
(self.y0 > h)
):
return _s0;
else:
_pl = 0 if (self.x0 >= 1) else (1 - self.x0);
_pr = self.w if (self.x1 <= w) else (w - self.x0 + 1);
_y = y + max(self.y0, 1) - 1;
_x = y + max(self.x0, 1) - 1;
_s = str_trim_at(self._drawraw, _pl, _pr);
_style = self._colstyle["FOCUSED"] if f else self._colstyle["NONACT"];
return _s0 + putstr(_y, _x, _s, *_style);
class ElemCmage(BSElem):
def __init__(self, y: int = 1, x: int = 1, h: int = 1, w: int = 0, value: list = [], autoscale: bool = True, boxstyle: str = '', colorstyle: dict = STYLE_CLASSIC) -> None:
self._BSElem = super();
self._BSElem.__init__(y = y, x = x, h = 1, w = w, value = value, boxstyle = boxstyle, colorstyle = colorstyle);
self._rect = (y, x, 1, w);
self._autoscale = autoscale;
self._drawraws = [];
self._update_drawraws();
def _update_drawraws(self):
if self._autoscale:
_dw = str_width(self._val[0]) if len(self._val) > 0 else 0;
_dh = len(self._val);
self._drawraws = self._val;
self._rect = (self._rect[0], self._rect[1], _dh, _dw);
else:
self._drawraws = [];
for _line in self._val:
_s = str_trim_al(_line, self._rect[3]);
self._drawraws.append(_s);
if len(self._drawraws) >= self._rect[2]:
self._drawraws = self._drawraws[:self._rect[2]];
else:
self._drawraws += [' ' * self._rect[3]] * (self._rect[2] - len(self._drawraws));
return;
@property
def rect(self) -> tuple:
"""
The relative coordinate of this elem;
"""
return self._rect;
@rect.setter
def rect(self, val:tuple) -> None:
if self._autoscale:
_dw = str_width(self._drawraws[0]) if len(self._drawraws) > 0 else 0;
_dh = len(self._drawraws);
self._rect = (val[0], val[1], _dh, _dw);
else:
self._rect = val;
self._update_drawraws();
return;
@property
def value(self) -> str:
"""
The string of this elem;
"""
return self._val;
@value.setter
def value(self, val:str) -> None:
self._val = val;
self._update_drawraws();
return;
@property
def autoscale(self) -> bool:
return self._autoscale;
@autoscale.setter
def autoscale(self, val:bool) -> None:
self._autoscale = val;
if self._autoscale:
self._update_drawraw();
return;
def draw(self, y:int = 1, x:int = 1, h:int = 0, w:int = 0, f:bool = False) -> str:
_s0 = self._BSElem.draw(y, x, h, w, f);
if (
(self.x1 < 1) or
(self.x0 > w) or
(self.y1 < 1) or
(self.y0 > h)
):
return _s0;
else:
_pu = 0 if (self.y1 >= 1) else (1 - self.y0);
_pl = 0 if (self.x0 >= 1) else (1 - self.x0);
_pd = self.h if (self.y1 <= h) else (h - self.y0 + 1);
_pr = self.w if (self.x1 <= w) else (w - self.x0 + 1);
_y = y + max(self.y0, 1) - 1;
_x = y + max(self.x0, 1) - 1;
_ss = [str_trim_at(_line, _pl, _pr) for _line in self._drawraws];
_style = self._colstyle["FOCUSED"] if f else self._colstyle["NONACT"];
return _s0 + putstrs(_y, _x, _ss, *_style);