-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_codes.py
executable file
·177 lines (140 loc) · 5.81 KB
/
color_codes.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -*- coding: utf-8 -*-
"""Terminal formatting with ANSI escape sequences.
On terminals that support ANSI escape sequences, generate codes to color
and format output.
The ColorCodes class provides an instance method get_code() for
using directly within code. It also makes the sequence
available for reference as an instance property
called "code." See examples below.
Values is a helper class to allow ColorCodes to be easily instantiated
without having to use reference information. However, the
Values class is not required. ColorCodes parameters may be
entered as integers and booleans.
ANSI escape sequences aren't supported by Windows, but may work
on Windows with terminals included with applications such
as PyCharm. Alternatives include:
colorama is a good cross-platform package for
producing terminal color.
https://github.com/tartley/colorama
ansicon provides ANSI escape sequences for Windows console
programs. However, this is only useful on a per-user basis,
as opposed to being a solution for one's Windows users.
https://github.com/adoxa/ansicon
Examples:
Create a ColorCodes instance:
bright_green = ColorCodes(
Values.Color.green,
Values.Target.foreground,
Values.Intensity.bright,
underline=False, bold=False)
Use the ColorCodes get_code() method to change the
color of console text:
print("{}This text is green.{} And this color "
"has been reset.".format(
bright_green.get_code(),
bright_green.reset_code))
Use the "code" property to print the generated escape sequence:
print(bright_green.code)
The escape sequence can be used directly:
print("{}This is also green text.".format('\x1b[92m'))
References:
https://en.wikipedia.org/wiki/ANSI_escape_code
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
"""
from __future__ import (absolute_import, print_function, unicode_literals)
class Values(object):
"""Map ANSI values to readable names."""
class Color(object):
"""ANSI color table."""
black = 0
red = 1
green = 2
yellow = 3
blue = 4
magenta = 5
cyan = 6
white = 7
class Target(object):
"""Background colors values are 10 more than foreground values."""
foreground = 0
background = 10
class Intensity(object):
"""Normal color values begin at 30. Bright values begin at 90."""
normal = 30
bright = 90
class ColorCodes(object):
"""Terminal formatting with ANSI escape sequences.
Instance Attributes:
code: The escape sequence for the ColorCode instance
for reference or "manual" use.
Class Attributes:
csi: The Control Sequence Initiator. The CSI begins with an escape
character, "ESC," represented as ASCII decimal 27, hex 0x1B,
or octal 033. This is followed by '[' for sequences of
more than two characters.
reset_code: The reset/normal escape sequence.
"""
_escape = '\033'
csi = _escape + '['
_sgr_final_byte = 'm'
reset_code = csi + '0' + _sgr_final_byte
def __init__(self, color, target, intensity, underline=False, bold=False):
"""Object initialization
Note that the Values class may be used when instantiating
a ColorCodes object, instead of using literal integers and booleans.
Args:
color (int): Value from the ANSI color table.
target (int): SGR code indicating either foreground or background.
intensity (int): SGR code indicating normal or bright intensity.
underline (bool): Include the SGR code for underline if True.
bold (bool): Include the SRG code for bold if True.
"""
self.color = color
self.target = target
self.intensity = intensity
self.underline = underline
self.bold = bold
self.color_code = None
self.sgr_code = ''
self._initialize_instance()
def _initialize_instance(self):
"""Orchestrator for escape sequence."""
self._generate_color_value()
self._generate_sgr_code()
self._calculate_effects()
self._add_final_byte()
self.code = self.sgr_code.__repr__()
return None
def _generate_color_value(self):
"""The initial value is the sum of color, target, and intensity."""
self.color_code = str(self.color + self.target + self.intensity)
return None
def _generate_sgr_code(self):
"""Generate initial SGR (Select Graphic Rendition) value."""
# The SGR begins with a CSI (Control Sequence Initiator),
# followed by the value generated for color,
# target (foreground/background), and brightness.
self.sgr_code += ColorCodes.csi
self.sgr_code += self.color_code
return None
def _calculate_effects(self):
"""Add values to the SGR code for underline and bold if present."""
if self.underline:
self.sgr_code = "{0};{1}".format(
self.sgr_code,
str(4)
)
if self.bold:
self.sgr_code = "{0};{1}".format(
self.sgr_code,
str(1)
)
return None
def _add_final_byte(self):
"""Escape codes end with a final byte."""
self.sgr_code += ColorCodes._sgr_final_byte
return None
def get_code(self):
"""Return the generated escape sequence. This value can
be used directly in print() statements."""
return self.sgr_code