colorama 0.2.4

Cross-platform colored terminal text.

Downloads ↓

Download and docs:
http://pypi.python.org/pypi/colorama
Development:
http://code.google.com/p/colorama

Description

Makes ANSI escape character sequences, for producing colored terminal text and
cursor positioning, work under MS Windows.

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
Windows, too. It also provides some shortcuts to help generate ANSI sequences,
and works fine in conjunction with any other ANSI sequence generation library,
such as Termcolor (http://pypi.python.org/pypi/termcolor.)

This has the upshot of providing a simple cross-platform API for printing
colored terminal text from Python, and has the happy side-effect that existing
applications or libraries which use ANSI sequences to produce colored output on
Linux or Macs can now also work on Windows, simply by calling
colorama.init().

Demo scripts in the source code repository prints some colored text using
ANSI sequences. Compare their output under Gnome-terminal's built in ANSI
handling, versus on Windows Command-Prompt using Colorama:

ANSI sequences on Ubuntu under gnome-terminal.
Same ANSI sequences on Windows, using Colorama.

These screengrabs show that Colorama on Windows does not support ANSI 'dim
text': it looks the same as 'normal text'.

Dependencies

None, other than Python. Tested on Python 2.5.5, 2.6.5, 2.7, 3.1.2, and 3.2

Usage

Initialisation

Applications should initialise Colorama using:

from colorama import init
init()

If you are on Windows, the call to init() will start filtering ANSI escape
sequences out of any text sent to stdout or stderr, and will replace them with
equivalent Win32 calls.

Calling init() has no effect on other platforms (unless you request other
optional functionality, see keyword args below.) The intention is that
applications can call init() unconditionally on all platforms, after which
ANSI output should just work.

To stop using colorama before your program exits, simply call deinit().
This will restore stdout and stderr to their original values, so that Colorama
is disabled. To start using Colorama again, call reinit(), which wraps
stdout and stderr again, but is cheaper to call than doing init() all over
again.

Colored Output

Cross-platform printing of colored text can then be done using Colorama's
constant shorthand for ANSI escape sequences:

from colorama import Fore, Back, Style
print Fore.RED + 'some red text'
print Back.GREEN + and with a green background'
print Style.DIM + 'and in dim text'
print + Fore.RESET + Back.RESET + Style.RESET_ALL
print 'back to normal now'

or simply by manually printing ANSI sequences from your own code:

print '/033[31m' + 'some red text'
print '/033[30m' # and reset to default color

or Colorama can be used happily in conjunction with existing ANSI libraries
such as Termcolor:

from colorama import init
from termcolor import colored
# use Colorama to make Termcolor work on Windows too
init()
# then use Termcolor for all colored text output
print colored('Hello, World!', 'green', 'on_red')

Available formatting constants are:

Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Style: DIM, NORMAL, BRIGHT, RESET_ALL

Style.RESET_ALL resets foreground, background and brightness. Colorama will
perform this reset automatically on program exit.

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。