Skip to content

Commit 35020b0

Browse files
committed
To strip higher bits from inch and how to debug using files
1 parent d8c93ff commit 35020b0

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

‎example/curses/border_ext.py‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Testing out the curses lib.
6+
"""
7+
8+
import curses
9+
10+
def debug(str):
11+
"""
12+
Ease debugging with curese by writing string to file.
13+
"""
14+
15+
with open("debug.txt", "a") as f:
16+
f.write(str + "\n")
17+
18+
19+
20+
def dump(scr, startX, startY, endX, endY):
21+
"""
22+
Dump content of part of screen, do not output empty content.
23+
"""
24+
25+
with open("output.txt", "w") as f:
26+
for y in range(startY, endY):
27+
for x in range(startX, endX):
28+
29+
# Get the character from screen
30+
ch = scr.inch(y, x)
31+
debug(str(x) + " - " + str(y) + ": " + str(ch))
32+
33+
# Output only if not space
34+
if(ch != 32):
35+
ch = chr(ch & 0x00ff)
36+
f.write(ch)
37+
38+
39+
40+
def main(scr):
41+
"""
42+
Draw a border around the screen, move around using the cursor and leave a mark
43+
of the latest pressed character on the keyboard.
44+
45+
Perhaps you could make a nice painting using asciart?
46+
47+
Quit using 'q'.
48+
"""
49+
50+
# Clear the screen of any output
51+
scr.clear()
52+
53+
# Get screen dimensions
54+
y1, x1 = scr.getmaxyx()
55+
y1 -= 1
56+
x1 -= 1
57+
58+
y0, x0 = 0, 0
59+
60+
# Get center position
61+
yc, xc = (y1-y0)//2, (x1-x0)//2
62+
63+
# Draw a border
64+
scr.border()
65+
66+
# Move cursor to center
67+
scr.move(yc, xc)
68+
69+
# Refresh to draw out
70+
scr.refresh()
71+
72+
# Main loop
73+
x = xc
74+
y = yc
75+
ch = 'o'
76+
77+
while True:
78+
key = scr.getkey()
79+
if key == 'q':
80+
break
81+
elif key == 'o':
82+
dump(scr, 1, 1, x1, y1)
83+
break
84+
elif key == 'KEY_UP':
85+
y -= 1
86+
elif key == 'KEY_DOWN':
87+
y += 1
88+
elif key == 'KEY_LEFT':
89+
x -= 1
90+
elif key == 'KEY_RIGHT':
91+
x += 1
92+
else:
93+
ch = key
94+
95+
# Draw out the char at cursor positino
96+
scr.addstr(ch)
97+
98+
# Move cursor to new position
99+
scr.move(y, x)
100+
101+
# Redraw all items on the screen
102+
scr.refresh()
103+
104+
105+
106+
if __name__ == "__main__":
107+
print(__doc__)
108+
print(main.__doc__)
109+
input("Press enter to begin playing...")
110+
curses.wrapper(main)

0 commit comments

Comments
 (0)