mirror of
https://github.com/kennethreitz/crayons.git
synced 2026-06-05 23:10:18 +00:00
Add random() to select a color at random (#14)
This commit is contained in:
+17
-3
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user