Skip to main content

A library to produce ansi color output and colored highlighting and diffing

Project description

https://pypip.in/v/ansicolor/badge.png https://travis-ci.org/numerodix/ansicolor.png?branch=master https://pypip.in/wheel/ansicolor/badge.png

Python version support: CPython 2.6, 2.7, 3.2, 3.3 and PyPy.

Introduction

ansicolor is a library that makes it easy to use ansi color markup in command line programs.

Installation

$ pip install ansicolor

Getting started

To get some simple color output:

from ansicolor import green
from ansicolor import red
from ansicolor import white

print("Let's try two colors: %s and %s!" % (red("red"), green("green")))
print("It's also easy to produce text in %s," % (red("bold", bold=True)))
print("...%s," % (green("reverse", reverse=True)))
print("...and %s." % (cyan("bold and reverse", bold=True, reverse=True)))

This will emit ansi escapes into the string: one when starting a color, another to reset the color back to the default:

>>> from ansicolor import green

>>> green("green")
'\x1b[0;0;32mgreen\x1b[0;0m'

If I want to be able to pass a color as an argument I can also use the colorize function:

from ansicolor import Colors
from ansicolor import colorize

print(colorize("I'm blue", Colors.Blue))

I can also apply color on a portion of a string:

from ansicolor import Colors
from ansicolor import wrap_string

print(wrap_string("I'm blue, said the policeman.", 8, Colors.Blue))

Sometimes I may have a string that contains markup and I’ll want to do something with it that concerns only the text, so I can strip the markup:

>>> from ansicolor import red
>>> from ansicolor import strip_escapes
>>> from ansicolor import yellow

>>> message = "My favorite colors are %s and %s" % (yellow("yellow"), red("red"))
>>> print("The length of this string is not: %d" % len(message))
The length of this string is not: 67
>>> print("The length of this string is: %d" % len(strip_escapes(message)))
The length of this string is: 37

Going further

Take a look at the demos to see what’s possible.

$ python -m ansicolor.demos --color
$ python -m ansicolor.demos --highlight
$ python -m ansicolor.demos --diff

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ansicolor-0.0.4.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

ansicolor-0.0.4-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ansicolor-0.0.4.tar.gz.

File metadata

  • Download URL: ansicolor-0.0.4.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ansicolor-0.0.4.tar.gz
Algorithm Hash digest
SHA256 be519a8396bfa00a20552790c74f6b2f917438fcb4d6fd6c0ad40f1e256032a5
MD5 e0ff8d1d971bc7ffd7fe6cec727bd73a
BLAKE2b-256 a68f4afb920d7565ea76cb28c12171baccc97b34be8c674dd2138a8efd943de7

See more details on using hashes here.

File details

Details for the file ansicolor-0.0.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ansicolor-0.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b3c4c11275710f70c565dd113ce2b417f777077bc2f7d60f6e5002dc27e3b31e
MD5 494bec0830a4646cc1a16fbb875ccc2d
BLAKE2b-256 d93f4958f91b165c26203eb4e25453bb6bca9fb306e4f6fab94354e9f010a8a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page