OLD | NEW |
1 Download and docs: | 1 Download and docs: |
2 http://pypi.python.org/pypi/colorama | 2 http://pypi.python.org/pypi/colorama |
3 Development: | 3 Development: |
4 http://code.google.com/p/colorama | 4 http://code.google.com/p/colorama |
| 5 Discussion group: |
| 6 https://groups.google.com/forum/#!forum/python-colorama |
5 | 7 |
6 Description | 8 Description |
7 =========== | 9 =========== |
8 | 10 |
9 Makes ANSI escape character sequences, for producing colored terminal text and | 11 Makes ANSI escape character sequences for producing colored terminal text and |
10 cursor positioning, work under MS Windows. | 12 cursor positioning work under MS Windows. |
11 | 13 |
12 ANSI escape character sequences have long been used to produce colored terminal | 14 ANSI escape character sequences have long been used to produce colored terminal |
13 text and cursor positioning on Unix and Macs. Colorama makes this work on | 15 text and cursor positioning on Unix and Macs. Colorama makes this work on |
14 Windows, too. It also provides some shortcuts to help generate ANSI sequences, | 16 Windows, too, by wrapping stdout, stripping ANSI sequences it finds (which |
15 and works fine in conjunction with any other ANSI sequence generation library, | 17 otherwise show up as gobbledygook in your output), and converting them into the |
| 18 appropriate win32 calls to modify the state of the terminal. On other platforms, |
| 19 Colorama does nothing. |
| 20 |
| 21 Colorama also provides some shortcuts to help generate ANSI sequences |
| 22 but works fine in conjunction with any other ANSI sequence generation library, |
16 such as Termcolor (http://pypi.python.org/pypi/termcolor.) | 23 such as Termcolor (http://pypi.python.org/pypi/termcolor.) |
17 | 24 |
18 This has the upshot of providing a simple cross-platform API for printing | 25 This has the upshot of providing a simple cross-platform API for printing |
19 colored terminal text from Python, and has the happy side-effect that existing | 26 colored terminal text from Python, and has the happy side-effect that existing |
20 applications or libraries which use ANSI sequences to produce colored output on | 27 applications or libraries which use ANSI sequences to produce colored output on |
21 Linux or Macs can now also work on Windows, simply by calling | 28 Linux or Macs can now also work on Windows, simply by calling |
22 ``colorama.init()``. | 29 ``colorama.init()``. |
23 | 30 |
| 31 An alternative approach is to install 'ansi.sys' on Windows machines, which |
| 32 provides the same behaviour for all applications running in terminals. Colorama |
| 33 is intended for situations where that isn't easy (e.g. maybe your app doesn't |
| 34 have an installer.) |
| 35 |
24 Demo scripts in the source code repository prints some colored text using | 36 Demo scripts in the source code repository prints some colored text using |
25 ANSI sequences. Compare their output under Gnome-terminal's built in ANSI | 37 ANSI sequences. Compare their output under Gnome-terminal's built in ANSI |
26 handling, versus on Windows Command-Prompt using Colorama: | 38 handling, versus on Windows Command-Prompt using Colorama: |
27 | 39 |
28 .. image:: http://colorama.googlecode.com/hg/screenshots/ubuntu-demo.png | 40 .. image:: http://colorama.googlecode.com/hg/screenshots/ubuntu-demo.png |
29 :width: 661 | 41 :width: 661 |
30 :height: 357 | 42 :height: 357 |
31 :alt: ANSI sequences on Ubuntu under gnome-terminal. | 43 :alt: ANSI sequences on Ubuntu under gnome-terminal. |
32 | 44 |
33 .. image:: http://colorama.googlecode.com/hg/screenshots/windows-demo.png | 45 .. image:: http://colorama.googlecode.com/hg/screenshots/windows-demo.png |
34 :width: 668 | 46 :width: 668 |
35 :height: 325 | 47 :height: 325 |
36 :alt: Same ANSI sequences on Windows, using Colorama. | 48 :alt: Same ANSI sequences on Windows, using Colorama. |
37 | 49 |
38 These screengrabs show that Colorama on Windows does not support ANSI 'dim | 50 These screengrabs show that Colorama on Windows does not support ANSI 'dim |
39 text': it looks the same as 'normal text'. | 51 text': it looks the same as 'normal text'. |
40 | 52 |
41 | 53 |
| 54 License |
| 55 ======= |
| 56 |
| 57 Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file. |
| 58 |
| 59 |
42 Dependencies | 60 Dependencies |
43 ============ | 61 ============ |
44 | 62 |
45 None, other than Python. Tested on Python 2.5.5, 2.6.5, 2.7, 3.1.2, and 3.2 | 63 None, other than Python. Tested on Python 2.5.5, 2.6.5, 2.7, 3.1.2, and 3.2 |
46 | 64 |
47 | |
48 Usage | 65 Usage |
49 ===== | 66 ===== |
50 | 67 |
51 Initialisation | 68 Initialisation |
52 -------------- | 69 -------------- |
53 | 70 |
54 Applications should initialise Colorama using:: | 71 Applications should initialise Colorama using:: |
55 | 72 |
56 from colorama import init | 73 from colorama import init |
57 init() | 74 init() |
(...skipping 14 matching lines...) Expand all Loading... |
72 again. | 89 again. |
73 | 90 |
74 | 91 |
75 Colored Output | 92 Colored Output |
76 -------------- | 93 -------------- |
77 | 94 |
78 Cross-platform printing of colored text can then be done using Colorama's | 95 Cross-platform printing of colored text can then be done using Colorama's |
79 constant shorthand for ANSI escape sequences:: | 96 constant shorthand for ANSI escape sequences:: |
80 | 97 |
81 from colorama import Fore, Back, Style | 98 from colorama import Fore, Back, Style |
82 print Fore.RED + 'some red text' | 99 print(Fore.RED + 'some red text') |
83 print Back.GREEN + and with a green background' | 100 print(Back.GREEN + 'and with a green background') |
84 print Style.DIM + 'and in dim text' | 101 print(Style.DIM + 'and in dim text') |
85 print + Fore.RESET + Back.RESET + Style.RESET_ALL | 102 print(Fore.RESET + Back.RESET + Style.RESET_ALL) |
86 print 'back to normal now' | 103 print('back to normal now') |
87 | 104 |
88 or simply by manually printing ANSI sequences from your own code:: | 105 or simply by manually printing ANSI sequences from your own code:: |
89 | 106 |
90 print '/033[31m' + 'some red text' | 107 print('/033[31m' + 'some red text') |
91 print '/033[30m' # and reset to default color | 108 print('/033[30m' # and reset to default color) |
92 | 109 |
93 or Colorama can be used happily in conjunction with existing ANSI libraries | 110 or Colorama can be used happily in conjunction with existing ANSI libraries |
94 such as Termcolor:: | 111 such as Termcolor:: |
95 | 112 |
96 from colorama import init | 113 from colorama import init |
97 from termcolor import colored | 114 from termcolor import colored |
98 | 115 |
99 # use Colorama to make Termcolor work on Windows too | 116 # use Colorama to make Termcolor work on Windows too |
100 init() | 117 init() |
101 | 118 |
102 # then use Termcolor for all colored text output | 119 # then use Termcolor for all colored text output |
103 print colored('Hello, World!', 'green', 'on_red') | 120 print(colored('Hello, World!', 'green', 'on_red')) |
104 | 121 |
105 Available formatting constants are:: | 122 Available formatting constants are:: |
106 | 123 |
107 Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. | 124 Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. |
108 Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. | 125 Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. |
109 Style: DIM, NORMAL, BRIGHT, RESET_ALL | 126 Style: DIM, NORMAL, BRIGHT, RESET_ALL |
110 | 127 |
111 Style.RESET_ALL resets foreground, background and brightness. Colorama will | 128 Style.RESET_ALL resets foreground, background and brightness. Colorama will |
112 perform this reset automatically on program exit. | 129 perform this reset automatically on program exit. |
113 | 130 |
(...skipping 10 matching lines...) Expand all Loading... |
124 | 141 |
125 ``init()`` accepts some kwargs to override default behaviour. | 142 ``init()`` accepts some kwargs to override default behaviour. |
126 | 143 |
127 init(autoreset=False): | 144 init(autoreset=False): |
128 If you find yourself repeatedly sending reset sequences to turn off color | 145 If you find yourself repeatedly sending reset sequences to turn off color |
129 changes at the end of every print, then ``init(autoreset=True)`` will | 146 changes at the end of every print, then ``init(autoreset=True)`` will |
130 automate that:: | 147 automate that:: |
131 | 148 |
132 from colorama import init | 149 from colorama import init |
133 init(autoreset=True) | 150 init(autoreset=True) |
134 print Fore.RED + 'some red text' | 151 print(Fore.RED + 'some red text') |
135 print 'automatically back to default color again' | 152 print('automatically back to default color again') |
136 | 153 |
137 init(strip=None): | 154 init(strip=None): |
138 Pass ``True`` or ``False`` to override whether ansi codes should be | 155 Pass ``True`` or ``False`` to override whether ansi codes should be |
139 stripped from the output. The default behaviour is to strip if on Windows. | 156 stripped from the output. The default behaviour is to strip if on Windows. |
140 | 157 |
141 init(convert=None): | 158 init(convert=None): |
142 Pass ``True`` or ``False`` to override whether to convert ansi codes in the | 159 Pass ``True`` or ``False`` to override whether to convert ansi codes in the |
143 output into win32 calls. The default behaviour is to convert if on Windows | 160 output into win32 calls. The default behaviour is to convert if on Windows |
144 and output is to a tty (terminal). | 161 and output is to a tty (terminal). |
145 | 162 |
146 init(wrap=True): | 163 init(wrap=True): |
147 On Windows, colorama works by replacing ``sys.stdout`` and ``sys.stderr`` | 164 On Windows, colorama works by replacing ``sys.stdout`` and ``sys.stderr`` |
148 with proxy objects, which override the .write() method to do their work. If | 165 with proxy objects, which override the .write() method to do their work. If |
149 this wrapping causes you problems, then this can be disabled by passing | 166 this wrapping causes you problems, then this can be disabled by passing |
150 ``init(wrap=False)``. The default behaviour is to wrap if autoreset or | 167 ``init(wrap=False)``. The default behaviour is to wrap if autoreset or |
151 strip or convert are True. | 168 strip or convert are True. |
152 | 169 |
153 When wrapping is disabled, colored printing on non-Windows platforms will | 170 When wrapping is disabled, colored printing on non-Windows platforms will |
154 continue to work as normal. To do cross-platform colored output, you can | 171 continue to work as normal. To do cross-platform colored output, you can |
155 use Colorama's ``AnsiToWin32`` proxy directly:: | 172 use Colorama's ``AnsiToWin32`` proxy directly:: |
156 | 173 |
| 174 import sys |
157 from colorama import init, AnsiToWin32 | 175 from colorama import init, AnsiToWin32 |
158 init(wrap=False) | 176 init(wrap=False) |
159 stream = AnsiToWin32(sys.stderr).stream | 177 stream = AnsiToWin32(sys.stderr).stream |
160 print >>stream, Fore.BLUE + 'blue text on stderr' | 178 |
| 179 # Python 2 |
| 180 print >>stream, Fore.BLUE + 'blue text on stderr' |
| 181 |
| 182 # Python 3 |
| 183 print(Fore.BLUE + 'blue text on stderr', file=stream) |
161 | 184 |
162 | 185 |
163 Status & Known Problems | 186 Status & Known Problems |
164 ======================= | 187 ======================= |
165 | 188 |
166 I've personally only tested it on WinXP (CMD, Console2) and Ubuntu | 189 I've personally only tested it on WinXP (CMD, Console2), Ubuntu |
167 (gnome-terminal, xterm), although it sounds like others are using it on other | 190 (gnome-terminal, xterm), and OSX. |
168 platforms too. | 191 |
| 192 Some presumably valid ANSI sequences aren't recognised (see details below) |
| 193 but to my knowledge nobody has yet complained about this. Puzzling. |
169 | 194 |
170 See outstanding issues and wishlist at: | 195 See outstanding issues and wishlist at: |
171 http://code.google.com/p/colorama/issues/list | 196 http://code.google.com/p/colorama/issues/list |
172 | 197 |
173 If anything doesn't work for you, or doesn't do what you expected or hoped for, | 198 If anything doesn't work for you, or doesn't do what you expected or hoped for, |
174 I'd *love* to hear about it on that issues list. | 199 I'd love to hear about it on that issues list, would be delighted by patches, |
| 200 and would be happy to grant commit access to anyone who submits a working patch |
| 201 or two. |
175 | 202 |
176 | 203 |
177 Recognised ANSI Sequences | 204 Recognised ANSI Sequences |
178 ========================= | 205 ========================= |
179 | 206 |
180 ANSI sequences generally take the form: | 207 ANSI sequences generally take the form: |
181 | 208 |
182 ESC [ <param> ; <param> ... <command> | 209 ESC [ <param> ; <param> ... <command> |
183 | 210 |
184 Where <param> is an integer, and <command> is a single letter. Zero or more | 211 Where <param> is an integer, and <command> is a single letter. Zero or more |
185 params are passed to a <command>. If no params are passed, it is generally | 212 params are passed to a <command>. If no params are passed, it is generally |
186 synonymous with passing a single zero. No spaces exist in the sequence, they | 213 synonymous with passing a single zero. No spaces exist in the sequence, they |
187 have just been inserted here to make it easy to read. | 214 have just been inserted here to make it easy to read. |
188 | 215 |
189 The only ANSI sequences that colorama converts into win32 calls are:: | 216 The only ANSI sequences that colorama converts into win32 calls are:: |
190 | 217 |
191 ESC [ 0 m # reset all (colors and brightness) | 218 ESC [ 0 m # reset all (colors and brightness) |
192 ESC [ 1 m # bright | 219 ESC [ 1 m # bright |
193 ESC [ 2 m # dim (looks same as normal brightness) | 220 ESC [ 2 m # dim (looks same as normal brightness) |
194 ESC [ 22 m # normal brightness | 221 ESC [ 22 m # normal brightness |
(...skipping 14 matching lines...) Expand all Loading... |
209 ESC [ 41 m # red | 236 ESC [ 41 m # red |
210 ESC [ 42 m # green | 237 ESC [ 42 m # green |
211 ESC [ 43 m # yellow | 238 ESC [ 43 m # yellow |
212 ESC [ 44 m # blue | 239 ESC [ 44 m # blue |
213 ESC [ 45 m # magenta | 240 ESC [ 45 m # magenta |
214 ESC [ 46 m # cyan | 241 ESC [ 46 m # cyan |
215 ESC [ 47 m # white | 242 ESC [ 47 m # white |
216 ESC [ 49 m # reset | 243 ESC [ 49 m # reset |
217 | 244 |
218 # cursor positioning | 245 # cursor positioning |
219 ESC [ x;y H # position cursor at x,y | 246 ESC [ y;x H # position cursor at x across, y down |
220 | 247 |
221 # clear the screen | 248 # clear the screen |
222 ESC [ mode J # clear the screen. Only mode 2 (clear entire screen) | 249 ESC [ mode J # clear the screen. Only mode 2 (clear entire screen) |
223 # is supported. It should be easy to add other modes, | 250 # is supported. It should be easy to add other modes, |
224 # let me know if that would be useful. | 251 # let me know if that would be useful. |
225 | 252 |
226 Multiple numeric params to the 'm' command can be combined into a single | 253 Multiple numeric params to the 'm' command can be combined into a single |
227 sequence, eg:: | 254 sequence, eg:: |
228 | 255 |
229 ESC [ 36 ; 45 ; 1 m # bright cyan text on magenta background | 256 ESC [ 36 ; 45 ; 1 m # bright cyan text on magenta background |
230 | 257 |
231 All other ANSI sequences of the form ``ESC [ <param> ; <param> ... <command>`` | 258 All other ANSI sequences of the form ``ESC [ <param> ; <param> ... <command>`` |
232 are silently stripped from the output on Windows. | 259 are silently stripped from the output on Windows. |
233 | 260 |
234 Any other form of ANSI sequence, such as single-character codes or alternative | 261 Any other form of ANSI sequence, such as single-character codes or alternative |
235 initial characters, are not recognised nor stripped. It would be cool to add | 262 initial characters, are not recognised nor stripped. It would be cool to add |
236 them though. Let me know if it would be useful for you, via the issues on | 263 them though. Let me know if it would be useful for you, via the issues on |
237 google code. | 264 google code. |
238 | 265 |
239 | 266 |
240 Development | 267 Development |
241 =========== | 268 =========== |
242 | 269 |
| 270 Help and fixes welcome! Ask Jonathan for commit rights, you'll get them. |
| 271 |
243 Running tests requires: | 272 Running tests requires: |
244 | 273 |
245 - Michael Foord's 'mock' module to be installed. | 274 - Michael Foord's 'mock' module to be installed. |
246 - Tests are written using the 2010 era updates to 'unittest', and require to | 275 - Tests are written using the 2010 era updates to 'unittest', and require to |
247 be run either using Python2.7 or greater, or else to have Michael Foord's | 276 be run either using Python2.7 or greater, or else to have Michael Foord's |
248 'unittest2' module installed. | 277 'unittest2' module installed. |
249 | 278 |
250 unittest2 test discovery doesn't work for colorama, so I use 'nose':: | 279 unittest2 test discovery doesn't work for colorama, so I use 'nose':: |
251 | 280 |
252 nosetests -s | 281 nosetests -s |
253 | 282 |
254 The -s is required because 'nosetests' otherwise applies a proxy of its own to | 283 The -s is required because 'nosetests' otherwise applies a proxy of its own to |
255 stdout, which confuses the unit tests. | 284 stdout, which confuses the unit tests. |
256 | 285 |
257 | 286 |
| 287 Contact |
| 288 ======= |
| 289 |
| 290 Created by Jonathan Hartley, tartley@tartley.com |
| 291 |
| 292 |
258 Thanks | 293 Thanks |
259 ====== | 294 ====== |
260 Daniel Griffith for multiple fabulous patches. | 295 | Ben Hoyt, for a magnificent fix under 64-bit Windows. |
261 Oscar Lesta for valuable fix to stop ANSI chars being sent to non-tty output. | 296 | Jesse@EmptySquare for submitting a fix for examples in the README. |
262 Roger Binns, for many suggestions, valuable feedback, & bug reports. | 297 | User 'jamessp', an observant documentation fix for cursor positioning. |
263 Tim Golden for thought and much appreciated feedback on the initial idea. | 298 | User 'vaal1239', Dave Mckee & Lackner Kristof for a tiny but much-needed Win7
fix. |
| 299 | Julien Stuyck, for wisely suggesting Python3 compatible updates to README. |
| 300 | Daniel Griffith for multiple fabulous patches. |
| 301 | Oscar Lesta for valuable fix to stop ANSI chars being sent to non-tty output. |
| 302 | Roger Binns, for many suggestions, valuable feedback, & bug reports. |
| 303 | Tim Golden for thought and much appreciated feedback on the initial idea. |
264 | 304 |
OLD | NEW |