ansicolors


Nameansicolors JSON
Version 1.1.8 PyPI version JSON
download
home_pagehttp://github.com/jonathaneunice/colors/
SummaryANSI colors for Python
upload_time2017-06-02 21:22:10
maintainer
docs_urlNone
authorJonathan Eunice
requires_python
licenseISC
keywords asni color style console
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            
| |travisci| |version| |versions| |impls| |wheel| |coverage| |br-coverage|

.. |travisci| image:: https://api.travis-ci.org/jonathaneunice/colors.svg
    :target: http://travis-ci.org/jonathaneunice/colors

.. |version| image:: http://img.shields.io/pypi/v/ansicolors.svg?style=flat
    :alt: PyPI Package latest release
    :target: https://pypi.python.org/pypi/ansicolors

.. |versions| image:: https://img.shields.io/pypi/pyversions/ansicolors.svg
    :alt: Supported versions
    :target: https://pypi.python.org/pypi/ansicolors

.. |impls| image:: https://img.shields.io/pypi/implementation/ansicolors.svg
    :alt: Supported implementations
    :target: https://pypi.python.org/pypi/ansicolors

.. |wheel| image:: https://img.shields.io/pypi/wheel/ansicolors.svg
    :alt: Wheel packaging support
    :target: https://pypi.python.org/pypi/ansicolors

.. |coverage| image:: https://img.shields.io/badge/test_coverage-100%25-6600CC.svg
    :alt: Test line coverage
    :target: https://pypi.python.org/pypi/ansicolors

.. |br-coverage| image:: https://img.shields.io/badge/branch_coverage-100%25-6600CC.svg
    :alt: Test branch coverage
    :target: https://pypi.python.org/pypi/ansicolors

ANSI colors for Python
======================

Add ANSI colors and decorations to your strings.

Example Usage
-------------

::

    from __future__ import print_function  # accomodate Python 2
    from colors import *

    print(color('my string', fg='blue'))
    print(color('some text', fg='red', bg='yellow', style='underline'))

The strings returned by ``color`` will have embedded
`ANSI code sequences <https://en.wikipedia.org/wiki/ANSI_escape_code>`_
stipulating text colors and styles. For example, the above
code will print the strings::

    '\x1b[34mmy string\x1b[0m'
    '\x1b[31;43;4msome text\x1b[0m'

You can choose the foreground (text) color with the ``fg`` parameter,
the background color with ``bg``, and the style with ``style``.

You can choose one of the 8 basic ANSI colors: ``black``, ``red``, ``green``,
``yellow``, ``blue``, ``magenta``, ``cyan``, and ``white``, plus a special
``default`` which is display-specific, but usually a rational "no special
color" setting.

There are other ways to specify colors. Many devices support
an idiosyncratic 256-color scheme developed as an extension to
the original ANSI codes for the
`xterm terminal emulator <https://en.wikipedia.org/wiki/Xterm>`_.
Colors (or grays) from this larger palette can be specified via ``int``
value (0-255).

To see them all::

    from __future__ import print_function
    from colors import color

    for i in range(256):
        print(color('Color #%d' % i, fg=i))


The included ``show_colors.py`` program is a much-expanded version of this idea
that can be used to explore available color and style combinations on your
terminal or output device.

24-bit Color and CSS Compatibility
----------------------------------

Modern terminals go even further than the ``xterm`` 256, often supporting a
full 24-bit RGB color scheme. You can provide a full RGB value several ways:

* with a 3-element ``tuple`` or ``list`` of ``int``, each valued 0 to 255 (e.g. ``(255, 218, 185)``),
* a string containing a CSS-compatible color name (e.g. ``'peachpuff'``),
* a string containing a CSS-style hex value (e.g. ``'#aaa'`` or ``'#8a2be2'``)
* a string containing a CSS-style RGB notation (e.g. ``'rgb(102,51,153)'``)

These forms can be mixed and matched at will::

    print(color('orange on gray', 'orange', 'gray'))
    print(color('nice color', 'white', '#8a2be2'))

Note that any color name defined in the basic ANSI color set takes
primacy over the CSS color names. Combined with the fact that
terminals do not always agree which precise tone of blue should
qualify as ANSI ``blue``, there can be some ambiguity regarding
the named colors. If you need full precision, specify the RGB
color exactly. The ``parse_rgb`` function can be used to identify
the correct definition according to the CSS standards.

Caveats
-------

