root/cleverbox/trunk/cleverbox/utils/termcolors.py

Revision 260, 3.7 KB (checked in by trivoallan, 4 years ago)

cleverbox :

  • Added licence information to all project's file. Fixes #28
  • Fixed version number in O.4's setup.py
Line 
1# Copyright (c) 2005, the Lawrence Journal-World
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without modification,
5# are permitted provided that the following conditions are met:
6#
7#     1. Redistributions of source code must retain the above copyright notice,
8#        this list of conditions and the following disclaimer.
9#     
10#     2. Redistributions in binary form must reproduce the above copyright
11#        notice, this list of conditions and the following disclaimer in the
12#        documentation and/or other materials provided with the distribution.
13#
14#     3. Neither the name of Django nor the names of its contributors may be used
15#        to endorse or promote products derived from this software without
16#        specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29"""
30termcolors.py
31
32This code belongs to the Django project.
33"""
34
35color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
36foreground = dict([(color_names[x], '3%s' % x) for x in range(8)])
37background = dict([(color_names[x], '4%s' % x) for x in range(8)])
38del color_names
39
40RESET = '0'
41opt_dict = {'bold': '1', 'underscore': '4', 'blink': '5', 'reverse': '7', 'conceal': '8'}
42
43def colorize(text='', opts=(), **kwargs):
44    """
45    Returns your text, enclosed in ANSI graphics codes.
46
47    Depends on the keyword arguments 'fg' and 'bg', and the contents of
48    the opts tuple/list.
49
50    Returns the RESET code if no parameters are given.
51
52    Valid colors:
53        'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
54
55    Valid options:
56        'bold'
57        'underscore'
58        'blink'
59        'reverse'
60        'conceal'
61        'noreset' - string will not be auto-terminated with the RESET code
62
63    Examples:
64        colorize('hello', fg='red', bg='blue', opts=('blink',))
65        colorize()
66        colorize('goodbye', opts=('underscore',))
67        print colorize('first line', fg='red', opts=('noreset',))
68        print 'this should be red too'
69        print colorize('and so should this')
70        print 'this should not be red'
71    """
72    text = str(text)
73    code_list = []
74    if text == '' and len(opts) == 1 and opts[0] == 'reset':
75        return '\x1b[%sm' % RESET
76    for k, v in kwargs.iteritems():
77        if k == 'fg':
78            code_list.append(foreground[v])
79        elif k == 'bg':
80            code_list.append(background[v])
81    for o in opts:
82        if o in opt_dict:
83            code_list.append(opt_dict[o])
84    if 'noreset' not in opts:
85        text = text + '\x1b[%sm' % RESET
86    return ('\x1b[%sm' % ';'.join(code_list)) + text
87
88def make_style(opts=(), **kwargs):
89    """
90    Returns a function with default parameters for colorize()
91
92    Example:
93        bold_red = make_style(opts=('bold',), fg='red')
94        print bold_red('hello')
95        KEYWORD = make_style(fg='yellow')
96        COMMENT = make_style(fg='blue', opts=('bold',))
97    """
98    return lambda text: colorize(text, opts, **kwargs)
Note: See TracBrowser for help on using the browser.