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