colorama 0.2.4 : Python Package Index

colorama 0.2.4 : Python Package Index

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.

posted on 2012-05-11 17:19  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/05/11/2496401.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值