pycolib/ansicolor.py
author Radek Brich <radek.brich@devl.cz>
Mon, 19 Aug 2013 14:18:26 +0200
changeset 9 8d5a1affbb9d
parent 5 055f7dfb3e4f
permissions -rw-r--r--
Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
     3
# support Python 2.6+
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
     4
from __future__ import print_function
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
     5
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
     7
#: Set disabled = True to disable all colors.
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
     8
disabled = False
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
     9
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    11
#: Supported colors as enum and string names.
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    12
(BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, BOLD) = range(0,9)
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    13
names = ['black','red','green','yellow','blue','magenta','cyan','white','bold']
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    14
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
5
055f7dfb3e4f Refactor ansicolor, add demo.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    16
def highlight(enable, fg=None, bg=None):
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    17
    """Return ANSI escape code for selected color and font style.
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    18
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    19
    Set `enable` to true value to enable highlighting, false to disable/end highlighting.
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    20
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    21
    Set color and style using `fg` and `bg`. These parameters will accept
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    22
    string or variables defined in this module (BLACK, ...).
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    24
    Usage example::
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    26
        highlight(1) -- switch to bold, keep previous color
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    27
        highlight(1, 'bold red') -- bold red foreground
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    28
        highlight(1, BOLD | YELLOW, BLUE) -- yellow on blue, bold
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    29
        highlight(0) -- reset
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    30
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    31
    """
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
    global disabled
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
    if disabled:
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
        return ''
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
    if isinstance(fg, str):
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    37
        fg = sum(names.index(item.lower()) for item in fg.split())
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
    if isinstance(bg, str):
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    39
        bg = sum(names.index(item.lower()) for item in bg.split())
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
    if enable:
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
        code = '1'
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
        if fg is not None:
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
            code = ''
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
            if fg >= 8:
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
                fg -= 8
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
                code += '1;'
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
            code += str(30 + fg)
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
        if bg is not None:
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
            code += ';'
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
            if bg >= 8:
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
                bg -= 8
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
                code += '1;'
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
            code += str(40 + bg)
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
        return "\033[" + code + "m"
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
    else:
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
        return "\033[0m"
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    59
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    60
def hprint(*args, **kwargs):
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    61
    """Highlighted print.
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    62
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    63
    Usage is same as for print function.
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    64
    Whole output is colored as specified by `fg`, `bg` parameters.
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    65
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    66
    Usage example::
1
ee31f1bf17c1 Add ansicolor, prettysize, makeurl and README.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
9
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    68
        hprint('Hello in red!', fg='bold red')
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    69
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    70
    """
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    71
    fg = kwargs.pop('fg', None)
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    72
    bg = kwargs.pop('bg', None)
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    73
    end = kwargs.pop('end', '\n')
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    74
    print(highlight(1, fg, bg), end='')
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    75
    print(*args, end='', **kwargs)
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    76
    print(highlight(0), end=end)
8d5a1affbb9d Update ansicolor: Support Python2. Add more documentation. Update handling of string color names.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
    77