mirror of
https://github.com/kennethreitz/crayons.git
synced 2026-06-05 23:10:18 +00:00
Merge pull request #15 from grahamu/normal
Backport improvements/fixes from pipenv
This commit is contained in:
+2
-1
@@ -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.
|
||||
|
||||
+20
-5
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user