author | David Anderson <david.jc.anderson@gmail.com> |
Fri, 13 Mar 2009 01:59:54 +0000 | |
changeset 1824 | c54c304e3c0e |
child 1825 | a610a2df83d2 |
permissions | -rw-r--r-- |
1824
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
1 |
# Copyright 2009 the Melange authors. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
2 |
# |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
4 |
# you may not use this file except in compliance with the License. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
5 |
# You may obtain a copy of the License at |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
6 |
# |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
8 |
# |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
9 |
# Unless required by applicable law or agreed to in writing, software |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
12 |
# See the License for the specific language governing permissions and |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
13 |
# limitations under the License. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
14 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
15 |
"""Various utilities. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
16 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
17 |
Current contents: |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
18 |
- Text colorization using ANSI color codes |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
19 |
""" |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
20 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
21 |
__authors__ = [ |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
22 |
# alphabetical order by last name, please |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
23 |
'"David Anderson" <dave@natulte.net>', |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
24 |
] |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
25 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
26 |
# The magic escape sequence understood by modern terminal emulators to |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
27 |
# configure fore/background colors and other basic text display |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
28 |
# settings. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
29 |
_ANSI_ESCAPE = '\x1b[%dm' |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
30 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
31 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
32 |
# Some intrnal non-color settings that we use. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
33 |
_RESET = 0 # Reset to terminal defaults. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
34 |
_BOLD = 1 # Brighter colors. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
35 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
36 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
37 |
# ANSI color codes. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
38 |
RED = 31 |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
39 |
GREEN = 32 |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
40 |
WHITE = 37 |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
41 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
42 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
43 |
def _ansi_escape(code): |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
44 |
return _ANSI_ESCAPE % code |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
45 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
46 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
47 |
def colorize(text, color, bold=False): |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
48 |
"""Colorize some text using ANSI color codes. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
49 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
50 |
Note that while ANSI color codes look good in a terminal they look |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
51 |
like noise in log files unless viewed in an ANSI color capable |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
52 |
viewer (such as 'less -R'). |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
53 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
54 |
Args: |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
55 |
text: The text to colorize. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
56 |
color: One of the color symbols from this module. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
57 |
bold: If True, make the color brighter. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
58 |
|
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
59 |
Returns: |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
60 |
The input text string, appropriately sprinkled with color |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
61 |
codes. Colors are reset to terminal defaults after the input |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
62 |
text. |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
63 |
""" |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
64 |
bold = _ansi_escape(_BOLD) if bold else '' |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
65 |
return '%s%s%s%s' % (bold, _ansi_escape(color), |
c54c304e3c0e
Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff
changeset
|
66 |
text, _ansi_escape(_RESET)) |