2 # Copyright (c) 2008-2011 Volvox Development Team
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # Author: Konstantin Lepa <konstantin.lepa@gmail.com>
24 """ANSI Color formatting for output in terminal."""
26 from __future__ import print_function
30 __ALL__ = [ 'colored', 'cprint' ]
86 def colored(text, color=None, on_color=None, attrs=None):
89 Available text colors:
90 red, green, yellow, blue, magenta, cyan, white.
92 Available text highlights:
93 on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.
96 bold, dark, underline, blink, reverse, concealed.
99 colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
100 colored('Hello, World!', 'green')
102 if os.getenv('ANSI_COLORS_DISABLED') is None:
103 fmt_str = '\033[%dm%s'
104 if color is not None:
105 text = fmt_str % (COLORS[color], text)
107 if on_color is not None:
108 text = fmt_str % (HIGHLIGHTS[on_color], text)
110 if attrs is not None:
112 text = fmt_str % (ATTRIBUTES[attr], text)
118 def cprint(text, color=None, on_color=None, attrs=None, **kwargs):
119 """Print colorize text.
121 It accepts arguments of print function.
124 print((colored(text, color, on_color, attrs)), **kwargs)
127 if __name__ == '__main__':
128 print('Current terminal type: %s' % os.getenv('TERM'))
129 print('Test basic colors:')
130 cprint('Grey color', 'grey')
131 cprint('Red color', 'red')
132 cprint('Green color', 'green')
133 cprint('Yellow color', 'yellow')
134 cprint('Blue color', 'blue')
135 cprint('Magenta color', 'magenta')
136 cprint('Cyan color', 'cyan')
137 cprint('White color', 'white')
140 print('Test highlights:')
141 cprint('On grey color', on_color='on_grey')
142 cprint('On red color', on_color='on_red')
143 cprint('On green color', on_color='on_green')
144 cprint('On yellow color', on_color='on_yellow')
145 cprint('On blue color', on_color='on_blue')
146 cprint('On magenta color', on_color='on_magenta')
147 cprint('On cyan color', on_color='on_cyan')
148 cprint('On white color', color='grey', on_color='on_white')
151 print('Test attributes:')
152 cprint('Bold grey color', 'grey', attrs=['bold'])
153 cprint('Dark red color', 'red', attrs=['dark'])
154 cprint('Underline green color', 'green', attrs=['underline'])
155 cprint('Blink yellow color', 'yellow', attrs=['blink'])
156 cprint('Reversed blue color', 'blue', attrs=['reverse'])
157 cprint('Concealed Magenta color', 'magenta', attrs=['concealed'])
158 cprint('Bold underline reverse cyan color', 'cyan',
159 attrs=['bold', 'underline', 'reverse'])
160 cprint('Dark blink concealed white color', 'white',
161 attrs=['dark', 'blink', 'concealed'])
164 print('Test mixing:')
165 cprint('Underline red on grey color', 'red', 'on_grey',
167 cprint('Reversed green on red color', 'green', 'on_red', ['reverse'])