1 # Copyright (C) 2008 Konstantin Lepa <konstantin.lepa@gmail.com>.
3 # This file is part of termcolor.
5 # termcolor is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
10 # termcolor is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # You should have received a copy of the GNU General Public License
16 # along with termcolor. If not, see <http://www.gnu.org/licenses/>.
18 """ANSII Color formatting for output in terminal."""
23 __ALL__
= [ 'colored' ]
75 def colored(text
, color
=None, on_color
=None, attrs
=None):
78 Available text colors:
79 red, green, yellow, blue, magenta, cyan, white.
81 Available text highlights:
82 on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.
85 bold, dark, underline, blink, reverse, concealed.
88 colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
89 colored('Hello, World!', 'green')
91 if os
.getenv('ANSI_COLORS_DISABLED') is None:
92 fmt_str
= '\033[1;%dm%s'
94 text
= fmt_str
% (colors
[color
], text
)
96 if on_color
is not None:
97 text
= fmt_str
% (highlights
[on_color
], text
)
101 text
= fmt_str
% (attributes
[attr
], text
)
109 if __name__
== '__main__':
110 print 'Current terminal type: ', os
.getenv('TERM')
111 print 'Test basic colors:'
112 print colored('Grey color', 'grey')
113 print colored('Red color', 'red')
114 print colored('Green color', 'green')
115 print colored('Yellow color', 'yellow')
116 print colored('Blue color', 'blue')
117 print colored('Magenta color', 'magenta')
118 print colored('Cyan color', 'cyan')
119 print colored('White color', 'white')
122 print 'Test highlights:'
123 print colored('On grey color', on_color
='on_grey')
124 print colored('On red color', on_color
='on_red')
125 print colored('On green color', on_color
='on_green')
126 print colored('On yellow color', on_color
='on_yellow')
127 print colored('On blue color', on_color
='on_blue')
128 print colored('On magenta color', on_color
='on_magenta')
129 print colored('On cyan color', on_color
='on_cyan')
130 print colored('On white color', color
='grey', on_color
='on_white')
133 print 'Test attributes:'
134 print colored('Bold grey color', 'grey', attrs
=['bold'])
135 print colored('Dark red color', 'red', attrs
=['dark'])
136 print colored('Underline green color', 'green', attrs
=['underline'])
137 print colored('Blink yellow color', 'yellow', attrs
=['blink'])
138 print colored('Reversed blue color', 'blue', attrs
=['reverse'])
139 print colored('Concealed Magenta color', 'magenta', attrs
=['concealed'])
140 print colored('Bold underline reverse cyan color', 'cyan',
141 attrs
=['bold', 'underline', 'reverse'])
142 print colored('Dark blink concealed white color', 'white',
143 attrs
=['dark', 'blink', 'concealed'])
147 print colored('Underline red on grey color', 'red', 'on_grey',
149 print colored('Reversed green on red color', 'green', 'on_red', ['reverse'])