-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounce_fortune.py
60 lines (51 loc) · 1.45 KB
/
bounce_fortune.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
"""
Display a fortune in a cool way
Requires the `fortune` command to be installed.
"""
import curses
import random
import subprocess
def main(stdscr):
curses.curs_set(0)
bounce_fortune(stdscr)
while True:
c = get_chr(stdscr)
if c:
stdscr.clear()
bounce_fortune(stdscr)
def bounce_fortune(stdscr):
height, width = stdscr.getmaxyx()
ypos, xpos = random.randrange(0, height - 1), random.randrange(0, width - 1)
ydir, xdir = random.choice([-1, 1]), random.choice([-1, 1])
stdscr.nodelay(1)
c = None
fortune = get_fortune()
for c in fortune:
if ypos in {0, height - 1}:
ydir *= -1
if xpos in {0, width - 1}:
xdir *= -1
ypos += ydir
xpos += xdir
stdscr.addstr(ypos, xpos, c)
curses.delay_output(50)
stdscr.refresh()
def get_chr(stdscr):
try:
c = stdscr.getkey()
except curses.error:
return None
if c == 'q':
exit(0)
return c
def get_fortune():
p = subprocess.Popen(['fortune', '-s'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = p.communicate()
r = p.returncode
if r or not output[0]:
if output[1]:
raise RuntimeError("fortune did not work: " + output[1])
raise RuntimeError("fortune did not work. Maybe it isn't installed?")
return ' '.join(output[0].strip().split('\n'))
if __name__ == '__main__':
curses.wrapper(main)