Unfortunately there is no guarantee that every terminal will support all the
colors and styles ANSI ostensibly defines. In fact, most implement a rather
small subset. Colors are better supported than styles, for which you *might* get
one or two of the most popular such as ``bold`` or ``underline``.
*Might.*

Whatever colors and styles are supported, there is no guarantee they will be
accurately rendered. Even at this late date, over **fifty years** after the codes
began to be standardized, support from terminals and output devices is limited,
fragemented, and piecemeal.

ANSI codes evolved in an entirely different historical context from today's.
Both the Web and the idea of broad standardization were decades in the future.
Display technology was low-resolution, colors were limited on the rare occasions
they were present, and color/style fidelity was not a major consideration.
Vendors thought little or nothing of creating their own proprietary codes,
implementing functions differently from other vendors, and/or co-opting codes
previously in use for something else. Practical ANSI reference materials
include *many* phrases such as 'hardly ever supported' and 'non-standard.'

We still use ANSI codes today not because they're especially good, but because
they're the best, most-standard approach that pre-Web displays even remotely
agreed upon. Even deep into the Web era, text output endures as an important
means of human-computer interaction. The good news, such is it is: ANSI's color
and style specifications ("SGR" or "Select Graphic Rendition" in ANSI
terminology) are the most-used and best-adhered-to portion of the whole ANSI
show.

More Examples
-------------

::

    # use some partial functions

    from __future__ import print_function # so works on Python 2 and 3 alike
    from colors import red, green, blue

    print(red('This is red'))
    print(green('This is green'))
    print(blue('This is blue'))

Optionally you can add a background color and/or styles.::

    print(red('red on blue', bg='blue'))
    print(green('green on black', bg='black', style='underline'))

You can use multiple styles at once. Separate them with
a ``+``.::

    print(red('very important', style='bold+underline'))

You can additionally specify one of the supported styles: ``none``, ``bold``,
``faint``, ``italic``, ``underline``, ``blink``, ``blink2``, ``negative``,
``concealed``, ``crossed``. While most devices support only a few styles,
unsupported styles are generally ignored, so the only harm done is your text is
less pretty and/or formatted than you might like. A good general rule is
to enjoy the formatting if you get it, but don't depend on it--especially
don't depend on styles like ``blink`` (e.g. to highlight critical data) or
``concealed`` (e.g. to hide data). Most likely, they won't.

If you use a style often, you may want to create your own
named style::

    from functools import partial
    from colors import color

    important = partial(color, fg='red', style='bold+underline'))

    print(important('this is very important!'))

Utility Functions
-----------------

In deailing with ANSI-styled text, it can be necessary to determine the
"equivalent" text minus the styling. The function ``strip_color(s)`` does that,
removing ANSI codes from ``s``, returning its "plain text equivalent."

You may also wish to determine the effective length of a string. If it contains
ANSI codes, the builtin ``len()`` function will return the length including
those codes, even though they are logically 0-length. So plain ``len(s)`` is
probably not what you need. ``ansilen(s)`` in contrast returns the "effective"
length of the string, including only the non-ANSI characters. ``ansilen(s)`` is
equivalent to ``len(strip_color(s))``,

License
-------

