Debian Codegraph @

demos/demo06.py

raw
1 # Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
2 from __future__ import print_function
3 import fixpath
4 import colorama
5 from colorama import Fore, Back, Style, Cursor
6 from random import randint, choice
7 from string import printable
8
9 # Demonstrate printing colored, random characters at random positions on the screen
10
11 # Fore, Back and Style are convenience classes for the constant ANSI strings that set
12 # the foreground, background and style. They don't have any magic of their own.
13 FORES = [ Fore.BLACK, Fore.RED, Fore.GREEN, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.CYAN, Fore.WHITE ]
14 BACKS = [ Back.BLACK, Back.RED, Back.GREEN, Back.YELLOW, Back.BLUE, Back.MAGENTA, Back.CYAN, Back.WHITE ]
15 STYLES = [ Style.DIM, Style.NORMAL, Style.BRIGHT ]
16
17 # This assumes your terminal is 80x24. Ansi minimum coordinate is (1,1).
18 MINY, MAXY = 1, 24
19 MINX, MAXX = 1, 80
20
21 # set of printable ASCII characters, including a space.
22 CHARS = ' ' + printable.strip()
23
24 PASSES = 1000
25
26 def main():
27 colorama.just_fix_windows_console()
28 pos = lambda y, x: Cursor.POS(x, y)
29 # draw a white border.
30 print(Back.WHITE, end='')
31 print('%s%s' % (pos(MINY, MINX), ' '*MAXX), end='')
32 for y in range(MINY, 1+MAXY):
33 print('%s %s ' % (pos(y, MINX), pos(y, MAXX)), end='')
34 print('%s%s' % (pos(MAXY, MINX), ' '*MAXX), end='')
35 # draw some blinky lights for a while.
36 for i in range(PASSES):
37 print('%s%s%s%s%s' % (pos(randint(1+MINY,MAXY-1), randint(1+MINX,MAXX-1)), choice(FORES), choice(BACKS), choice(STYLES), choice(CHARS)), end='')
38 # put cursor to top, left, and set color to white-on-black with normal brightness.
39 print('%s%s%s%s' % (pos(MINY, MINX), Fore.WHITE, Back.BLACK, Style.NORMAL), end='')
40
41 if __name__ == '__main__':
42 main()