From 25a9a72dc1d4eb9e0f064ac5d431c5bc995ec548 Mon Sep 17 00:00:00 2001 From: CoprDistGit Date: Thu, 9 Mar 2023 02:18:49 +0000 Subject: automatic import of python-colorful --- .gitignore | 1 + python-colorful.spec | 1312 ++++++++++++++++++++++++++++++++++++++++++++++++++ sources | 1 + 3 files changed, 1314 insertions(+) create mode 100644 python-colorful.spec create mode 100644 sources diff --git a/.gitignore b/.gitignore index e69de29..b4487d0 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +/colorful-0.5.5.tar.gz diff --git a/python-colorful.spec b/python-colorful.spec new file mode 100644 index 0000000..65786cf --- /dev/null +++ b/python-colorful.spec @@ -0,0 +1,1312 @@ +%global _empty_manifest_terminate_build 0 +Name: python-colorful +Version: 0.5.5 +Release: 1 +Summary: Terminal string styling done right, in Python. +License: MIT License +URL: http://github.com/timofurrer/colorful +Source0: https://mirrors.nju.edu.cn/pypi/web/packages/e0/16/1ade0b33d17f86df7b7f214f33708ff9432239f94fc2ce2ba0b4c803c3b6/colorful-0.5.5.tar.gz +BuildArch: noarch + +Requires: python3-colorama + +%description +# colorful + +[![Actions Status](https://github.com/timofurrer/colorful/workflows/Continuous%20Integration/badge.svg)](https://github.com/timofurrer/colorful/actions) +[![codecov.io](https://codecov.io/github/timofurrer/colorful/coverage.svg?branch=master)](https://codecov.io/github/timofurrer/colorful?branch=master) +[![PyPI version](https://badge.fury.io/py/colorful.svg)](https://badge.fury.io/py/colorful) +[![PyPI](https://img.shields.io/pypi/pyversions/colorful.svg)](https://pypi.python.org/pypi/colorful) +[![PyPI](https://img.shields.io/pypi/wheel/colorful.svg)](https://pypi.python.org/pypi/colorful) + +Terminal string styling done right, in Python :tada: + +## Here's a tease + +![colorful example](examples/basic_example.png) + +```python +import colorful as cf + +# create a colored string using clever method translation +print(cf.bold_white('Hello World')) +# create a colored string using `str.format()` +print('{c.bold}{c.lightCoral_on_white}Hello World{c.reset}'.format(c=cf)) + +# nest colors +print(cf.red('red {0} red'.format(cf.white('white')))) +print(cf.red('red' + cf.white(' white ', nested=True) + 'red')) + +# combine styles with strings +print(cf.bold & cf.red | 'Hello World') + +# use true colors +cf.use_true_colors() + +# extend default color palette +cf.update_palette({'mint': '#c5e8c8'}) +print(cf.mint_on_snow('Wow, this is actually mint')) + +# choose a predefined style +cf.use_style('solarized') +# print the official solarized colors +print(cf.yellow('yellow'), cf.orange('orange'), + cf.red('red'), cf.magenta('magenta'), + cf.violet('violet'), cf.blue('blue'), + cf.cyan('cyan'), cf.green('green')) + +# directly print with colors +cf.print('{c.bold_blue}Hello World{c.reset}') + +# choose specific color mode for one block +with cf.with_8_ansi_colors() as c: + print(c.bold_green('colorful is awesome!')) + +# create and choose your own color palette +MY_COMPANY_PALETTE = { + 'companyOrange': '#f4b942', + 'companyBaige': '#e8dcc5' +} +with cf.with_palette(MY_COMPANY_PALETTE) as c: + print(c.companyOrange_on_companyBaige('Thanks for choosing our product!')) + +# use f-string (only Python >= 3.6) +print(f'{cf.bold}Hello World') + +# support for chinese +print(cf.red('你好')) +``` + +## Key Features + +* expressive and consistent API ([docs](#style-a-string)) +* support for different color modes (8 ANSI, 256 ANSI, true colors) ([docs](#color-modes)) +* support for predefined awesome styles (solarized, ...) ([docs](#styles)) +* support for custom color palettes ([docs](#color-palette)) +* support nesting styles ([docs](#nesting-styles)) +* support for different platforms (using colorama on Windows) +* context managers for clean color mode, color palette or style switch ([docs](#temporarily-change-colorful-settings)) +* support `len()` on colored strings ([docs](#correctly-support-the-len-protocol)) +* 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)) +* no dependencies + +## Usage + +**colorful** supports all major Python versions: *3.5*, *3.6* and *3.7*, *3.8*, *3.9*, *3.10*, *3.11*.
+We recommend to use the latest version released on [PyPI](https://pypi.python.org/pypi/colorful): + +```bash +pip install colorful +``` + +**colorful** does not require any special setup in order to be used: + +```python +import colorful as cf + +print(cf.italic_coral_on_beige('Hello World')) +print(cf.italic & cf.coral_on_beige | 'Hello World') +print('{c.italic_coral_on_beige}Hello World{c.reset}'.format(c=cf)) +``` + +Note: the entire documentation assumes `colorful` to be imported as `cf`. + +See the [Style a string](https://github.com/timofurrer/colorful#style-a-string) section for more information! + +### Color modes + +These 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. + +**colorful** supports the following color modes: + +* no colors / disable (``cf.NO_COLORS``) +* 8 colors -> 8 ANSI colors (``cf.ANSI_8_COLORS``) +* 256 colors -> 256 ANSI color palette (8bit ``cf.ANSI_256_COLORS``) +* 16'777'215 colors -> true color (24bit, ``cf.TRUE_COLORS``) + +By 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. + +However, sometimes it makes sense to specify what color mode should be used.
+**colorful** provides multiple ways to do so: + +#### (1) specify color mode globally via Python API + +```python +cf.disable() +cf.use_8_ansi_colors() +cf.use_256_ansi_colors() +cf.use_true_colors() +``` + +If you change the color mode during runtime it takes affect immediately and globally. + +#### (2) enforce color mode globally via environment variable + +```bash +COLORFUL_DISABLE=1 python eggs.py # this process will not use ANY coloring +COLORFUL_FORCE_8_COLORS=1 python eggs.py # this process will use 8 ANSI colors by default +COLORFUL_FORCE_256_COLORS=1 python eggs.py # this process will use 256 ANSI colors by default +COLORFUL_FORCE_TRUE_COLORS=1 python eggs.py # this process will use true colors by default +``` + +#### (3) specify color mode locally via Python API (contextmanager) + +```python +with cf.with_8_ansi_colors() as c: + print(c.italic_coral_on_beige('Hello world')) + +with cf.with_256_ansi_colors() as c: + print(c.italic_coral_on_beige('Hello world')) + +with cf.with_true_colors() as c: + print(c.italic_coral_on_beige('Hello world')) +``` + +### Color palette + +**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: + +```python +color_palette_example = { + 'black': '#000000', + 'white': '#FFFFFF', +} +``` + +*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.* + +The 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. +*colorful* ships with a second built-in [color palette called *colornames*](https://codepen.io/meodai/full/VMpNdQ/). +Those colors are from the curated list of the [color-names](https://github.com/meodai/color-names) repository. +You can use those via the `cf.setup()` method, like this: + + +```python +cf.setup(colorpalette=cf.COLORNAMES_COLORS) +``` + +If 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: + +```bash +COLORFUL_DEFAULT_COLOR_PALETTE=/usr/share/X11/rgb.txt python spam.py +``` + +The file either has to be a txt file like the X11 rgb.txt or a JSON file: + +```json +[ + {"name": "18th Century Green", "hex":"#a59344"}, + {"name": "1975 Earth Red", "hex":"#7a463a"} +] +``` + +#### Custom color palette +**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: + +```python +# corporate identity colors +ci_colors = { + 'mint': '#c5e8c8', # RGB hex value + 'darkRed': '#c11b55', # RGB hex value + 'lightBlue': (15, 138, 191) # RGB channel triplet +} + +# replace the default palette with my custom one +cf.use_palette(ci_colors) +# update the default palette with my custom one +cf.update_palette(ci_colors) + +# we can use these colors +print(cf.italic_mint_on_darkRed('My company')) +``` + +### Styles + +**colorful** supports some famous color palettes using what's called *styles* in colorful: + +```python +cf.use_style('solarized') + +# print the official solarized colors +print(cf.yellow('yellow'), cf.orange('orange'), + cf.red('red'), cf.magenta('magenta'), + cf.violet('violet'), cf.blue('blue'), + cf.cyan('cyan'), cf.green('green')) +``` + +The following styles are already supported: + +
+ solarized - Website +
+ solarized colors +
+
+ monokai +
+ monokai colors +
+
+ +*Note: if you know some awesome color palettes which could be a new style in colorful, please contribute it!* + +### Style a string + +**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: + +#### (1) Style a string with a method call `cf.[]_[]_[on_](str, nested=False)` + +```python +print(cf.red('I am red')) +print(cf.italic_yellow('I am italic and yellow')) +print(cf.black_on_white('I am black on white')) +``` + +The method syntax can be one of: + +* `cf.` +* `cf._` +* `cf.` +* `cf.on_` +* `cf._` +* `cf._` +* `cf._on_` +* `cf.__on_` + +*Note that multiple ``s can be specified at once.* + +Available modifiers are: + +* reset (explicitely reset all styles before the passed argument) +* bold +* dimmed (not widely supported) +* italic +* underlined +* blinkslow +* blinkrapid +* inversed (not widely supported) +* concealed (not widely supported) +* struckthrough + +The 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. + +The 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). + +As you can see from the syntax in the section name, **colorful** supports nesting styles. See [Nesting styles](#nesting-styles). + +#### (2) Style a string with `&` and `|` + +**colorful** implements the `__or__` and `__and__` protocol to combine styles and pipe strings into them: + +```python +print(cf.bold & cf.red | 'Hello World') +print(cf.bold_red_on_black | 'Hello World') +print(cf.bold | cf.red_on_black('Hello World') +``` + +*Note: the piping `|` has the same effect as doing a method call to the style.
+So you could do `(cf.bold & cf.red)('Hello World')`* + +#### (3) Style a string with `cf.format(string, *args, **kwargs)` + +```python +print(cf.format('{c.red}I am {what}{c.close_fg_color}', what='red')) +# alternatively to ``c.close_fg_color`` you can reset every style with ``c.reset`` +print(cf.format('{c.red}I am red{c.reset}')) + +print(cf.format('{c.italic_yellow}I am italic and yellow{c.no_italic}{c.close_fg_color}')) +print(cf.format('{c.black_on_white}I am black on white{c.close_fg_color}{c.close_bg_color}')) +``` + +**colorful** will replace the `{c.