Add a decolorize() helper to undo the effects of colorize().
authorDavid Anderson <david.jc.anderson@gmail.com>
Fri, 13 Mar 2009 18:14:44 +0000
changeset 1833 9df2e9a67081
parent 1832 5ded837037e7
child 1834 0589bf1395c5
Add a decolorize() helper to undo the effects of colorize().
scripts/release/util.py
--- a/scripts/release/util.py	Fri Mar 13 16:56:56 2009 +0000
+++ b/scripts/release/util.py	Fri Mar 13 18:14:44 2009 +0000
@@ -26,6 +26,7 @@
     ]
 
 import os.path
+import re
 import subprocess
 
 import error
@@ -43,6 +44,7 @@
 # configure fore/background colors and other basic text display
 # settings.
 _ANSI_ESCAPE = '\x1b[%dm'
+_ANSI_ESCAPE_RE = re.compile(r'\x1b\[\d+m')
 
 
 # Some internal non-color settings that we use.
@@ -82,6 +84,11 @@
                          text, _ansi_escape(_RESET))
 
 
+def decolorize(text):
+    """Remove ANSI color codes from text."""
+    return _ANSI_ESCAPE_RE.sub('', text)
+
+
 class Paths(object):
     """A helper to construct and check paths under a given root."""