-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurseshelpers.py
504 lines (475 loc) · 18.4 KB
/
curseshelpers.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
from __future__ import division
import curses
import time
import math
import logging
import logging.handlers
import multiprocessing
import threading
import sys
import traceback
import Queue
import re
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
def test():
logger.warning("test")
def default(x, y):
if x is not None: return x
return y
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def bicolor(screen, row, col, splitter, color0, color1, text):
items = re.split(splitter, text)
colors = [color0, color1]
currentcol = col
for item, i in zip(items,range(len(items))):
screen.addstr(row, currentcol, item, colors[i%2])
currentcol+=len(item)
class BaseElement:
def refresh(self):
pass
def goLeft(self):
pass
def goRight(self):
pass
def goUp(self):
pass
def goDown(self):
pass
def pageUp(self):
pass
def pageDown(self):
pass
def home(self):
pass
def end(self):
pass
class TabbedText(BaseElement):
def __init__(self, screen, maxrows=20000, top=0, left=0, height=None, width=None):
self.parent = screen
self.top, self.left = top, left
self._height = default(height, screen.getmaxyx()[0]-top)
self._width = default(width, screen.getmaxyx()[1])
self.heading = curses.newwin(2, self.width, top, left)
self.pads = []
self.positions = []
self.text = []
self.activeCard = 0
self._nrows = []
self.maxrows = maxrows
def clear(self):
self.text = []
self.pads = []
self._redraw()
def addFile(self, title, filename):
f = open(filename, 'r')
self.addText(title, f.read())
f.close()
def _countLines(self, text):
lines=0
for line in text.splitlines():
lines += 1+(len(line ) // self.width)
return lines
def addText(self, title, text):
self.text.append( (title, text) )
newpad = curses.newpad(min(self.maxrows, max(self.height, self._countLines(text))), self.width)
self.pads.append(newpad)
self.positions.append(0)
self._redraw()
def refresh(self):
self._redrawHeading()
self.heading.refresh()
self.pads[self.activeCard].refresh(self.positions[self.activeCard], 0, self.top + 2, self.left, self.top + 2 + self.height - 1, self.left + self.width - 1)
def goLeft(self):
self.activeCard = (self.activeCard - 1) % len(self.text)
def goRight(self):
self.activeCard = (self.activeCard + 1) % len(self.text)
def goUp(self):
self.positions[self.activeCard]=max(0, self.positions[self.activeCard] - 1)
def goDown(self):
self.positions[self.activeCard]=min(self.positions[self.activeCard] + 1, self.nrows - self.height)
def pageUp(self):
self.positions[self.activeCard]=max(self.positions[self.activeCard] - self.height, 0)
def pageDown(self):
self.positions[self.activeCard]=min(self.positions[self.activeCard] + self.height, self.nrows - self.height)
def home(self):
self.positions[self.activeCard] = 0
def end(self):
self.positions[self.activeCard] = self.nrows-self.height
def _redrawHeading(self):
self.heading.clear()
ncards = len(self.text)
cardSpacing = 1
cardWidth = (self.width-1) // ncards - 2 * (cardSpacing + 1)
cardid = 0
for (title, text) in self.text:
self.heading.addstr(0, ((cardSpacing+1)*2+cardWidth)*cardid, "{0}{1}{0}".format((1+cardSpacing)*" ",cardWidth*"_"))
self.heading.addstr(1, ((cardSpacing+1)*2+cardWidth)*cardid, ("{0}/{1:^"+str(cardWidth)+"."+str(cardWidth)+"}\{0}").format(cardSpacing*"_",title))
if cardid==self.activeCard:
self.heading.addstr(1, ((cardSpacing+1)*2+cardWidth)*cardid+cardSpacing+1, ("{0:^"+str(cardWidth)+"."+str(cardWidth)+"}").format(title), curses.A_REVERSE)
cardid += 1
def _redraw(self):
self._redrawHeading()
textid=0
self._nrows=[]
for (title, text) in self.text:
rownumber=0
for line in text.splitlines():
rows=list(chunks(line,self.width))
for row in rows:
try:
self.pads[textid].addstr(rownumber, 0, row)
except curses.error:
self.pads[textid].addstr(rownumber-1, 0, "Maximum rows reached, please use a different viewer for more lines ",curses.A_REVERSE)
break
rownumber+=1
self._nrows.append(rownumber)
textid+=1
@property
def height(self):
return min(self._height,self.parent.getmaxyx()[0]-self.top-3)
@property
def width(self):
return min(self._width, self.parent.getmaxyx()[1])
@property
def nrows(self):
return self._nrows[self.activeCard]
class BottomText(BaseElement):
def __init__(self, screen, top=1, left=0, height=None, width=None):
self.parent = screen
self.top, self.left = top, left
self._height = default(height, screen.getmaxyx()[0]-top)
self._width = default(width, screen.getmaxyx()[1])
self.pad = curses.newwin(self.height, self.width, top, left)
self.text = []
def clear(self):
self.pad.clear()
self.text = []
def addText(self, text):
self.text.append(text)
self.text = self.text[-100:]
self._redraw()
def refresh(self):
self.pad.refresh()
def _redraw(self):
self.pad.clear()
rownumber=0
rows=[]
for text in self.text:
for line in text.splitlines():
rows+=list(chunks(line,self.width))
rownumber=self.height-1
for row in reversed(rows):
if rownumber==-1: break
try:
self.pad.addstr(rownumber, 0, row)
except:
raise Exception(str(self.top+rownumber)+" "+str(self.top)+" "+str(self.height))
rownumber-=1
@property
def height(self):
return min(self._height,self.parent.getmaxyx()[0]-self.top-1)
@property
def width(self):
return min(self._width, self.parent.getmaxyx()[1])
class MultiText(BaseElement):
def __init__(self, screen, maxrows=20000, top=1, left=0, height=None, width=None):
self.parent = screen
self.top, self.left = top, left
self._height = default(height, screen.getmaxyx()[0]-top)
self._width = default(width, screen.getmaxyx()[1])
self.pad = curses.newpad(maxrows, self.width)
self.position = 0
self.text = []
self.nrows=0
def clear(self):
self.pad.clear()
self.text = []
def addFile(self, title, filename):
f = open(filename, 'r')
self.addText(title, f.read())
f.close()
def addText(self, title, text):
self.text.append( (title, text) )
self._redraw()
def refresh(self):
self.pad.refresh(self.position, 0, self.top, self.left, self.top+self.height-1, self.left+self.width-1)
def goUp(self):
self.position=max(0, self.position-1)
def goDown(self):
self.position=min(self.position+1, self.nrows-self.height)
def pageUp(self):
self.position=max(self.position-self.height, 0)
def pageDown(self):
self.position=min(self.position+self.height, self.nrows-self.height)
def _redraw(self):
rownumber=0
for (title, text) in self.text:
self.pad.addstr(rownumber, 0, ("{0:^"+str(self.width)+"."+str(self.width-1)+"}").format(title), curses.A_UNDERLINE)
rownumber+=1
rows=[]
for line in text.splitlines():
rows+=list(chunks(line,self.width))
for row in rows:
try:
self.pad.addstr(rownumber, 0, row)
except:
raise Exception(str(rownumber))
rownumber+=1
rownumber+=2
rownumber-=2
self.nrows=rownumber
def home(self):
self.position = 0
def end(self):
self.position = self.nrows-self.height
@property
def height(self):
return min(self._height,self.parent.getmaxyx()[0]-self.top-1)
@property
def width(self):
return min(self._width, self.parent.getmaxyx()[1])
class Text(BaseElement):
def __init__(self, screen, maxrows=2000, top=1, left=0, height=None, width=None):
self.parent = screen
self.top, self.left = top, left
self._height = default(height, screen.getmaxyx()[0]-top)
self._width = default(width, screen.getmaxyx()[1])
self.pad = curses.newpad(maxrows, self.width)
self.position = 0
self.text = ""
self.nrows=0
self.pad.scrollok(True)
def clear(self):
self.setText("")
def readFile(self, filename):
f = open(filename, 'r')
self.setText(f.read())
f.close()
def setText(self, text):
self.text=text
self._redraw()
def refresh(self):
self.pad.refresh(self.position, 0, self.top, self.left, self.top+self.height-1, self.left+self.width-1)
def goUp(self):
self.position=max(0, self.position-1)
def goDown(self):
self.position=min(self.position+1, self.nrows-self.height)
def pageUp(self):
self.position=max(self.position-self.height, 0)
def pageDown(self):
self.position=min(self.position+self.height, self.nrows-self.height)
def _redraw(self):
rows=[]
for line in self.text.splitlines():
rows+=list(chunks(line,self.width))
for (row, rownumber) in zip(rows, range(len(rows))):
self.pad.addstr(rownumber, 0, row)
self.nrows=len(rows)
def home(self):
self.position = 0
def end(self):
self.position = self.nrows-self.height
@property
def height(self):
return min(self._height,self.parent.getmaxyx()[0]-self.top-1)
@property
def width(self):
return min(self._width, self.parent.getmaxyx()[1])
def colWidthsReducerMaximum(colWidths, totalWidth):
# reduces the maximum width by 1 as long as the desired total width is reached
while sum(colWidths) > totalWidth:
colWidths[max(xrange(len(colWidths)),key=colWidths.__getitem__)]-=1
return colWidths
class SelectTable(BaseElement):
"""A Table where a single row can be selected
"""
def __init__(self, screen, maxrows=1000, top=1, left=0, height=None, width=None, footer=False):
if footer:
nfooterlines = 1
else:
nfooterlines = 0
self.parent = screen
self.top, self.left = top+1, left
self._height = default(height, screen.getmaxyx()[0]-top)-1-nfooterlines
self._width = default(width, screen.getmaxyx()[1])
self.pad = curses.newpad(maxrows, self.width)
self.header = curses.newwin(1, self.width, top, left)
self.cursor, self.position = 0, 0
self.rows, self.formats, self.keys = [], [], []
self.colFooters = None
def setColHeaders(self, headers, colwidths=None):
self.colHeaders = headers
if colwidths is None:
x=int(math.floor((self.width-1)/len(self.colHeaders)))
self.colWidths = [x]*len(self.colHeaders)
else:
self.colWidths = colWidthsReducerMaximum(colwidths, self.width-1)
self._drawColHeaders()
def setFooters(self, footers):
self.colFooters = footers
self.footer = curses.newwin(1, self.width, self.top+self.height, self.left)
self._drawFooters()
def _drawFooters(self):
if self.colFooters is not None:
for cell, i in zip(self.colFooters, range(len(self.colFooters))):
if isinstance(cell, (int, long, float, complex)):
direction=">"
else:
direction="<"
self.footer.addstr(0, sum(self.colWidths[:i]), ("{0:"+direction+str(self.colWidths[i]-1)+"."+str(self.colWidths[i]-1)+"} ").format(str(cell)))
def _drawColHeaders(self):
for i in range(len(self.colHeaders)):
self.header.addstr(0, sum(self.colWidths[:i]), ("{0:^"+str(self.colWidths[i])+"."+str(self.colWidths[i]-1)+"}").format(self.colHeaders[i]), curses.A_UNDERLINE)
if self.width-sum(self.colWidths)-1 > 0:
self.header.addstr(0, sum(self.colWidths), " "*(self.width-sum(self.colWidths)-1), curses.A_UNDERLINE)
def addRow(self, row, formatting=None, key=None):
self.rows.append(row)
self.formats.append(default(formatting,curses.A_NORMAL))
self.keys.append(default(key, self.nrows))
self._redrawRows(self.nrows-1)
def clear(self):
self.rows, self.formats, self.keys = [], [], []
self.pad.clear()
self.refresh()
def refresh(self):
self.pad.refresh(self.position, 0, self.top, self.left, self.top+self.height-1, self.left+self.width-1)
self._drawColHeaders()
self.header.refresh()
if self.colFooters is not None:
self._drawFooters()
self.footer.refresh()
def goUp(self):
oldcursor = self.cursor
self.cursor=max(self.cursor-1, 0)
self._redrawRows(self.cursor, oldcursor)
if self.cursor-self.position<1/4*self.height and self.position>0:
self.position -= 1
def goDown(self):
oldcursor = self.cursor
self.cursor=min(self.cursor+1, self.nrows-1)
self._redrawRows(self.cursor, oldcursor)
#if self.position<self.cursor-2/3*self.height and self.position+self.height<self.nrows:
if self.cursor-self.position>3/4*self.height and self.position+self.height<self.nrows:
self.position += 1
def _redrawRows(self, *rowids):
if len(self.rows)==0:
#nothing to display
return
for rownumber in rowids:
if rownumber==self.cursor:
formatting = self.formats[rownumber] | curses.A_REVERSE
else:
formatting=self.formats[rownumber]
for cell, i in zip(self.rows[rownumber], range(len(self.rows[rownumber]))):
if isinstance(cell, (int, long, float, complex)):
direction=">"
else:
direction="<"
#self.pad.addstr(rownumber, sum(self.colWidths[:i]), str(cell)[0:self.colWidths[i]-1], formatting)
try:
self.pad.addstr(rownumber, sum(self.colWidths[:i]), ("{0:"+direction+str(self.colWidths[i]-1)+"."+str(self.colWidths[i]-1)+"} ").format(str(cell)), formatting)
except:
#print ("{0:"+direction+str(self.colWidths[i]-1)+"."+str(self.colWidths[i]-1)+"} ").format(str(cell))
pass
try:
self.pad.addstr(rownumber, sum(self.colWidths), " "*(self.width-sum(self.colWidths)-1), formatting)
except:
pass
@property
def nrows(self):
return len(self.rows)
def pageUp(self):
oldcursor = self.cursor
self.cursor=max(self.cursor-self.height, 0)
self.position=max(self.position-self.height, 0)
self._redrawRows(self.cursor, oldcursor)
def pageDown(self):
oldcursor = self.cursor
self.cursor=min(self.cursor+self.height, self.nrows-1)
self.position=min(self.position+self.height, self.nrows-self.height)
self._redrawRows(self.cursor, oldcursor)
def home(self):
oldcursor = self.cursor
self.position, self.cursor = 0, 0
self._redrawRows(self.cursor, oldcursor)
def end(self):
oldcursor = self.cursor
self.position, self.cursor = self.nrows-self.height, self.nrows-1
self._redrawRows(self.cursor, oldcursor)
@property
def selectedRow(self):
return self.keys[self.cursor]
@property
def height(self):
return min(self._height,self.parent.getmaxyx()[0]-self.top-1)
@property
def width(self):
return min(self._width, self.parent.getmaxyx()[1])
class CursesHandler(logging.Handler):
def __init__(self, stdscr,bottomText, level=logging.DEBUG):
logging.Handler.__init__(self, level)
self.bottomText = bottomText
self.stdscr = stdscr
def emit(self, record):
attr = {"DEBUG":curses.A_NORMAL,
"INFO":curses.A_NORMAL,
"WARNING":curses.A_BOLD,
"ERROR":curses.A_BOLD,
"CRITICAL":curses.A_STANDOUT}
self.bottomText.addText(self.format(record)+"\n")
# http://stackoverflow.com/questions/641420/how-should-i-log-while-using-multiprocessing-in-python
class CursesMultiHandler(logging.Handler):
def __init__(self, stdscr,bottomText, log_q,level=logging.DEBUG):
logging.Handler.__init__(self)
self._handler = CursesHandler(stdscr , bottomText, level)
#~ self.queue = multiprocessing.Queue(-1)
self.queue = log_q
t = threading.Thread(target=self.receive)
t.daemon = True
t.start()
def setFormatter(self, fmt):
logging.Handler.setFormatter(self, fmt)
self._handler.setFormatter(fmt)
def receive(self):
while True:
try:
record = self.queue.get()
self._handler.emit(record)
except (KeyboardInterrupt, SystemExit):
raise
except Queue.Empty:
pass
except EOFError:
break
except:
traceback.print_exc(file=sys.stderr)
def send(self, s):
self.queue.put_nowait(s)
def _format_record(self, record):
# ensure that exc_info and args
# have been stringified. Removes any chance of
# unpickleable things inside and possibly reduces
# message size sent over the pipe
if record.args:
record.msg = record.msg % record.args
record.args = None
if record.exc_info:
dummy = self.format(record)
record.exc_info = None
return record
def emit(self, record):
try:
s = self._format_record(record)
self.send(s)
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
def close(self):
self._handler.close()
logging.Handler.close(self)