Refactor ANSI colorization into a new utility module.
authorDavid Anderson <david.jc.anderson@gmail.com>
Fri, 13 Mar 2009 01:59:54 +0000
changeset 1824 c54c304e3c0e
parent 1823 f55d0e1d3f82
child 1825 a610a2df83d2
Refactor ANSI colorization into a new utility module.
scripts/release/release.py
scripts/release/util.py
--- a/scripts/release/release.py	Fri Mar 13 01:49:39 2009 +0000
+++ b/scripts/release/release.py	Fri Mar 13 01:59:54 2009 +0000
@@ -48,6 +48,7 @@
 import sys
 
 import error
+import util
 
 
 # Default repository URLs for Melange and the Google release
@@ -112,7 +113,7 @@
       SubprocessFailed: The subprocess exited with a non-zero exit
                         code.
     """
-    print '\x1b[1m# %s\x1b[22m' % ' '.join(argv)
+    print util.colorize('# ' + ' '.join(argv), util.WHITE, bold=True)
 
     process = subprocess.Popen(argv,
                                shell=False,
@@ -131,13 +132,13 @@
 
 
 def error(msg):
-    """Print an error message, with appropriate formatting."""
-    print '\x1b[1m\x1b[31m%s\x1b[0m' % msg
+    """Log an error message."""
+    print util.colorize(msg, util.RED, bold=True)
 
 
 def info(msg):
-    """Print an informational message, with appropriate formatting."""
-    print '\x1b[32m%s\x1b[0m' % msg
+    """Log an informational message."""
+    print util.colorize(msg, util.GREEN)
 
 
 def confirm(prompt, default=False):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/release/util.py	Fri Mar 13 01:59:54 2009 +0000
@@ -0,0 +1,66 @@
+# Copyright 2009 the Melange authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Various utilities.
+
+Current contents:
+ - Text colorization using ANSI color codes
+"""
+
+__authors__ = [
+    # alphabetical order by last name, please
+    '"David Anderson" <dave@natulte.net>',
+    ]
+
+# The magic escape sequence understood by modern terminal emulators to
+# configure fore/background colors and other basic text display
+# settings.
+_ANSI_ESCAPE = '\x1b[%dm'
+
+
+# Some intrnal non-color settings that we use.
+_RESET = 0  # Reset to terminal defaults.
+_BOLD = 1   # Brighter colors.
+
+
+# ANSI color codes.
+RED = 31
+GREEN = 32
+WHITE = 37
+
+
+def _ansi_escape(code):
+    return _ANSI_ESCAPE % code
+
+
+def colorize(text, color, bold=False):
+    """Colorize some text using ANSI color codes.
+
+    Note that while ANSI color codes look good in a terminal they look
+    like noise in log files unless viewed in an ANSI color capable
+    viewer (such as 'less -R').
+
+    Args:
+      text: The text to colorize.
+      color: One of the color symbols from this module.
+      bold: If True, make the color brighter.
+
+    Returns:
+      The input text string, appropriately sprinkled with color
+      codes. Colors are reset to terminal defaults after the input
+      text.
+    """
+    bold = _ansi_escape(_BOLD) if bold else ''
+    return '%s%s%s%s' % (bold, _ansi_escape(color),
+                         text, _ansi_escape(_RESET))