``colors`` is licensed under the `ISC license <https://en.wikipedia.org/wiki/ISC_license>`_.

            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/jonathaneunice/colors/",
    "name": "ansicolors",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "ASNI color style console",
    "author": "Jonathan Eunice",
    "author_email": "jonathan.eunice@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/76/31/7faed52088732704523c259e24c26ce6f2f33fbeff2ff59274560c27628e/ansicolors-1.1.8.zip",
    "platform": "",
    "description": "\n| |travisci| |version| |versions| |impls| |wheel| |coverage| |br-coverage|\n\n.. |travisci| image:: https://api.travis-ci.org/jonathaneunice/colors.svg\n    :target: http://travis-ci.org/jonathaneunice/colors\n\n.. |version| image:: http://img.shields.io/pypi/v/ansicolors.svg?style=flat\n    :alt: PyPI Package latest release\n    :target: https://pypi.python.org/pypi/ansicolors\n\n.. |versions| image:: https://img.shields.io/pypi/pyversions/ansicolors.svg\n    :alt: Supported versions\n    :target: https://pypi.python.org/pypi/ansicolors\n\n.. |impls| image:: https://img.shields.io/pypi/implementation/ansicolors.svg\n    :alt: Supported implementations\n    :target: https://pypi.python.org/pypi/ansicolors\n\n.. |wheel| image:: https://img.shields.io/pypi/wheel/ansicolors.svg\n    :alt: Wheel packaging support\n    :target: https://pypi.python.org/pypi/ansicolors\n\n.. |coverage| image:: https://img.shields.io/badge/test_coverage-100%25-6600CC.svg\n    :alt: Test line coverage\n    :target: https://pypi.python.org/pypi/ansicolors\n\n.. |br-coverage| image:: https://img.shields.io/badge/branch_coverage-100%25-6600CC.svg\n    :alt: Test branch coverage\n    :target: https://pypi.python.org/pypi/ansicolors\n\nANSI colors for Python\n======================\n\nAdd ANSI colors and decorations to your strings.\n\nExample Usage\n-------------\n\n::\n\n    from __future__ import print_function  # accomodate Python 2\n    from colors import *\n\n    print(color('my string', fg='blue'))\n    print(color('some text', fg='red', bg='yellow', style='underline'))\n\nThe strings returned by ``color`` will have embedded\n`ANSI code sequences <https://en.wikipedia.org/wiki/ANSI_escape_code>`_\nstipulating text colors and styles. For example, the above\ncode will print the strings::\n\n    '\\x1b[34mmy string\\x1b[0m'\n    '\\x1b[31;43;4msome text\\x1b[0m'\n\nYou can choose the foreground (text) color with the ``fg`` parameter,\nthe background color with ``bg``, and the style with ``style``.\n\nYou can choose one of the 8 basic ANSI colors: ``black``, ``red``, ``green``,\n``yellow``, ``blue``, ``magenta``, ``cyan``, and ``white``, plus a special\n``default`` which is display-specific, but usually a rational \"no special\ncolor\" setting.\n\nThere are other ways to specify colors. Many devices support\nan idiosyncratic 256-color scheme developed as an extension to\nthe original ANSI codes for the\n`xterm terminal emulator <https://en.wikipedia.org/wiki/Xterm>`_.\nColors (or grays) from this larger palette can be specified via ``int``\nvalue (0-255).\n\nTo see them all::\n\n    from __future__ import print_function\n    from colors import color\n\n    for i in range(256):\n        print(color('Color #%d' % i, fg=i))\n\n\nThe included ``show_colors.py`` program is a much-expanded version of this idea\nthat can be used to explore available color and style combinations on your\nterminal or output device.\n\n24-bit Color and CSS Compatibility\n----------------------------------\n\nModern terminals go even further than the ``xterm`` 256, often supporting a\nfull 24-bit RGB color scheme. You can provide a full RGB value several ways:\n\n* with a 3-element ``tuple`` or ``list`` of ``int``, each valued 0 to 255 (e.g. ``(255, 218, 185)``),\n* a string containing a CSS-compatible color name (e.g. ``'peachpuff'``),\n* a string containing a CSS-style hex value (e.g. ``'#aaa'`` or ``'#8a2be2'``)\n* a string containing a CSS-style RGB notation (e.g. ``'rgb(102,51,153)'``)\n\nThese forms can be mixed and matched at will::\n\n    print(color('orange on gray', 'orange', 'gray'))\n    print(color('nice color', 'white', '#8a2be2'))\n\nNote that any color name defined in the basic ANSI color set takes\nprimacy over the CSS color names. Combined with the fact that\nterminals do not always agree which precise tone of blue should\nqualify as ANSI ``blue``, there can be some ambiguity regarding\nthe named colors. If you need full precision, specify the RGB\ncolor exactly. The ``parse_rgb`` function can be used to identify\nthe correct definition according to the CSS standards.\n\nCaveats\n-------\n\nUnfortunately there is no guarantee that every terminal will support all the\ncolors and styles ANSI ostensibly defines. In fact, most implement a rather\nsmall subset. Colors are better supported than styles, for which you *might* get\none or two of the most popular such as ``bold`` or ``underline``.\n*Might.*\n\nWhatever colors and styles are supported, there is no guarantee they will be\naccurately rendered. Even at this late date, over **fifty years** after the codes\nbegan to be standardized, support from terminals and output devices is limited,\nfragemented, and piecemeal.\n\nANSI codes evolved in an entirely different historical context from today's.\nBoth the Web and the idea of broad standardization were decades in the future.\nDisplay technology was low-resolution, colors were limited on the rare occasions\nthey were present, and color/style fidelity was not a major consideration.\nVendors thought little or nothing of creating their own proprietary codes,\nimplementing functions differently from other vendors, and/or co-opting codes\npreviously in use for something else. Practical ANSI reference materials\ninclude *many* phrases such as 'hardly ever supported' and 'non-standard.'\n\nWe still use ANSI codes today not because they're especially good, but because\nthey're the best, most-standard approach that pre-Web displays even remotely\nagreed upon. Even deep into the Web era, text output endures as an important\nmeans of human-computer interaction. The good news, such is it is: ANSI's color\nand style specifications (\"SGR\" or \"Select Graphic Rendition\" in ANSI\nterminology) are the most-used and best-adhered-to portion of the whole ANSI\nshow.\n\nMore Examples\n-------------\n\n::\n\n    # use some partial functions\n\n    from __future__ import print_function # so works on Python 2 and 3 alike\n    from colors import red, green, blue\n\n    print(red('This is red'))\n    print(green('This is green'))\n    print(blue('This is blue'))\n\nOptionally you can add a background color and/or styles.::\n\n    print(red('red on blue', bg='blue'))\n    print(green('green on black', bg='black', style='underline'))\n\nYou can use multiple styles at once. Separate them with\na ``+``.::\n\n    print(red('very important', style='bold+underline'))\n\nYou can additionally specify one of the supported styles: ``none``, ``bold``,\n``faint``, ``italic``, ``underline``, ``blink``, ``blink2``, ``negative``,\n``concealed``, ``crossed``. While most devices support only a few styles,\nunsupported styles are generally ignored, so the only harm done is your text is\nless pretty and/or formatted than you might like. A good general rule is\nto enjoy the formatting if you get it, but don't depend on it--especially\ndon't depend on styles like ``blink`` (e.g. to highlight critical data) or\n``concealed`` (e.g. to hide data). Most likely, they won't.\n\nIf you use a style often, you may want to create your own\nnamed style::\n\n    from functools import partial\n    from colors import color\n\n    important = partial(color, fg='red', style='bold+underline'))\n\n    print(important('this is very important!'))\n\nUtility Functions\n-----------------\n\nIn deailing with ANSI-styled text, it can be necessary to determine the\n\"equivalent\" text minus the styling. The function ``strip_color(s)`` does that,\nremoving ANSI codes from ``s``, returning its \"plain text equivalent.\"\n\nYou may also wish to determine the effective length of a string. If it contains\nANSI codes, the builtin ``len()`` function will return the length including\nthose codes, even though they are logically 0-length. So plain ``len(s)`` is\nprobably not what you need. ``ansilen(s)`` in contrast returns the \"effective\"\nlength of the string, including only the non-ANSI characters. ``ansilen(s)`` is\nequivalent to ``len(strip_color(s))``,\n\nLicense\n-------\n\n``colors`` is licensed under the `ISC license <https://en.wikipedia.org/wiki/ISC_license>`_.\n",
    "bugtrack_url": null,
    "license": "ISC",
    "summary": "ANSI colors for Python",
    "version": "1.1.8",
    "split_keywords": [
        "asni",
        "color",
        "style",
        "console"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "f357aa02db2466bc24ff1815cff1aeb3",
                "sha256": "00d2dde5a675579325902536738dd27e4fac1fd68f773fe36c21044eb559e187"
            },
            "downloads": -1,
            "filename": "ansicolors-1.1.8-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f357aa02db2466bc24ff1815cff1aeb3",
            "packagetype": "bdist_wheel",
            "python_version": "3.6",
            "requires_python": null,
            "size": 13847,
            "upload_time": "2017-06-02T21:22:12",
            "upload_time_iso_8601": "2017-06-02T21:22:12.670271Z",
            "url": "https://files.pythonhosted.org/packages/53/18/a56e2fe47b259bb52201093a3a9d4a32014f9d85071ad07e9d60600890ca/ansicolors-1.1.8-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "9ca7e2396ffa2e20af023c6b83ab7b14",
                "sha256": "99f94f5e3348a0bcd43c82e5fc4414013ccc19d70bd939ad71e0133ce9c372e0"
            },
            "downloads": -1,
            "filename": "ansicolors-1.1.8.zip",
            "has_sig": false,
            "md5_digest": "9ca7e2396ffa2e20af023c6b83ab7b14",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 23027,
            "upload_time": "2017-06-02T21:22:10",
            "upload_time_iso_8601": "2017-06-02T21:22:10.729545Z",
            "url": "https://files.pythonhosted.org/packages/76/31/7faed52088732704523c259e24c26ce6f2f33fbeff2ff59274560c27628e/ansicolors-1.1.8.zip",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2017-06-02 21:22:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "jonathaneunice",
    "github_project": "colors",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": false,
    "tox": true,
    "lcname": "ansicolors"
}
        
Elapsed time: 0.01773s