Добавляем интересную разработку
[archive.git] / Apkawa / tsru / termcolor.py
blob3a8e04908dbe4d9670c1c34edb257a699ffaeac3
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
8 # version.
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
13 # more details.
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."""
20 import os
23 __ALL__ = [ 'colored' ]
26 attributes = dict(
27 zip([
28 'bold',
29 'dark',
30 '',
31 'underline',
32 'blink',
33 '',
34 'reverse',
35 'concealed'
37 range(1, 9)
40 del attributes['']
43 highlights = dict(
44 zip([
45 'on_grey',
46 'on_red',
47 'on_green',
48 'on_yellow',
49 'on_blue',
50 'on_magenta',
51 'on_cyan',
52 'on_white'
54 range(40, 48)
59 colors = dict(
60 zip([
61 'grey',
62 'red',
63 'green',
64 'yellow',
65 'blue',
66 'magenta',
67 'cyan',
68 'white',
70 range(30, 38)
75 def colored(text, color=None, on_color=None, attrs=None):
76 """Colorize text.
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.
84 Available attributes:
85 bold, dark, underline, blink, reverse, concealed.
87 Example:
88 colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
89 colored('Hello, World!', 'green')
90 """
91 if os.getenv('ANSI_COLORS_DISABLED') is None:
92 fmt_str = '\033[1;%dm%s'
93 if color is not None:
94 text = fmt_str % (colors[color], text)
96 if on_color is not None:
97 text = fmt_str % (highlights[on_color], text)
99 if attrs is not None:
100 for attr in attrs:
101 text = fmt_str % (attributes[attr], text)
103 reset = '\033[1;m'
104 text += reset
106 return 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')
120 print '-' * 78
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')
131 print '-' * 78
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'])
144 print '-' * 78
146 print 'Test mixing:'
147 print colored('Underline red on grey color', 'red', 'on_grey',
148 ['underline'])
149 print colored('Reversed green on red color', 'green', 'on_red', ['reverse'])