-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagertest.py
76 lines (62 loc) · 1.85 KB
/
pagertest.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
"""
A simple 'test' program that runs urwid and outputs key presses. When 'p' is
pressed, urwid is stopped, a pager is started, and when it exits, urwid
is resumed where it left off.
"""
import sys, subprocess
from subprocess import PIPE
import urwid
import pygments
from pygments import lexers, formatters, styles
l = lexers.get_lexer_by_name('python')
snames = styles.get_all_styles()
fs = [formatters.get_formatter_by_name('terminal256',style=s) for s in snames]
t = """def func(arg, key=val):
try:
print 4/0
except Exception:
return 2+2
"""
processedtxt = ''.join(pygments.highlight(t, l, f) for f in fs)
def run_pager(txt):
proc = subprocess.Popen(['less', '-Rc'], stdin=PIPE)
#proc = subprocess.Popen(['more', '-c'], stdin=PIPE)
proc.communicate(txt)
class MainLoop(urwid.MainLoop):
def unhandled_input(self, input):
if input == 'q':
raise urwid.ExitMainLoop()
elif input == 'p':
self.screen.stop()
#run_pager('This is the pager!')
run_pager(processedtxt)
self.screen.start()
else:
self.txtwidget.set_text(input)
self.draw_screen()
return True
def main():
#print "Running..."
#run_pager(t)
#print "Ran."
#return
s = urwid.raw_display.Screen()
txtwidget = urwid.Text('Test!')
widget = urwid.Filler(txtwidget)
def handler(inpt):
if inpt == 'q':
raise urwid.ExitMainLoop()
elif inpt == 'p':
s.stop()
#run_pager('This is the pager!')
run_pager(processedtxt)
s.start()
else:
txtwidget.set_text(inpt)
s.draw_screen()
return True
loop = MainLoop(widget, screen=s)
loop.txtwidget = txtwidget
loop.run()
if __name__ == '__main__':
main()