rm dist
[rainbowstream.git] / rainbowstream / colors.py
1 import random
2 import itertools
3 from functools import wraps
4 from termcolor import *
5 from pyfiglet import figlet_format
6
7 grey = lambda x: colored(x, 'grey', attrs=['bold'])
8 red = lambda x: colored(x, 'red', attrs=['bold'])
9 green = lambda x: colored(x, 'green', attrs=['bold'])
10 yellow = lambda x: colored(x, 'yellow', attrs=['bold'])
11 blue = lambda x: colored(x, 'blue', attrs=['bold'])
12 magenta = lambda x: colored(x, 'magenta', attrs=['bold'])
13 cyan = lambda x: colored(x, 'cyan', attrs=['bold'])
14 white = lambda x: colored(x, 'white', attrs=['bold'])
15
16 on_grey = lambda x: colored(x, 'white', 'on_grey', attrs=['bold'])
17 on_red = lambda x: colored(x, 'white', 'on_red', attrs=['bold'])
18 on_green = lambda x: colored(x, 'white', 'on_green', attrs=['bold'])
19 on_yellow = lambda x: colored(x, 'white', 'on_yellow', attrs=['bold'])
20 on_blue = lambda x: colored(x, 'white', 'on_blue', attrs=['bold'])
21 on_magenta = lambda x: colored(x, 'white', 'on_magenta', attrs=['bold'])
22 on_cyan = lambda x: colored(x, 'white', 'on_cyan', attrs=['bold'])
23 on_white = lambda x: colored(x, 'white', 'on_white', attrs=['bold'])
24
25 colors_shuffle = [grey, red, green, yellow, blue, magenta, cyan]
26 background_shuffle = [
27 on_grey,
28 on_red,
29 on_green,
30 on_yellow,
31 on_blue,
32 on_magenta,
33 on_cyan]
34 cyc = itertools.cycle(colors_shuffle[1:])
35
36
37 def 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
45 def 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
53 def 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
68 def cycle_color(s):
69 """
70 Cycle the colors_shuffle
71 """
72 return next(cyc)(s)
73
74
75 def 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 )