blob: 782d17f3033c859a115ee22e8f13d2ae66cfd0b2 [file] [log] [blame]
Andrew M. Kuchling15c3c2b2000-12-15 00:41:48 +00001#!/usr/bin/env python
2#
3# $Id$
4#
5# (n)curses exerciser in Python, an interactive test for the curses
6# module. Currently, only the panel demos are ported.
7
8import curses
9
10def wGetchar(win = None):
11 if win == None: win = stdscr
12 return win.getch()
13
14def Getchar():
15 wGetchar()
16
17#
18# Panels tester
19#
20def wait_a_while():
21 if nap_msec == 1:
22 Getchar()
23 else:
24 curses.napms(nap_msec)
25
26def saywhat(text):
27 stdscr.move(curses.LINES - 1, 0)
28 stdscr.clrtoeol()
29 stdscr.addstr(text)
30
31def mkpanel(color, rows, cols, tly, tlx):
32 win = curses.newwin(rows, cols, tly, tlx)
33 pan = win.new_panel()
34 if curses.has_colors():
35 if color == curses.COLOR_BLUE:
36 fg = curses.COLOR_WHITE
37 else:
38 fg = curses.COLOR_BLACK
39 bg = color
40 curses.init_pair(color, fg, bg)
41 win.bkgdset(ord(' '), curses.color_pair(color))
42 else:
43 win.bkgdset(ord(' '), curses.A_BOLD)
44
45 return pan
46
47def pflush():
48 curses.update_panels()
49 curses.doupdate()
50
51def fill_panel(pan):
52 win = pan.window()
53 num = pan.userptr()[1]
54
55 win.move(1, 1)
56 win.addstr("-pan%c-" % num)
57 win.clrtoeol()
58 win.box()
59
60 maxy, maxx = win.getmaxyx()
61 for y in range(2, maxy - 1):
62 for x in range(1, maxx - 1):
63 win.move(y, x)
64 win.addch(num)
65
66def demo_panels(win):
67 global stdscr, nap_msec, mod
68 stdscr = win
69 nap_msec = 1
70 mod = ["test", "TEST", "(**)", "*()*", "<-->", "LAST"]
71
72 stdscr.refresh()
73
74 for y in range(0, curses.LINES - 1):
75 for x in range(0, curses.COLS):
76 stdscr.addstr("%d" % ((y + x) % 10))
77 for y in range(0, 1):
78 p1 = mkpanel(curses.COLOR_RED,
79 curses.LINES / 2 - 2,
80 curses.COLS / 8 + 1,
81 0,
82 0)
83 p1.set_userptr("p1")
84
85 p2 = mkpanel(curses.COLOR_GREEN,
86 curses.LINES / 2 + 1,
87 curses.COLS / 7,
88 curses.LINES / 4,
89 curses.COLS / 10)
90 p2.set_userptr("p2")
91
92 p3 = mkpanel(curses.COLOR_YELLOW,
93 curses.LINES / 4,
94 curses.COLS / 10,
95 curses.LINES / 2,
96 curses.COLS / 9)
97 p3.set_userptr("p3")
98
99 p4 = mkpanel(curses.COLOR_BLUE,
100 curses.LINES / 2 - 2,
101 curses.COLS / 8,
102 curses.LINES / 2 - 2,
103 curses.COLS / 3)
104 p4.set_userptr("p4")
105
106 p5 = mkpanel(curses.COLOR_MAGENTA,
107 curses.LINES / 2 - 2,
108 curses.COLS / 8,
109 curses.LINES / 2,
110 curses.COLS / 2 - 2)
111 p5.set_userptr("p5")
112
113 fill_panel(p1)
114 fill_panel(p2)
115 fill_panel(p3)
116 fill_panel(p4)
117 fill_panel(p5)
118 p4.hide()
119 p5.hide()
120 pflush()
121 saywhat("press any key to continue")
122 wait_a_while()
123
124 saywhat("h3 s1 s2 s4 s5;press any key to continue")
125 p1.move(0, 0)
126 p3.hide()
127 p1.show()
128 p2.show()
129 p4.show()
130 p5.show()
131 pflush()
132 wait_a_while()
133
134 saywhat("s1; press any key to continue")
135 p1.show()
136 pflush()
137 wait_a_while()
138
139 saywhat("s2; press any key to continue")
140 p2.show()
141 pflush()
142 wait_a_while()
143
144 saywhat("m2; press any key to continue")
145 p2.move(curses.LINES / 3 + 1, curses.COLS / 8)
146 pflush()
147 wait_a_while()
148
149 saywhat("s3; press any key to continue")
150 p3.show()
151 pflush()
152 wait_a_while()
153
154 saywhat("m3; press any key to continue")
155 p3.move(curses.LINES / 4 + 1, curses.COLS / 15)
156 pflush()
157 wait_a_while()
158
159 saywhat("b3; press any key to continue")
160 p3.bottom()
161 pflush()
162 wait_a_while()
163
164 saywhat("s4; press any key to continue")
165 p4.show()
166 pflush()
167 wait_a_while()
168
169 saywhat("s5; press any key to continue")
170 p5.show()
171 pflush()
172 wait_a_while()
173
174 saywhat("t3; press any key to continue")
175 p3.top()
176 pflush()
177 wait_a_while()
178
179 saywhat("t1; press any key to continue")
180 p1.show()
181 pflush()
182 wait_a_while()
183
184 saywhat("t2; press any key to continue")
185 p2.show()
186 pflush()
187 wait_a_while()
188
189 saywhat("t3; press any key to continue")
190 p3.show()
191 pflush()
192 wait_a_while()
193
194 saywhat("t4; press any key to continue")
195 p4.show()
196 pflush()
197 wait_a_while()
198
199 for itmp in range(0, 6):
200 w4 = p4.window()
201 w5 = p5.window()
202
203 saywhat("m4; press any key to continue")
204 w4.move(curses.LINES / 8, 1)
205 w4.addstr(mod[itmp])
206 p4.move(curses.LINES / 6, itmp * curses.COLS / 8)
207 w5.move(curses.LINES / 6, 1)
208 w5.addstr(mod[itmp])
209 pflush()
210 wait_a_while()
211
212 saywhat("m5; press any key to continue")
213 w4.move(curses.LINES / 6, 1)
214 w4.addstr(mod[itmp])
215 p5.move(curses.LINES / 3 - 1, itmp * 10 + 6)
216 w5.move(curses.LINES / 8, 1)
217 w5.addstr(mod[itmp])
218 pflush()
219 wait_a_while()
220
221 saywhat("m4; press any key to continue")
222 p4.move(curses.LINES / 6, (itmp + 1) * curses.COLS / 8)
223 pflush()
224 wait_a_while()
225
226 saywhat("t5; press any key to continue")
227 p5.top()
228 pflush()
229 wait_a_while()
230
231 saywhat("t2; press any key to continue")
232 p2.top()
233 pflush()
234 wait_a_while()
235
236 saywhat("t1; press any key to continue")
237 p1.top()
238 pflush()
239 wait_a_while()
240
241 saywhat("d2; press any key to continue")
242 del p2
243 pflush()
244 wait_a_while()
245
246 saywhat("h3; press any key to continue")
247 p3.hide()
248 pflush()
249 wait_a_while()
250
251 saywhat("d1; press any key to continue")
252 del p1
253 pflush()
254 wait_a_while()
255
256 saywhat("d4; press any key to continue")
257 del p4
258 pflush()
259 wait_a_while()
260
261 saywhat("d5; press any key to continue")
262 del p5
263 pflush()
264 wait_a_while()
265 if nap_msec == 1:
266 break
267 nap_msec = 100
268
269#
270# one fine day there'll be the menu at this place
271#
272curses.wrapper(demo_panels)