zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    Java的日期类和日期格式化类
    Java中的内部类
    c#中的里氏转换和Java中强制类型转换在多态中的应用
    MySQL存储引擎
    如何控制多线程执行顺序
    为什么 1000==1000 返回为false,而 100==100 会返回为true
    HashMap和HashTable的区别
    打印昨天的当前时刻
    怎样将GB2312编码的字符串转换为ISO-8859-1编码的字符串?
    MyBatis中 # 和 $ 的区别?
  • 原文地址:https://www.cnblogs.com/lexus/p/2496401.html
Copyright © 2011-2022 走看看