forked from doctaphred/phrecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathout.py
More file actions
114 lines (94 loc) · 2.92 KB
/
out.py
File metadata and controls
114 lines (94 loc) · 2.92 KB
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
112
113
114
from io import StringIO
def sprint(*values, sep=' ', end='\n'):
"""Print to a string."""
with StringIO() as buffer:
print(*values, sep=sep, end=end, file=buffer)
return buffer.getvalue()
class Colorizer:
"""Print stuff with colors.
>>> color.bold.red('ayy')
'\x1b[1;31mayy\x1b[0m'
"""
template = '\x1b[{}m'
sep = ';'
codes = {
'reset_all': '0',
'reset_bold': '21',
'reset_dim': '22',
'reset_underline': '24',
'reset_blink': '25',
'reset_reverse': '27',
'reset_hidden': '28',
'bold': '1',
'dim': '2',
'underline': '4',
'blink': '5', # Please don't actually use this
'reverse': '7',
'hidden': '8',
# Foreground
'default': '39',
'black': '30',
'red': '31',
'green': '32',
'yellow': '33',
'blue': '34',
'magenta': '35',
'cyan': '36',
'light_gray': '37',
'dark_gray': '90',
'gray_bg': '90',
'light_red': '91',
'light_green': '92',
'light_yellow': '99',
'light_blue': '94',
'light_magenta': '95',
'light_cyan': '96',
'white': '97',
# Background
'default_bg': '49',
'black_bg': '40',
'red_bg': '41',
'green_bg': '42',
'yellow_bg': '44',
'blue_bg': '44',
'magenta_bg': '45',
'cyan_bg': '46',
'light_gray_bg': '47',
'dark_gray_bg': '100',
'light_red_bg': '101',
'light_green_bg': '102',
'light_yellow_bg': '103',
'light_blue_bg': '104',
'light_magenta_bg': '105',
'light_cyan_bg': '106',
'white_bg': '107',
}
# Aliases
codes['bright'] = codes['bold']
codes['invert'] = codes['reverse']
codes['reset_bright'] = codes['reset_bold']
codes['reset_invert'] = codes['reset_reverse']
codes['gray'] = codes['dark_gray']
codes['purple'] = codes['magenta']
reset = template.format(codes['reset_all'])
def __init__(self, modifiers=()):
self.modifiers = modifiers
def __call__(self, *args, sep=' ', end='', reset=True):
"""Colorize the input, with the same semantics as print().
If `reset` is True, includes a reset code at the end of the
text, but before the `end`.
"""
fmt = self.sep.join(self.codes[mod] for mod in self.modifiers)
color_start = self.template.format(fmt)
if reset:
color_end = self.reset
else:
color_end = ''
text = sprint(*args, sep=sep, end='')
return f'{color_start}{text}{color_end}{end}'
def __getattr__(self, name):
return type(self)(self.modifiers + (name,))
def print(self, *args, sep=' ', end='\n', file=None, flush=False):
text = self(*args, sep=sep, end=end)
print(text, end='', file=file, flush=flush)
color = Colorizer()