Skip to main content

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

Project description

ansicolor
=========

.. image:: https://pypip.in/v/ansicolor/badge.png
:target: https://pypi.python.org/pypi/ansicolor/

.. image:: https://travis-ci.org/numerodix/ansicolor.png?branch=master
:target: https://travis-ci.org/numerodix/ansicolor

.. image:: https://pypip.in/wheel/ansicolor/badge.png
:target: https://pypi.python.org/pypi/ansicolor/

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
------------

.. code:: bash

$ pip install ansicolor


Getting started
---------------

To highlight using colors:

.. literalinclude:: snippets/getting_started_1.py

.. figure:: snippets/getting_started_1.png


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

.. code:: python

>>> 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:

.. code:: python

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:

.. code:: python

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:

.. code:: python

>>> 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.

.. code:: bash

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

Also see the `API documentation`_.


.. _`API documentation`: https://ansicolor.readthedocs.org/

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.1.5.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

ansicolor-0.1.5-py2.py3-none-any.whl (7.0 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for ansicolor-0.1.5.tar.gz
Algorithm Hash digest
SHA256 8e5e64ae35e1549f82d4c4df9a3215ed212e2ff5cd6240170af04f12d0dedb1a
MD5 fea00079a9383dbc1ac81ee24b6a61bb
BLAKE2b-256 e07ad657c72a44a6c89351338a73cc2135c75545ebce944b1ea6e2f4fbe7c352

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ansicolor-0.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 65d89dc5854c5c9905eac3e20f437d8074d96c8cd9a921c62cea5ee5c650c80c
MD5 493ad24e275479d11d90302f59b58753
BLAKE2b-256 e95383f21b956c868a859b873b3fb96c28f7babe642cbb000d2083815e335951

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