README.rst
[rainbowstream.git] / build / lib / rainbowstream / colors.py
... / ...
CommitLineData
1import random
2import itertools
3from functools import wraps
4from termcolor import *
5from pyfiglet import figlet_format
6
7grey = lambda x: colored(x, 'grey', attrs=['bold'])
8red = lambda x: colored(x, 'red', attrs=['bold'])
9green = lambda x: colored(x, 'green', attrs=['bold'])
10yellow = lambda x: colored(x, 'yellow', attrs=['bold'])
11blue = lambda x: colored(x, 'blue', attrs=['bold'])
12magenta = lambda x: colored(x, 'magenta', attrs=['bold'])
13cyan = lambda x: colored(x, 'cyan', attrs=['bold'])
14white = lambda x: colored(x, 'white', attrs=['bold'])
15
16on_grey = lambda x: colored(x, 'white', 'on_grey', attrs=['bold'])
17on_red = lambda x: colored(x, 'white', 'on_red', attrs=['bold'])
18on_green = lambda x: colored(x, 'white', 'on_green', attrs=['bold'])
19on_yellow = lambda x: colored(x, 'white', 'on_yellow', attrs=['bold'])
20on_blue = lambda x: colored(x, 'white', 'on_blue', attrs=['bold'])
21on_magenta = lambda x: colored(x, 'white', 'on_magenta', attrs=['bold'])
22on_cyan = lambda x: colored(x, 'white', 'on_cyan', attrs=['bold'])
23on_white = lambda x: colored(x, 'white', 'on_white', attrs=['bold'])
24
25colors_shuffle = [grey, red, green, yellow, blue, magenta, cyan]
26background_shuffle = [
27 on_grey,
28 on_red,
29 on_green,
30 on_yellow,
31 on_blue,
32 on_magenta,
33 on_cyan]
34cyc = itertools.cycle(colors_shuffle[1:])
35
36
37def order_rainbow(s):
38 """
39 Print a string with ordered color with each character
40 """
41 c = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))]
42 return reduce(lambda x, y: x + y, c)
43
44
45def random_rainbow(s):
46 """
47 Print a string with random color with each character
48 """
49 c = [random.choice(colors_shuffle)(i) for i in s]
50 return reduce(lambda x, y: x + y, c)
51
52
53def Memoize(func):
54 """
55 Memoize decorator
56 """
57 cache = {}
58
59 @wraps(func)
60 def wrapper(*args):
61 if args not in cache:
62 cache[args] = func(*args)
63 return cache[args]
64 return wrapper
65
66
67@Memoize
68def cycle_color(s):
69 """
70 Cycle the colors_shuffle
71 """
72 return next(cyc)(s)
73
74
75def ascii_art(text):
76 """
77 Draw the Ascii Art
78 """
79 fi = figlet_format(text, font='doom')
80 print('\n'.join(
81 [next(cyc)(i) for i in fi.split('\n')]
82 )
83 )