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