color.py 7.31 KB
Newer Older
1
2
3
##############################################################################
# Copyright (c) 2013, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
4
#
5
6
7
# This file is part of Spack.
# Written by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
8
#
9
10
# For details, see https://scalability-llnl.github.io/spack
# Please also see the LICENSE file for our notice and the LGPL.
11
#
12
13
14
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (as published by
# the Free Software Foundation) version 2.1 dated February 1999.
15
#
16
17
18
19
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU General Public License for more details.
20
#
21
22
23
24
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
This file implements an expression syntax, similar to printf, for adding
ANSI colors to text.

See colorize(), cwrite(), and cprint() for routines that can generate
colored output.

colorize will take a string and replace all color expressions with
ANSI control codes.  If the isatty keyword arg is set to False, then
the color expressions will be converted to null strings, and the
returned string will have no color.

cwrite and cprint are equivalent to write() and print() calls in
python, but they colorize their output.  If the stream argument is
not supplied, they write to sys.stdout.

Here are some example color expressions:

  @r         Turn on red coloring
  @R         Turn on bright red coloring
45
46
  @*{foo}    Bold foo, but don't change text color
  @_{bar}    Underline bar, but don't change text color
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
  @*b        Turn on bold, blue text
  @_B        Turn on bright blue text with an underline
  @.         Revert to plain formatting
  @*g{green} Print out 'green' in bold, green text, then reset to plain.
  @*ggreen@. Print out 'green' in bold, green text, then reset to plain.

The syntax consists of:

  color-expr    = '@' [style] color-code '{' text '}' | '@.' | '@@'
  style         = '*' | '_'
  color-code    = [krgybmcwKRGYBMCW]
  text          = .*

'@' indicates the start of a color expression.  It can be followed
by an optional * or _ that indicates whether the font should be bold or
underlined.  If * or _ is not provided, the text will be plain.  Then
an optional color code is supplied.  This can be [krgybmcw] or [KRGYBMCW],
where the letters map to  black(k), red(r), green(g), yellow(y), blue(b),
magenta(m), cyan(c), and white(w).  Lowercase letters denote normal ANSI
colors and capital letters denote bright ANSI colors.

Finally, the color expression can be followed by text enclosed in {}.  If
braces are present, only the text in braces is colored.  If the braces are
NOT present, then just the control codes to enable the color will be output.
The console can be reset later to plain text with '@.'.

To output an @, use '@@'.  To output a } inside braces, use '}}'.
"""
import re
import sys

78
class ColorParseError(Exception):
79
80
81
82
83
    """Raised when a color format fails to parse."""
    def __init__(self, message):
        super(ColorParseError, self).__init__(message)

# Text styles for ansi codes
84
85
86
styles = {'*'  : '1',       # bold
          '_'  : '4',       # underline
          None : '0' }      # plain
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

# Dim and bright ansi colors
colors = {'k' : 30, 'K' : 90,  # black
          'r' : 31, 'R' : 91,  # red
          'g' : 32, 'G' : 92,  # green
          'y' : 33, 'Y' : 93,  # yellow
          'b' : 34, 'B' : 94,  # blue
          'm' : 35, 'M' : 95,  # magenta
          'c' : 36, 'C' : 96,  # cyan
          'w' : 37, 'W' : 97 } # white

# Regex to be used for color formatting
color_re = r'@(?:@|\.|([*_])?([a-zA-Z])?(?:{((?:[^}]|}})*)})?)'


102
103
104
105
# Force color even if stdout is not a tty.
_force_color = False


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class match_to_ansi(object):
    def __init__(self, color=True):
        self.color = color

    def escape(self, s):
        """Returns a TTY escape sequence for a color"""
        if self.color:
            return "\033[%sm" % s
        else:
            return ''

    def __call__(self, match):
        """Convert a match object generated by color_re into an ansi color code
           This can be used as a handler in re.sub.
        """
        style, color, text = match.groups()
        m = match.group(0)

        if m == '@@':
            return '@'
        elif m == '@.':
            return self.escape(0)
128
        elif m == '@':
129
130
            raise ColorParseError("Incomplete color format: '%s' in %s"
                                  % (m, match.string))
131
132
133
134
135
136
137

        string = styles[style]
        if color:
            if color not in colors:
                raise ColorParseError("invalid color specifier: '%s' in '%s'"
                                      % (color, match.string))
            string += ';' + str(colors[color])
138
139
140
141
142

        colored_text = ''
        if text:
            colored_text = text + self.escape(0)

143
        return self.escape(string) + colored_text
144
145
146
147
148
149
150
151
152
153
154
155


def colorize(string, **kwargs):
    """Take a string and replace all color expressions with ANSI control
       codes.  Return the resulting string.
       If color=False is supplied, output will be plain text without
       control codes, for output to non-console devices.
    """
    color = kwargs.get('color', True)
    return re.sub(color_re, match_to_ansi(color), string)


156
157
158
159
160
def clen(string):
    """Return the length of a string, excluding ansi color sequences."""
    return len(re.sub(r'\033[^m]*m', '', string))


161
162
163
164
165
def cextra(string):
    """"Length of extra color characters in a string"""
    return len(''.join(re.findall(r'\033[^m]*m', string)))


166
167
168
169
170
171
172
def cwrite(string, stream=sys.stdout, color=None):
    """Replace all color expressions in string with ANSI control
       codes and write the result to the stream.  If color is
       False, this will write plain text with o color.  If True,
       then it will always write colored output.  If not supplied,
       then it will be set based on stream.isatty().
    """
173
    if color is None:
174
        color = stream.isatty() or _force_color
175
176
177
178
179
180
181
    stream.write(colorize(string, color=color))


def cprint(string, stream=sys.stdout, color=None):
    """Same as cwrite, but writes a trailing newline to the stream."""
    cwrite(string + "\n", stream, color)

182
183
184
185
def cescape(string):
    """Replace all @ with @@ in the string provided."""
    return str(string).replace('@', '@@')

186
187
188

class ColorStream(object):
    def __init__(self, stream, color=None):
Todd Gamblin's avatar
Todd Gamblin committed
189
190
        self._stream = stream
        self._color = color
191
192

    def write(self, string, **kwargs):
Todd Gamblin's avatar
Todd Gamblin committed
193
194
195
196
197
198
199
200
        raw = kwargs.get('raw', False)
        raw_write = getattr(self._stream, 'write')

        color = self._color
        if self._color is None:
            if raw:
                color=True
            else:
201
                color = self._stream.isatty() or _force_color
Todd Gamblin's avatar
Todd Gamblin committed
202
        raw_write(colorize(string, color=color))
203
204
205
206
207

    def writelines(self, sequence, **kwargs):
        raw = kwargs.get('raw', False)
        for string in sequence:
            self.write(string, self.color, raw=raw)