diff --git a/README.rst b/README.rst index 05cef59..d535e27 100644 --- a/README.rst +++ b/README.rst @@ -19,7 +19,7 @@ Crayons: Text UI colors for Python. This module is really simple, it gives you colored strings for terminal -usage. Included colors are ``red``, ``green``, ``yellow``, ``blue``, ``black``, ``magenta``, ``cyan``, and ``white`` ( as well as ``clean`` and ``disable``). +usage. Included colors are ``red``, ``green``, ``yellow``, ``blue``, ``black``, ``magenta``, ``cyan``, ``white``, and ``normal`` ( as well as ``clean`` and ``disable``). **Crayons** is nice because it automatically wraps a given string in both the foreground color, as well as returning to the original state after the string is complete. Most terminal color libraries make you manage this yourself. @@ -33,6 +33,7 @@ Features -------- - If you call ``disable()``, all future calls to colors will be ignored. +- If you call ``normal()``, color is reset to default foreground color - If the current process is not in a TTY (e.g. being piped), no colors will be displayed. - Length of ColoredStrings can be properly calculated. - Powered by colorama. diff --git a/crayons.py b/crayons.py index 455d3e9..5c68e0d 100644 --- a/crayons.py +++ b/crayons.py @@ -12,13 +12,13 @@ import os import re import sys -PY3 = sys.version_info[0] >= 3 - import colorama +PY3 = sys.version_info[0] >= 3 + __all__ = ( 'red', 'green', 'yellow', 'blue', - 'black', 'magenta', 'cyan', 'white', + 'black', 'magenta', 'cyan', 'white', 'normal', 'clean', 'disable' ) @@ -67,7 +67,11 @@ class ColoredString(object): @property def color_str(self): style = 'BRIGHT' if self.bold else 'NORMAL' - c = '%s%s%s%s%s' % (getattr(colorama.Fore, self.color), getattr(colorama.Style, style), self.s, colorama.Fore.RESET, getattr(colorama.Style, 'NORMAL')) + c = '%s%s%s%s%s' % (getattr(colorama.Fore, self.color), + getattr(colorama.Style, style), + self.s, + colorama.Fore.RESET, + getattr(colorama.Style, 'NORMAL')) if self.always_color: return c @@ -76,7 +80,6 @@ class ColoredString(object): else: return self.s - def __len__(self): return len(self.s) @@ -121,30 +124,42 @@ def clean(s): return txt +def normal(string, always=False, bold=False): + return ColoredString('RESET', string, always_color=always, bold=bold) + + def black(string, always=False, bold=False): return ColoredString('BLACK', string, always_color=always, bold=bold) + def red(string, always=False, bold=False): return ColoredString('RED', string, always_color=always, bold=bold) + def green(string, always=False, bold=False): return ColoredString('GREEN', string, always_color=always, bold=bold) + def yellow(string, always=False, bold=False): return ColoredString('YELLOW', string, always_color=always, bold=bold) + def blue(string, always=False, bold=False): return ColoredString('BLUE', string, always_color=always, bold=bold) + def magenta(string, always=False, bold=False): return ColoredString('MAGENTA', string, always_color=always, bold=bold) + def cyan(string, always=False, bold=False): return ColoredString('CYAN', string, always_color=always, bold=bold) + def white(string, always=False, bold=False): return ColoredString('WHITE', string, always_color=always, bold=bold) + def disable(): """Disables colors.""" global DISABLE_COLOR diff --git a/setup.py b/setup.py index 331ab4d..48b1886 100755 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ required = [ setup( name='crayons', - version='0.1.2', + version='0.1.3', description='TextUI colors for Python.', long_description=long_description, author='Kenneth Reitz',