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