@ -2,17 +2,24 @@ Download and docs:
http://pypi.python.org/pypi/colorama
http://pypi.python.org/pypi/colorama
Development:
Development:
http://code.google.com/p/colorama
http://code.google.com/p/colorama
Discussion group:
https://groups.google.com/forum/#!forum/python-colorama
Description
Description
===========
===========
Makes ANSI escape character sequences, for producing colored terminal text and
Makes ANSI escape character sequences for producing colored terminal text and
cursor positioning, work under MS Windows.
cursor positioning work under MS Windows.
ANSI escape character sequences have long been used to produce colored terminal
ANSI escape character sequences have long been used to produce colored terminal
text and cursor positioning on Unix and Macs. Colorama makes this work on
text and cursor positioning on Unix and Macs. Colorama makes this work on
Windows, too. It also provides some shortcuts to help generate ANSI sequences,
Windows, too, by wrapping stdout, stripping ANSI sequences it finds (which
and works fine in conjunction with any other ANSI sequence generation library,
otherwise show up as gobbledygook in your output), and converting them into the
appropriate win32 calls to modify the state of the terminal. On other platforms,
Colorama does nothing.
Colorama also provides some shortcuts to help generate ANSI sequences
but works fine in conjunction with any other ANSI sequence generation library,
such as Termcolor (http://pypi.python.org/pypi/termcolor.)
such as Termcolor (http://pypi.python.org/pypi/termcolor.)
This has the upshot of providing a simple cross-platform API for printing
This has the upshot of providing a simple cross-platform API for printing
@ -21,6 +28,11 @@ applications or libraries which use ANSI sequences to produce colored output on
Linux or Macs can now also work on Windows, simply by calling
Linux or Macs can now also work on Windows, simply by calling
``colorama.init()``.
``colorama.init()``.
An alternative approach is to install 'ansi.sys' on Windows machines, which
provides the same behaviour for all applications running in terminals. Colorama
is intended for situations where that isn't easy (e.g. maybe your app doesn't
have an installer.)
Demo scripts in the source code repository prints some colored text using
Demo scripts in the source code repository prints some colored text using
ANSI sequences. Compare their output under Gnome-terminal's built in ANSI
ANSI sequences. Compare their output under Gnome-terminal's built in ANSI
handling, versus on Windows Command-Prompt using Colorama:
handling, versus on Windows Command-Prompt using Colorama:
@ -39,12 +51,17 @@ These screengrabs show that Colorama on Windows does not support ANSI 'dim
text': it looks the same as 'normal text'.
text': it looks the same as 'normal text'.
License
=======
Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
Dependencies
Dependencies
============
============
None, other than Python. Tested on Python 2.5.5, 2.6.5, 2.7, 3.1.2, and 3.2
None, other than Python. Tested on Python 2.5.5, 2.6.5, 2.7, 3.1.2, and 3.2
Usage
Usage
=====
=====
@ -79,16 +96,16 @@ Cross-platform printing of colored text can then be done using Colorama's
constant shorthand for ANSI escape sequences::
constant shorthand for ANSI escape sequences::
from colorama import Fore, Back, Style
from colorama import Fore, Back, Style
print Fore.RED + 'some red text'
print(Fore.RED + 'some red text')
print Back.GREEN + and with a green background'
print(Back.GREEN + 'and with a green background')
print Style.DIM + 'and in dim text'
print(Style.DIM + 'and in dim text')
print + Fore.RESET + Back.RESET + Style.RESET_ALL
print(Fore.RESET + Back.RESET + Style.RESET_ALL)
print 'back to normal now'
print('back to normal now')
or simply by manually printing ANSI sequences from your own code::
or simply by manually printing ANSI sequences from your own code::
print '/033[31m' + 'some red text'
print('/033[31m' + 'some red text')
print '/033[30m' # and reset to default color
print('/033[30m' # and reset to default color)
or Colorama can be used happily in conjunction with existing ANSI libraries
or Colorama can be used happily in conjunction with existing ANSI libraries
such as Termcolor::
such as Termcolor::
@ -100,7 +117,7 @@ such as Termcolor::
init()
init()
# then use Termcolor for all colored text output
# then use Termcolor for all colored text output
print colored('Hello, World!', 'green', 'on_red')
print( colored('Hello, World!', 'green', 'on_red'))
Available formatting constants are::
Available formatting constants are::
@ -131,8 +148,8 @@ init(autoreset=False):
from colorama import init
from colorama import init
init(autoreset=True)
init(autoreset=True)
print Fore.RED + 'some red text'
print(Fore.RED + 'some red text')
print 'automatically back to default color again'
print('automatically back to default color again')
init(strip=None):
init(strip=None):
Pass ``True`` or ``False`` to override whether ansi codes should be
Pass ``True`` or ``False`` to override whether ansi codes should be
@ -154,24 +171,34 @@ init(wrap=True):
continue to work as normal. To do cross-platform colored output, you can
continue to work as normal. To do cross-platform colored output, you can
use Colorama's ``AnsiToWin32`` proxy directly::
use Colorama's ``AnsiToWin32`` proxy directly::
import sys
from colorama import init, AnsiToWin32
from colorama import init, AnsiToWin32
init(wrap=False)
init(wrap=False)
stream = AnsiToWin32(sys.stderr).stream
stream = AnsiToWin32(sys.stderr).stream
# Python 2
print >>stream, Fore.BLUE + 'blue text on stderr'
print >>stream, Fore.BLUE + 'blue text on stderr'
# Python 3
print(Fore.BLUE + 'blue text on stderr', file=stream)
Status & Known Problems
Status & Known Problems
=======================
=======================
I've personally only tested it on WinXP (CMD, Console2) and Ubuntu
I've personally only tested it on WinXP (CMD, Console2), Ubuntu
(gnome-terminal, xterm), although it sounds like others are using it on other
(gnome-terminal, xterm), and OSX.
platforms too.
Some presumably valid ANSI sequences aren't recognised (see details below)
but to my knowledge nobody has yet complained about this. Puzzling.
See outstanding issues and wishlist at:
See outstanding issues and wishlist at:
http://code.google.com/p/colorama/issues/list
http://code.google.com/p/colorama/issues/list
If anything doesn't work for you, or doesn't do what you expected or hoped for,
If anything doesn't work for you, or doesn't do what you expected or hoped for,
I'd *love* to hear about it on that issues list.
I'd love to hear about it on that issues list, would be delighted by patches,
and would be happy to grant commit access to anyone who submits a working patch
or two.
Recognised ANSI Sequences
Recognised ANSI Sequences
@ -216,7 +243,7 @@ The only ANSI sequences that colorama converts into win32 calls are::
ESC [ 49 m # reset
ESC [ 49 m # reset
# cursor positioning
# cursor positioning
ESC [ x; y H # position cursor at x,y
ESC [ y;x H # position cursor at x across , y down
# clear the screen
# clear the screen
ESC [ mode J # clear the screen. Only mode 2 (clear entire screen)
ESC [ mode J # clear the screen. Only mode 2 (clear entire screen)
@ -240,6 +267,8 @@ google code.
Development
Development
===========
===========
Help and fixes welcome! Ask Jonathan for commit rights, you'll get them.
Running tests requires:
Running tests requires:
- Michael Foord's 'mock' module to be installed.
- Michael Foord's 'mock' module to be installed.
@ -255,10 +284,21 @@ The -s is required because 'nosetests' otherwise applies a proxy of its own to
stdout, which confuses the unit tests.
stdout, which confuses the unit tests.
Contact
=======
Created by Jonathan Hartley, tartley@tartley.com
Thanks
Thanks
======
======
Daniel Griffith for multiple fabulous patches.
| Ben Hoyt, for a magnificent fix under 64-bit Windows.
Oscar Lesta for valuable fix to stop ANSI chars being sent to non-tty output.
| Jesse@EmptySquare for submitting a fix for examples in the README.
Roger Binns, for many suggestions, valuable feedback, & bug reports.
| User 'jamessp', an observant documentation fix for cursor positioning.
Tim Golden for thought and much appreciated feedback on the initial idea.
| User 'vaal1239', Dave Mckee & Lackner Kristof for a tiny but much-needed Win7 fix.
| Julien Stuyck, for wisely suggesting Python3 compatible updates to README.
| Daniel Griffith for multiple fabulous patches.
| Oscar Lesta for valuable fix to stop ANSI chars being sent to non-tty output.
| Roger Binns, for many suggestions, valuable feedback, & bug reports.
| Tim Golden for thought and much appreciated feedback on the initial idea.