-
Notifications
You must be signed in to change notification settings - Fork 0
/
cwindows.py
329 lines (262 loc) · 7.06 KB
/
cwindows.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
import os;
import curses;
import curses.textpad;
import code;
import sys;
import traceback;
from codeop import CommandCompiler, compile_command
class CursesStack:
def __init__(self, stdscr, start_window):
self.screen=stdscr
self.lines=curses.LINES
self.cols=curses.COLS
self.fullwin=stdscr
start_window.setWindow(stdscr)
self.sub_windows=[start_window]
self.current_window=0
self.changeActive(0)
self.logger=start_window
def update(self, state):
self.fullwin.erase()
curses.update_lines_cols()
if(self.lines!=curses.LINES or self.cols!=curses.COLS):
for sub in self.sub_windows:
sub.resize()
i=0;
while(i<len(self.sub_windows)):
self.sub_windows[(self.current_window+1+i) %len(self.sub_windows)].update(state)
i=i+1
def handleKey(self, key, state):
if(key=="KEY_F(2)"):
self.addWindow(ReplWindow(40, 40, 40, 10))
return True
if(key=="\t"):
self.changeActive(self.current_window+1);
return True
if(key=="KEY_BTAB"):
self.changeActive(self.current_window-1);
return True
return self.sub_windows[self.current_window].handleKey(key, state)
def addWindow(self, window):
window.setWindow(self.fullwin)
self.sub_windows.append(window)
def changeActive(self, to):
self.sub_windows[self.current_window].setActive(False)
if(to==-1):
self.current_window=to+len(self.sub_windows)
else:
self.current_window=to%len(self.sub_windows)
self.sub_windows[self.current_window].setActive(True)
def logError(self, message):
self.logger.log("Error: "+message)
class StackSubwindow():
def __init__(self, x, y, width, height):
self.bx=x
self.by=y
self.bheight=height
self.bwidth=width
self.vScroll=0
self.hScroll=0
self.content=[]
self.active=False
self.resize()
def resize(self):
self.x=self.bx
self.y=self.by
self.height=self.bheight
self.width=self.bwidth
if(self.x<0):
self.x=curses.COLS+self.x
if(self.y<0):
self.y=curses.LINES+self.y
if(self.height<0):
self.height=curses.LINES-self.y+self.height-1
if(self.width<0):
self.width=curses.COLS-self.x+self.width-1
if(self.x>curses.COLS):
self.x=curses.COLS-3
if(self.y>curses.LINES):
self.y=curses.LINES-3
if(self.x+self.width>curses.COLS):
self.width=curses.COLS-self.x-2
if(self.y+self.height>curses.LINES):
self.height=curses.LINES-self.y-2
def update(self, state):
if(self.active):
self.window.attron(curses.color_pair(2))
curses.textpad.rectangle(self.window, self.y, self.x, self.height+self.y, self.width+self.x)
if(self.active):
self.window.attron(curses.color_pair(1))
self.content=self.getContent(state)
if(self.vScroll==-1):
self.vScroll=max(0, len(self.content)-self.height+1)
i=0
for line in self.content[self.vScroll:self.vScroll+self.height-1]:
self.mvaddstr(self.x+1, self.y+1+i, line[self.hScroll:self.hScroll+self.width-2])
i+=1
def setActive(self, to):
self.active=to
def scrollTop(self):
self.vScroll=0
def scrollBottom(self):
self.vScroll=-1
def scrollUp(self):
self.vScroll=max(0, self.vScroll-1)
def scrollDown(self):
self.vScroll+=1
if(self.vScroll+self.height>len(self.content)):
self.scrollBottom()
def scrollLeft(self):
self.hScroll=max(0, self.hScroll-1)
def scrollRight(self):
self.hScroll+=1
def getContent(self, state):
return []
def handleKey(self, key, state):
if(key=="KEY_UP"):
self.scrollUp()
return True
if(key=="KEY_DOWN"):
self.scrollDown();
return True
if(key=="KEY_HOME"):
self.scrollTop();
return True
if(key=="KEY_END"):
self.scrollBottom();
return True
if(key=="KEY_LEFT"):
self.scrollLeft();
return True
if(key=="KEY_RIGHT"):
self.scrollRight();
return True
return False
def addstr(self, data):
self.window.addstr(data)
def mvaddstr(self, x, y, data):
self.window.addstr(y, x, data)
def setWindow(self, win):
self.window=win
class SimpleWindow(StackSubwindow):
def __init__(self, x, y, width, height, cb):
super().__init__(x, y, width, height)
self.cb=cb
def getContent(self, state):
return self.cb(state)
class ReplWindow(StackSubwindow):
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height)
self.input=[""]
self.logBuffer=[]
def getContent(self, state):
s=[]
s.extend(self.logBuffer)
s.append("> "+self.input[0])
s.extend(self.input[1:])
return s
def log(self, data):
for line in data.split("\n"):
self.logBuffer.append(line)
self.scrollBottom()
def displayHook(self):
return lambda v : self.log(repr(v))
def handleKey(self, key, state):
if(key=="\n"):
inp="\n".join(self.input)
try:
comp=code.compile_command(inp, "<input>", "single")#TODO partial lines
if(comp==None):
self.input.append("")
self.scrollBottom()
return True
else:
self.log("> "+inp)
self.input=[""]
except (OverflowError, SyntaxError, ValueError):
self.log("> "+inp)
type, value, tb = sys.exc_info()
lines = traceback.format_exception_only(type, value)
self.log(''.join(lines))
self.scrollBottom()
self.input=[""]
return True
try:
oldhook=sys.displayhook
sys.displayhook=self.displayHook()
exec(comp, None, state)
sys.displayhook=oldhook
except SystemExit:
raise
except:
type, value, tb = sys.exc_info()
lines = traceback.format_exception(type, value, tb)
self.log(''.join(lines))
elif(key[0:3]=="KEY"):
if(key=="KEY_BACKSPACE"):
self.input[-1]=self.input[-1][0:-1]
else:
return False
else:
self.input[-1]+=key
self.scrollBottom()
return True
class TableWindow(StackSubwindow):
def __init__(self, x, y, width, height, tablegen):
super().__init__(x, y, width, height)
self.tablegen=tablegen
def getContent(self, state):
table=self.tablegen(state)
if(table==None or len(table)==0):
return []
res=[""]
keys=[]
for ele in table:
line=""
ekeys=ele.keys()
for ekey in ekeys:
if(not ekey in keys):
keys.append(ekey)
for key in keys:
try:
line+=str(ele[key])+"\t"
except KeyError:
line+="N/A\t"
res.append(line)
line=""
for key in keys:
line+=str(key)+"\t"
res[0]=line
return res
class FormattedTableWindow(StackSubwindow):
def __init__(self, x, y, width, height, tablegen, fm):
super().__init__(x, y, width, height)
self.tablegen=tablegen
self.format=fm
for fmt in self.format:
if(not "filter" in fmt.keys()):
fmt["filter"]=lambda x : x
def getContent(self, state):
table=self.tablegen(state)
if(table==None or len(table)==0):
return []
header=""
for fmt in self.format:
if(len(fmt['header'])>fmt['width']):
header+=fmt['header'][0:fmt['width']]
else:
header+=fmt['header']+(" "*(fmt['width']-len(fmt['header'])))
res=[header]
for ele in table:
line=""
for fmt in self.format:
try:
val=str(fmt['filter'](ele[fmt["key"]]))
if(len(val)>fmt['width']):
line+=val[0:fmt['width']]
else:
line+=val+" "*(fmt['width']-len(val))
except KeyError:
line+=" "*fmt['width']
res.append(line)
return res