|
1
|
|
|
#!/usr/bin/python |
|
2
|
|
|
# -*- encoding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
import termbox |
|
5
|
|
|
import time |
|
6
|
|
|
import sys |
|
7
|
|
|
import random |
|
8
|
|
|
#import psyco |
|
9
|
|
|
|
|
10
|
|
|
#psyco.full() |
|
11
|
|
|
|
|
12
|
|
|
spaceord = ord(u" ") |
|
13
|
|
|
|
|
14
|
|
|
def print_line(t, msg, y, fg, bg): |
|
15
|
|
|
w = t.width() |
|
16
|
|
|
l = len(msg) |
|
17
|
|
|
x = 0 |
|
18
|
|
|
for i in range(w): |
|
19
|
|
|
c = spaceord |
|
20
|
|
|
if i < l: |
|
21
|
|
|
c = ord(msg[i]) |
|
22
|
|
|
t.change_cell(x+i, y, c, fg, bg) |
|
23
|
|
|
|
|
24
|
|
|
class SelectBox(object): |
|
25
|
|
|
def __init__(self, tb, choices, active=-1): |
|
26
|
|
|
self.tb = tb |
|
27
|
|
|
self.active = active |
|
28
|
|
|
self.choices = choices |
|
29
|
|
|
self.color_active = (termbox.BLACK, termbox.CYAN) |
|
30
|
|
|
self.color_normal = (termbox.WHITE, termbox.BLACK) |
|
31
|
|
|
|
|
32
|
|
|
def draw(self): |
|
33
|
|
|
for i, c in enumerate(self.choices): |
|
34
|
|
|
color = self.color_normal |
|
35
|
|
|
if i == self.active: |
|
36
|
|
|
color = self.color_active |
|
37
|
|
|
print_line(self.tb, c, i, *color) |
|
38
|
|
|
|
|
39
|
|
|
def validate_active(self): |
|
40
|
|
|
if self.active < 0: |
|
41
|
|
|
self.active = 0 |
|
42
|
|
|
if self.active >= len(self.choices): |
|
43
|
|
|
self.active = len(self.choices)-1 |
|
44
|
|
|
|
|
45
|
|
|
def set_active(self, i): |
|
46
|
|
|
self.active = i |
|
47
|
|
|
self.validate_active() |
|
48
|
|
|
|
|
49
|
|
|
def move_up(self): |
|
50
|
|
|
self.active -= 1 |
|
51
|
|
|
self.validate_active() |
|
52
|
|
|
|
|
53
|
|
|
def move_down(self): |
|
54
|
|
|
self.active += 1 |
|
55
|
|
|
self.validate_active() |
|
56
|
|
|
|
|
57
|
|
|
choices = [ |
|
58
|
|
|
u"This instructs Psyco", |
|
59
|
|
|
u"to compile and run as", |
|
60
|
|
|
u"much of your application", |
|
61
|
|
|
u"code as possible. This is the", |
|
62
|
|
|
u"simplest interface to Psyco.", |
|
63
|
|
|
u"In good cases you can just add", |
|
64
|
|
|
u"these two lines and enjoy the speed-up.", |
|
65
|
|
|
u"If your application does a lot", |
|
66
|
|
|
u"of initialization stuff before", |
|
67
|
|
|
u"the real work begins, you can put", |
|
68
|
|
|
u"the above two lines after this", |
|
69
|
|
|
u"initialization - e.g. after importing", |
|
70
|
|
|
u"modules, creating constant global objects, etc.", |
|
71
|
|
|
u"This instructs Psyco", |
|
72
|
|
|
u"to compile and run as", |
|
73
|
|
|
u"much of your application", |
|
74
|
|
|
u"code as possible. This is the", |
|
75
|
|
|
u"simplest interface to Psyco.", |
|
76
|
|
|
u"In good cases you can just add", |
|
77
|
|
|
u"these two lines and enjoy the speed-up.", |
|
78
|
|
|
u"If your application does a lot", |
|
79
|
|
|
u"of initialization stuff before", |
|
80
|
|
|
u"the real work begins, you can put", |
|
81
|
|
|
u"the above two lines after this", |
|
82
|
|
|
u"initialization - e.g. after importing", |
|
83
|
|
|
u"modules, creating constant global objects, etc." |
|
84
|
|
|
] |
|
85
|
|
|
|
|
86
|
|
|
def draw_bottom_line(t, i): |
|
87
|
|
|
i = i % 8 |
|
88
|
|
|
w = t.width() |
|
89
|
|
|
h = t.height() |
|
90
|
|
|
c = i |
|
91
|
|
|
palette = [termbox.DEFAULT, termbox.BLACK, termbox.RED, termbox.GREEN, |
|
92
|
|
|
termbox.YELLOW, termbox.BLUE, termbox.MAGENTA, termbox.CYAN, |
|
93
|
|
|
termbox.WHITE] |
|
94
|
|
|
for x in range(w): |
|
95
|
|
|
t.change_cell(x, h-1, ord(u' '), termbox.BLACK, palette[c]) |
|
96
|
|
|
t.change_cell(x, h-2, ord(u' '), termbox.BLACK, palette[c]) |
|
97
|
|
|
c += 1 |
|
98
|
|
|
if c > 7: |
|
99
|
|
|
c = 0 |
|
100
|
|
|
|
|
101
|
|
|
with termbox.Termbox() as t: |
|
102
|
|
|
sb = SelectBox(t, choices, 0) |
|
103
|
|
|
t.clear() |
|
104
|
|
|
sb.draw() |
|
105
|
|
|
t.present() |
|
106
|
|
|
i = 0 |
|
107
|
|
|
run_app = True |
|
108
|
|
|
while run_app: |
|
109
|
|
|
event_here = t.poll_event() |
|
110
|
|
|
while event_here: |
|
111
|
|
|
(type, ch, key, mod, w, h, x, y) = event_here |
|
112
|
|
|
if type == termbox.EVENT_KEY and key == termbox.KEY_ESC: |
|
113
|
|
|
run_app = False |
|
114
|
|
|
if type == termbox.EVENT_KEY: |
|
115
|
|
|
if key == termbox.KEY_ARROW_DOWN: |
|
116
|
|
|
sb.move_down() |
|
117
|
|
|
elif key == termbox.KEY_ARROW_UP: |
|
118
|
|
|
sb.move_up() |
|
119
|
|
|
elif key == termbox.KEY_HOME: |
|
120
|
|
|
sb.set_active(-1) |
|
121
|
|
|
elif key == termbox.KEY_END: |
|
122
|
|
|
sb.set_active(999) |
|
123
|
|
|
event_here = t.peek_event() |
|
124
|
|
|
|
|
125
|
|
|
t.clear() |
|
126
|
|
|
sb.draw() |
|
127
|
|
|
draw_bottom_line(t, i) |
|
128
|
|
|
t.present() |
|
129
|
|
|
i += 1 |
|
130
|
|
|
|