| 1 | import random |
| 2 | import itertools |
| 3 | from functools import wraps |
| 4 | from pyfiglet import figlet_format |
| 5 | from .config import * |
| 6 | from functools import reduce |
| 7 | |
| 8 | |
| 9 | def basic_color(code): |
| 10 | """ |
| 11 | 16 colors supported |
| 12 | """ |
| 13 | def inner(text, bold=True): |
| 14 | c = code |
| 15 | if bold: |
| 16 | c = "1;%s" % c |
| 17 | return "\033[%sm%s\033[0m" % (c, text) |
| 18 | return inner |
| 19 | |
| 20 | |
| 21 | def RGB(code): |
| 22 | """ |
| 23 | 256 colors supported |
| 24 | """ |
| 25 | def inner(text): |
| 26 | c = code |
| 27 | return "\033[38;5;%sm%s\033[0m" % (c, text) |
| 28 | return inner |
| 29 | |
| 30 | |
| 31 | default = basic_color('39') |
| 32 | black = basic_color('30') |
| 33 | red = basic_color('31') |
| 34 | green = basic_color('32') |
| 35 | yellow = basic_color('33') |
| 36 | blue = basic_color('34') |
| 37 | magenta = basic_color('35') |
| 38 | cyan = basic_color('36') |
| 39 | grey = basic_color('90') |
| 40 | light_red = basic_color('91') |
| 41 | light_green = basic_color('92') |
| 42 | light_yellow = basic_color('93') |
| 43 | light_blue = basic_color('94') |
| 44 | light_magenta = basic_color('95') |
| 45 | light_cyan = basic_color('96') |
| 46 | white = basic_color('97') |
| 47 | |
| 48 | on_default = basic_color('49') |
| 49 | on_black = basic_color('40') |
| 50 | on_red = basic_color('41') |
| 51 | on_green = basic_color('42') |
| 52 | on_yellow = basic_color('43') |
| 53 | on_blue = basic_color('44') |
| 54 | on_magenta = basic_color('45') |
| 55 | on_cyan = basic_color('46') |
| 56 | on_grey = basic_color('100') |
| 57 | on_light_red = basic_color('101') |
| 58 | on_light_green = basic_color('102') |
| 59 | on_light_yellow = basic_color('103') |
| 60 | on_light_blue = basic_color('104') |
| 61 | on_light_magenta = basic_color('105') |
| 62 | on_light_cyan = basic_color('106') |
| 63 | on_white = basic_color('107') |
| 64 | |
| 65 | def init_cycle(): |
| 66 | colors_shuffle = [globals()[i.encode('utf8')] |
| 67 | if not i.startswith('RGB_') |
| 68 | else RGB(int(i[4:])) |
| 69 | for i in c['CYCLE_COLOR']] |
| 70 | return itertools.cycle(colors_shuffle) |
| 71 | |
| 72 | cyc = init_cycle() |
| 73 | |
| 74 | def order_rainbow(s): |
| 75 | """ |
| 76 | Print a string with ordered color with each character |
| 77 | """ |
| 78 | c = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))] |
| 79 | return reduce(lambda x, y: x + y, c) |
| 80 | |
| 81 | |
| 82 | def random_rainbow(s): |
| 83 | """ |
| 84 | Print a string with random color with each character |
| 85 | """ |
| 86 | c = [random.choice(colors_shuffle)(i) for i in s] |
| 87 | return reduce(lambda x, y: x + y, c) |
| 88 | |
| 89 | |
| 90 | def Memoize(func): |
| 91 | """ |
| 92 | Memoize decorator |
| 93 | """ |
| 94 | cache = {} |
| 95 | |
| 96 | @wraps(func) |
| 97 | def wrapper(*args): |
| 98 | if args not in cache: |
| 99 | cache[args] = func(*args) |
| 100 | return cache[args] |
| 101 | return wrapper |
| 102 | |
| 103 | |
| 104 | @Memoize |
| 105 | def cycle_color(s): |
| 106 | """ |
| 107 | Cycle the colors_shuffle |
| 108 | """ |
| 109 | return next(cyc)(s) |
| 110 | |
| 111 | |
| 112 | def ascii_art(text): |
| 113 | """ |
| 114 | Draw the Ascii Art |
| 115 | """ |
| 116 | fi = figlet_format(text, font='doom') |
| 117 | print('\n'.join( |
| 118 | [next(cyc)(i) for i in fi.split('\n')] |
| 119 | ) |
| 120 | ) |