This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
colored.py
111 lines (72 loc) · 2.24 KB
/
colored.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
"""
clint.colored
~~~~~~~~~~~~~
This module provides a simple and elegant wrapper for colorama.
"""
from __future__ import absolute_import
import re
import sys
from ..packages import colorama
if sys.stdout.isatty():
colorama.init(autoreset=True)
__all__ = (
'red', 'green', 'yellow', 'blue',
'black', 'magenta', 'cyan', 'white'
)
class ColoredString(object):
"""Enhanced string for __len__ operations on Colored output."""
def __init__(self, color, s):
super(ColoredString, self).__init__()
self.s = s
self.color = color
@property
def color_str(self):
if sys.stdout.isatty():
return '%s%s%s' % (
getattr(colorama.Fore, self.color), self.s, colorama.Fore.RESET)
else:
return self.s
def __len__(self):
return len(self.s)
def __repr__(self):
return "<%s-string: '%s'>" % (self.color, self.s)
def __str__(self):
return self.__unicode__().encode('utf8')
def __unicode__(self):
return self.color_str
def __add__(self, other):
self.s += other
return self
def __radd__(self, other):
self.s = other + self.s
return self
def __mul__(self, other):
return (self.color_str * other)
def split(self, x=' '):
# print map(self._new, self.s.split(x))
return map(self._new, self.s.split(x))
def _new(self, s):
return ColoredString(self.color, s)
def clean(s):
strip = re.compile("([^-_a-zA-Z0-9!@#%&=,/'\";:~`\$\^\*\(\)\+\[\]\.\{\}\|\?\<\>\\]+|[^\s]+)")
txt = strip.sub('', str(s))
strip = re.compile(r'\[\d+m')
txt = strip.sub('', txt)
return txt
def black(string):
return ColoredString('BLACK', string)
def red(string):
return ColoredString('RED', string)
def green(string):
return ColoredString('GREEN', string)
def yellow(string):
return ColoredString('YELLOW', string)
def blue(string):
return ColoredString('BLUE', string)
def magenta(string):
return ColoredString('MAGENTA', string)
def cyan(string):
return ColoredString('CYAN', string)
def white(string):
return ColoredString('WHITE', string)