summaryrefslogtreecommitdiffstats
path: root/README.md
blob: 83f704e8ad4612dfe01e87cbca3fdf36f9be61f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# colorclass

Yet another ANSI color text library for Python. Provides "auto colors" for dark/light terminals. Works on Linux, OS X,
and Windows. For Windows support you just need to call ``Windows.enable()`` in your application.

On Linux/OS X ``autocolors`` are toggled by calling ``set_light_background()`` and ``set_dark_background()``. On Windows
this can be done automatically if you call ``Windows.enable(auto_colors=True)``. Even though the latest Windows 10 does
support ANSI color codes natively, you still need to run Windows.enable() to take advantage of automatically detecting
the console's background color.

In Python2.x this library subclasses ``unicode``, while on Python3.x it subclasses ``str``.

* Python 2.6, 2.7, PyPy, PyPy3, 3.3, 3.4, and 3.5 supported on Linux and OS X.
* Python 2.6, 2.7, 3.3, 3.4, and 3.5 supported on Windows (both 32 and 64 bit versions of Python).

## Quickstart
Install:
```bash
    pip install colorclass
```

## Piped Command Line
It is possible to pipe curly-bracket tagged (or regular ANSI coded) text to Python in the command line to produce color
text. Some examples:

```bash
    echo "{red}Red{/red}" |python -m colorclass  # Red colored text.
    echo -e "\033[31mRed\033[0m" | COLOR_DISABLE=true python -m colorclass  # Strip colors
    echo -e "\033[31mRed\033[0m" | COLOR_ENABLE=true python -m colorclass &> file.txt  # Force colors.
```
Export these environment variables as "true" to enable/disable some features:

    =============== ============================================
    Env Variable    Description
    =============== ============================================
    COLOR_ENABLE    Force colors even when piping to a file.
    COLOR_DISABLE   Strip all colors from incoming text.
    COLOR_LIGHT     Use light colored text for dark backgrounds.
    COLOR_DARK      Use dark colored text for light backgrounds.
    =============== ============================================

## Example Implementation

![Example Script Screenshot](https://github.com/Robpol86/colorclass/raw/master/example.png?raw=true)

![Example Windows Screenshot](https://github.com/Robpol86/colorclass/raw/master/example_windows.png?raw=true)

Source code for the example code is: [example.py](https://github.com/Robpol86/colorclass/blob/master/example.py)

## Usage

Different colors are chosen using curly-bracket tags, such as ``{red}{/red}``. For a list of available colors, call
``colorclass.list_tags()``.

The available "auto colors" tags are:

* autoblack
* autored
* autogreen
* autoyellow
* autoblue
* automagenta
* autocyan
* autowhite
* autobgblack
* autobgred
* autobggreen
* autobgyellow
* autobgblue
* autobgmagenta
* autobgcyan
* autobgwhite

Methods of Class instances try to return sane data, such as:

```python
    from colorclass import Color
    color_string = Color('{red}Test{/red}')

    >>> color_string
    u'\x1b[31mTest\x1b[39m'

    >>> len(color_string)
    4

    >>> color_string.istitle()
    True
```
There are also a couple of helper attributes for all Color instances:

```python
    >>> color_string.value_colors
    '\x1b[31mTest\x1b[39m'

    >>> color_string.value_no_colors
    'Test'
```

[Change Log](https://github.com/matthewdeanmartin/colorclass/blob/master/CHANGELOG.md)