{ "info": { "author": "Timo Furrer", "author_email": "tuxtimo@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Environment :: Console", "License :: OSI Approved :: MIT License", "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft :: Windows", "Operating System :: POSIX", "Operating System :: POSIX :: Linux", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: Implementation", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy" ], "description": "# colorful\n\n[![Actions Status](https://github.com/timofurrer/colorful/workflows/Continuous%20Integration/badge.svg)](https://github.com/timofurrer/colorful/actions)\n[![codecov.io](https://codecov.io/github/timofurrer/colorful/coverage.svg?branch=master)](https://codecov.io/github/timofurrer/colorful?branch=master)\n[![PyPI version](https://badge.fury.io/py/colorful.svg)](https://badge.fury.io/py/colorful)\n[![PyPI](https://img.shields.io/pypi/pyversions/colorful.svg)](https://pypi.python.org/pypi/colorful)\n[![PyPI](https://img.shields.io/pypi/wheel/colorful.svg)](https://pypi.python.org/pypi/colorful)\n\nTerminal string styling done right, in Python :tada:\n\n## Here's a tease\n\n![colorful example](examples/basic_example.png)\n\n```python\nimport colorful as cf\n\n# create a colored string using clever method translation\nprint(cf.bold_white('Hello World'))\n# create a colored string using `str.format()`\nprint('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=cf))\n\n# nest colors\nprint(cf.red('red {0} red'.format(cf.white('white'))))\nprint(cf.red('red' + cf.white(' white ', nested=True) + 'red'))\n\n# combine styles with strings\nprint(cf.bold & cf.red | 'Hello World')\n\n# use true colors\ncf.use_true_colors()\n\n# extend default color palette\ncf.update_palette({'mint': '#c5e8c8'})\nprint(cf.mint_on_snow('Wow, this is actually mint'))\n\n# choose a predefined style\ncf.use_style('solarized')\n# print the official solarized colors\nprint(cf.yellow('yellow'), cf.orange('orange'),\n cf.red('red'), cf.magenta('magenta'),\n cf.violet('violet'), cf.blue('blue'),\n cf.cyan('cyan'), cf.green('green'))\n\n# directly print with colors\ncf.print('{c.bold_blue}Hello World{c.reset}')\n\n# choose specific color mode for one block\nwith cf.with_8_ansi_colors() as c:\n print(c.bold_green('colorful is awesome!'))\n\n# create and choose your own color palette\nMY_COMPANY_PALETTE = {\n 'companyOrange': '#f4b942',\n 'companyBaige': '#e8dcc5'\n}\nwith cf.with_palette(my_company_palette) as c:\n print(c.companyOrange_on_companyBaige('Thanks for choosing our product!'))\n\n# use f-string (only Python >= 3.6)\nprint(f'{cf.bold}Hello World')\n\n# support for chinese\nprint(cf.red('\u4f60\u597d'))\n```\n\n## Key Features\n\n* expressive and consistent API ([docs](#style-a-string))\n* support for different color modes (8 ANSI, 256 ANSI, true colors) ([docs](#color-modes))\n* support for predefined awesome styles (solarized, ...) ([docs](#styles))\n* support for custom color palettes ([docs](#color-palette))\n* support nesting styles ([docs](#nesting-styles))\n* support for different platforms (using colorama on Windows)\n* context managers for clean color mode, color palette or style switch ([docs](#temporarily-change-colorful-settings))\n* support `len()` on colored strings ([docs](#correctly-support-the-len-protocol))\n* support color names from [X11 rgb.txt](https://en.wikipedia.org/wiki/X11_color_names) ([docs](#1-style-a-string-with-a-method-call-colorfulmodifiers_fgcolor_on_bgcolorstr-nestedfalse))\n* no dependencies\n\n## Usage\n\n**colorful** supports all major Python versions: *2.7*, *3.4*, *3.5*, *3.6* and *3.7*.
\nWe recommend to use the latest version released on [PyPI](https://pypi.python.org/pypi/colorful):\n\n```bash\npip install colorful\n```\n\n**colorful** does not require any special setup in order to be used:\n\n```python\nimport colorful as cf\n\nprint(cf.italic_coral_on_beige('Hello World'))\nprint(cf.italic & cf.coral_on_beige | 'Hello World')\nprint('{c.italic_coral_on_beige}Hello World{c.reset}'.format(c=cf))\n```\n\nNote: the entire documentation assumes `colorful` to be imported as `cf`.\n\nSee the [Style a string](https://github.com/timofurrer/colorful#style-a-string) section for more information!\n\n### Color modes\n\nThese days terminals not only support the ancient 8 ANSI colors but often they support up to 16 Million colors with *[true color](https://en.wikipedia.org/wiki/Color_depth#True_color_.2824-bit.29)*. And if they don't support *true color* they might support the *[256 ANSI color palette](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors)* at least.\n\n**colorful** supports the following color modes:\n\n* no colors / disable (``cf.NO_COLORS``)\n* 8 colors -> 8 ANSI colors (``cf.ANSI_8_COLORS``)\n* 256 colors -> 256 ANSI color palette (8bit ``cf.ANSI_256_COLORS``)\n* 16'777'215 colors -> true color (24bit, ``cf.TRUE_COLORS``)\n\nBy default *colorful* tries to auto detect the best supported color mode by your terminal. Consult [`cf.terminal`](https://github.com/timofurrer/colorful/blob/master/colorful/terminal.py) for more details.\n\nHowever, sometimes it makes sense to specify what color mode should be used.
\n**colorful** provides multiple ways to do so:\n\n#### (1) specify color mode globally via Python API\n\n```python\ncf.disable()\ncf.use_8_ansi_colors()\ncf.use_256_ansi_colors()\ncf.use_true_colors()\n```\n\nIf you change the color mode during runtime it takes affect immediately and globally.\n\n#### (2) enforce color mode globally via environment variable\n\n```bash\nCOLORFUL_DISABLE=1 python eggs.py # this process will not use ANY coloring\nCOLORFUL_FORCE_8_COLORS=1 python eggs.py # this process will use 8 ANSI colors by default\nCOLORFUL_FORCE_256_COLORS=1 python eggs.py # this process will use 256 ANSI colors by default\nCOLORFUL_FORCE_TRUE_COLORS=1 python eggs.py # this process will use true colors by default\n```\n\n#### (3) specify color mode locally via Python API (contextmanager)\n\n```python\nwith cf.with_8_ansi_colors() as c:\n print(c.italic_coral_on_beige('Hello world'))\n\nwith cf.with_256_ansi_colors() as c:\n print(c.italic_coral_on_beige('Hello world'))\n\nwith cf.with_true_colors() as c:\n print(c.italic_coral_on_beige('Hello world'))\n```\n\n### Color palette\n\n**colorful**'s Python API is based on *color names* like in `cf.bold_white_on_black('Hello')`. During runtime these *color names* are translated into proper [ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code) sequences supported by the *color mode* in use. However, all *color names* are registered in a **color palette** which is basically a mapping between the *color names* and it's corresponding RGB value. Very much like this:\n\n```python\ncolor_palette_example = {\n 'black': '#000000',\n 'white': '#FFFFFF',\n}\n```\n\n*Note: Depending on the color mode which is used the RGB value will be reduced to fit in the value domain of the color mode.*\n\nThe default color palette is the [X11 rgb.txt](https://en.wikipedia.org/wiki/X11_color_names) palette - it's shipped with *colorful*, thus, you don't have to provide your own.\n*colorful* ships with a second built-in [color palette called *colornames*](https://codepen.io/meodai/full/VMpNdQ/).\nThose colors are from the curated list of the [color-names](https://github.com/meodai/color-names) repository.\nYou can use those via the `cf.setup()` method, like this:\n\n\n```python\ncf.setup(colorpalette=cf.COLORNAMES_COLORS)\n```\n\nIf you wish to have another color palette from a file as your default color palette you can set the `COLORFUL_DEFAULT_COLOR_PALETTE` environment variable to this file:\n\n```bash\nCOLORFUL_DEFAULT_COLOR_PALETTE=/usr/share/X11/rgb.txt python spam.py\n```\n\nThe file either has to be a txt file like the X11 rgb.txt or a JSON file:\n\n```json\n[\n {\"name\": \"18th Century Green\", \"hex\":\"#a59344\"},\n {\"name\": \"1975 Earth Red\", \"hex\":\"#7a463a\"}\n]\n```\n\n#### Custom color palette\n**colorful** supports to update or replace the default color palette with custom colors. The colors have to be specified as RGB hex or channel values:\n\n```python\n# corporate identity colors\nci_colors = {\n 'mint': '#c5e8c8', # RGB hex value\n 'darkRed': '#c11b55', # RGB hex value\n 'lightBlue': (15, 138, 191) # RGB channel triplet\n}\n\n# replace the default palette with my custom one\ncf.use_palette(ci_colors)\n# update the default palette with my custom one\ncf.update_palette(ci_colors)\n\n# we can use these colors\nprint(cf.italic_mint_on_darkRed('My company'))\n```\n\n### Styles\n\n**colorful** supports some famous color palettes using what's called *styles* in colorful:\n\n```python\ncf.use_style('solarized')\n\n# print the official solarized colors\nprint(cf.yellow('yellow'), cf.orange('orange'),\n cf.red('red'), cf.magenta('magenta'),\n cf.violet('violet'), cf.blue('blue'),\n cf.cyan('cyan'), cf.green('green'))\n```\n\nThe following styles are already supported:\n\n
\n solarized - Website\n
\n \"solarized\n
\n
\n monokai\n
\n \"monokai\n
\n
\n\n*Note: if you know some awesome color palettes which could be a new style in colorful, please contribute it!*\n\n### Style a string\n\n**colorful** provides multiple ways to use style a string. Most useful and expressive is probably the *method syntax* where you specify the modifiers and colors in the method name itself and pass the string as argument to this method. However, you can use all the following methods to achive similars things:\n\n#### (1) Style a string with a method call `cf.[]_[]_[on_](str, nested=False)`\n\n```python\nprint(cf.red('I am red'))\nprint(cf.italic_yellow('I am italic and yellow'))\nprint(cf.black_on_white('I am black on white'))\n```\n\nThe method syntax can be one of:\n\n* `cf.`\n* `cf._`\n* `cf.`\n* `cf.on_`\n* `cf._`\n* `cf._`\n* `cf._on_`\n* `cf.__on_`\n\n*Note that multiple ``s can be specified at once.*\n\nAvailable modifiers are:\n\n* reset (explicitely reset all styles before the passed argument)\n* bold\n* dimmed (not widely supported)\n* italic\n* underlined\n* blinkslow\n* blinkrapid\n* inversed (not widely supported)\n* concealed (not widely supported)\n* struckthrough\n\nThe available colors depend on the [color palette](#color-palette) you are using. By default all [X11 rgb.txt colors](https://en.wikipedia.org/wiki/X11_color_names) are available.\n\nThe type of the return value of such a *style method* is `colorful.ColorfulString`. It correctly supports all `str()` methods including [`len()`](#correctly-support-the-len-protocol).\n\nAs you can see from the syntax in the section name, **colorful** supports nesting styles. See [Nesting styles](#nesting-styles).\n\n#### (2) Style a string with `&` and `|`\n\n**colorful** implements the `__or__` and `__and__` protocol to combine styles and pipe strings into them:\n\n```python\nprint(cf.bold & cf.red | 'Hello World')\nprint(cf.bold_red_on_black | 'Hello World')\nprint(cf.bold | cf.red_on_black('Hello World')\n```\n\n*Note: the piping `|` has the same effect as doing a method call to the style.
\nSo you could do `(cf.bold & cf.red)('Hello World')`*\n\n#### (3) Style a string with `cf.format(string, *args, **kwargs)`\n\n```python\nprint(cf.format('{c.red}I am {what}{c.close_fg_color}', what='red'))\n# alternatively to ``c.close_fg_color`` you can reset every style with ``c.reset``\nprint(cf.format('{c.red}I am red{c.reset}'))\n\nprint(cf.format('{c.italic_yellow}I am italic and yellow{c.no_italic}{c.close_fg_color}'))\nprint(cf.format('{c.black_on_white}I am black on white{c.close_fg_color}{c.close_bg_color}'))\n```\n\n**colorful** will replace the `{c.