-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytui.py
More file actions
171 lines (145 loc) · 6.08 KB
/
pytui.py
File metadata and controls
171 lines (145 loc) · 6.08 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
import os
import sys
class Tui:
def _b_place_cursor(self, col:int, row:int):
self._place_cursor_abs(col+self.col_offset, row+self.row_offset)
def _place_cursor_abs(self, col:int, row:int):
self._queue += [f"{self._CSI}{row};{col}H"]
def _place_text(self, text:str, col:int, row:int):
self._b_place_cursor(col, row)
self._queue += [text]
def _split_text_every_nth(self, text:str, n:int) -> list[str]:
return [text[i:i+n] for i in range(0, len(text), n)]
def _split_text_before_every_nth(self, text:str, n:int, ch:str = " ", allow_overflow:bool = True) -> list[str]:
ret = []
t_cols, _ = os.get_terminal_size()
while text != "":
if len(text) < n:
ret += [text]
return ret
idx = text.find(ch)
if idx == -1:
if allow_overflow:
ret += [text]
else:
ret += self._split_text_every_nth(text, n)
return ret
idx = text.rfind(ch,0,max(n,idx+1))
if not allow_overflow:
idx = min(n, idx)
else:
idx = min(t_cols, idx)
cut = text[:idx]
if idx < n:
cut += (n-idx)*' '
ret += [cut]
text = text[idx+1:]
return ret
def _correct_dims(self, col, row, width, height):
t_cols, t_rows = os.get_terminal_size()
width = min(width, min(t_cols,self.max_width) -col-self.col_offset)
height = min(height, min(t_rows,self.max_height)-row-self.row_offset)
return width, height
def _clear_box(self, col, row, width, height, char):
for i in range(height):
self._place_text(char*width, col, row+i)
self._flush()
def clear_box(self, col:int = 0, row:int = 0, width:int = 10000, height: int = 10000, char=' '):
if width == 0 or height == 0:
return
width, height = self._correct_dims(col, row, width, height)
if width <= 0 or height <= 0:
return
col += 1
row += 1
self._clear_box(col,row,width,height,char)
def clear_line(self, col:int = 0, row:int = 0):
self.clear_box(col, row, height=1)
self._flush()
def place_text(self, text:str, col:int = 0, row:int = 0, width:int = 10000, height:int = 10000):
if width == 0 or height == 0:
return
width, height = self._correct_dims(col, row, width, height)
if width <= 0 or height <= 0:
return
col += 1
row += 1
max_len = width*height
trunkated = ''.join(self._split_text_before_every_nth(text, width, allow_overflow=True))
if len(text)>max_len:
post = ''
if max_len >= 10:
post = '...'
t_len = max_len - len(post)
trunkated = text[:t_len] + post
padding = ' '*(max_len-len(trunkated))
for i,text in enumerate(self._split_text_before_every_nth(trunkated + padding, width, allow_overflow=True)):
self._place_text(text, col, row+i)
self._flush()
def place_cursor(self, col:int=0, row:int=0):
col, row = self._correct_dims(0,0,col,row)
self._b_place_cursor(col+1, row+1)
self._flush()
def _flush(self, force:bool=False):
if force or (not self.buffered):
self._queue = [self.get_colour_string(*self.bg_colour, *self.t_colour)] + self._queue
if self.return_on_flush:
self._queue = [f"{self._CSI}s"] + self._queue + [f"{self._CSI}u"]
sys.stdout.write(''.join(self._queue))
sys.stdout.flush()
self._queue = []
def flush(self):
self._flush(True)
def clear(self):
self.clear_box(0,0)
self._flush()
def hide_cursor(self, hide:bool):
self._queue += [f"{self._CSI}?25l" if hide else f"{self._CSI}?25h"]
self._flush()
def get_colour_string(self,bgr,bgg,bgb,tr,tg,tb):
return f"{self._CSI}48;2;{bgr};{bgg};{bgb}m{self._CSI}38;2;{tr};{tg};{tb}m"
def __init__(self,buffered:bool = False, hide_cursor:bool = True, col_offset=0, row_offset=0, max_width=10000, max_height=10000, default_cursor_pos=None, return_on_flush=True, border = '', t_colour =(255,255,255), bg_colour=(0,0,0)):
self._queue = []
self._CSI = '\033['
self.buffered = buffered
self.col_offset = col_offset
self.row_offset = row_offset
self.max_width = max_width + col_offset
self.max_height = max_height + row_offset
self.bg_colour = bg_colour
self.t_colour = t_colour
if default_cursor_pos is not None:
self.return_on_flush = False
self._place_cursor_abs(*default_cursor_pos)
self.flush()
self.return_on_flush = return_on_flush
self.hide_cursor(hide_cursor)
if len(border) > 0:
self.clear_box(char=border)
self.col_offset +=1
self.row_offset +=1
self.max_width -=1
self.max_height -=1
self.clear_box(char=' ')
def test():
import time
tui = Tui()
tui.clear()
tui.place_text("1234567", 3, 3, width = 3, height = 1)
time.sleep(1)
tui.buffered = True
tui.place_text("1234567890123456789", 3, 3, width = 15, height = 1)
time.sleep(1)
tui.place_text("123", 3, 3, width = 10, height = 1)
tui.flush()
tui.buffered = False
tui.place_text("1234567890123456789", 4, 4, width = 2)
tui.place_text("1234567890123456789", 7, 7, width = 3, height = 3)
time.sleep(1)
tui.clear()
[print(x) for x in tui._split_text_before_every_nth("123 567 9", 6, allow_overflow=True)]
[print(x) for x in tui._split_text_before_every_nth("123567 9", 6, allow_overflow=True)]
[print(x) for x in tui._split_text_before_every_nth("1235679", 6, allow_overflow=True)]
[print(x) for x in tui._split_text_before_every_nth("12234566667 12227011 4 17098 01479 047189 704911704 87 01842 70481 70431", 6, allow_overflow=True)]
if __name__ == "__main__":
test()