Add random() to select a color at random (#14)

This commit is contained in:
Matthew Peveler
2019-09-09 06:41:35 -03:00
parent 469441e4d8
commit 800c50aeab
2 changed files with 19 additions and 3 deletions
+17 -3
View File
@@ -2,6 +2,7 @@
"""A simple and elegant wrapper for colorama."""
import os
from random import choice, seed
import re
import sys
@@ -13,11 +14,13 @@ PY3 = sys.version_info[0] >= 3
__all__ = (
'red', 'green', 'yellow', 'blue',
'black', 'magenta', 'cyan', 'white',
'clean', 'disable', 'enable'
'clean', 'disable', 'enable', 'random'
)
colorama.init()
COLORS = __all__[:-2]
seed()
COLORS = __all__[:-4]
if 'get_ipython' in dir():
"""
@@ -123,7 +126,7 @@ def clean(s):
return txt
_colors = {x: x.upper() for x in __all__[:-3]}
_colors = {x: x.upper() for x in COLORS}
_colors['normal'] = 'RESET'
for key, val in _colors.items():
@@ -134,6 +137,17 @@ for key, val in _colors.items():
del key, val, _colors, function
def random(string, always=False, bold=False, colors=COLORS):
"""Selects a color at random from a list."""
colors = list(filter(lambda color: color in COLORS, colors)) or COLORS
return ColoredString(
choice(colors).upper(),
string,
always_color=always,
bold=bold
)
def disable():
"""Disables colors."""
global DISABLE_COLOR
+2
View File
@@ -13,3 +13,5 @@ print(crayons.red('red string', bold=True))
print(crayons.yellow('yellow string', bold=True))
print(crayons.magenta('magenta string', bold=True))
print(crayons.white('white string', bold=True))
print(crayons.random('random color'))
print(crayons.random('random and bold', bold=True))