From 0e8dff40f248b4054404769c496746ffe7e7ce0d Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Fri, 13 Jul 2012 13:00:59 -0400
Subject: [PATCH 1/2] Issue #1683026: Refactor ANSI colors into their own
 function.

---
 includes/drush.inc | 53 +++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 39 insertions(+), 14 deletions(-)

diff --git a/includes/drush.inc b/includes/drush.inc
index d09f930..5da7b6e 100644
--- a/includes/drush.inc
+++ b/includes/drush.inc
@@ -1363,17 +1363,6 @@ function drush_pipe_output() {
  *   False in case of an error or failed type, True in all other cases.
  */
 function _drush_print_log($entry) {
-  if (drush_get_context('DRUSH_NOCOLOR')) {
-    $red = "[%s]";
-    $yellow = "[%s]";
-    $green = "[%s]";
-  }
-  else {
-    $red = "\033[31;40m\033[1m[%s]\033[0m";
-    $yellow = "\033[1;33;40m\033[1m[%s]\033[0m";
-    $green = "\033[1;32;40m\033[1m[%s]\033[0m";
-  }
-
   $verbose = drush_get_context('DRUSH_VERBOSE');
   $debug = drush_get_context('DRUSH_DEBUG');
 
@@ -1381,11 +1370,11 @@ function _drush_print_log($entry) {
   switch ($entry['type']) {
     case 'warning' :
     case 'cancel' :
-      $type_msg = sprintf($yellow, $entry['type']);
+      $type_msg = sprintf(drush_ansi_color('yellow'), '[' . $entry['type'] . ']');
       break;
     case 'failed' :
     case 'error' :
-      $type_msg = sprintf($red, $entry['type']);
+      $type_msg = sprintf(drush_ansi_color('red'), '[' . $entry['type'] . ']');
       $return = FALSE;
       break;
     case 'ok' :
@@ -1396,7 +1385,7 @@ function _drush_print_log($entry) {
       if (drush_get_context('DRUSH_QUIET')) {
         return TRUE;
       }
-      $type_msg = sprintf($green, $entry['type']);
+      $type_msg = sprintf(drush_ansi_color('green'), '[' . $entry['type'] . ']');
       break;
     case 'notice' :
     case 'message' :
@@ -1444,6 +1433,42 @@ function _drush_print_log($entry) {
   return $return;
 }
 
+/**
+ * Map a color into the ANSI escape sequence used in a terminal to highlight
+ * text in that color.
+ *
+ * @param $color
+ *   The color to display the text in.
+ *
+ * @return
+ *   A string suitable for use with sprintf(). If color is not available in the
+ *   current terminal, '%s' is returned.
+ */
+function drush_ansi_color($color) {
+  if (!drush_get_context('DRUSH_NOCOLOR')) {
+    switch ($color) {
+      case 'black':
+        return "\033[30;40m\033[1m%s\033[0m";
+      case 'red':
+        return "\033[31;40m\033[1m%s\033[0m";
+      case 'green':
+        return "\033[1;32;40m\033[1m%s\033[0m";
+      case 'yellow':
+        return "\033[1;33;40m\033[1m%s\033[0m";
+      case 'blue':
+        return "\033[1;34;40m\033[1m%s\033[0m";
+      case 'magenta':
+        return "\033[1;35;40m\033[1m%s\033[0m";
+      case 'cyan':
+        return "\033[1;36;40m\033[1m%s\033[0m";
+      case 'white':
+        return "\033[1;37;40m\033[1m%s\033[0m";
+    }
+  }
+
+  return '%s';
+}
+
 // Print all timers for the request.
 function drush_print_timers() {
   global $timers;
-- 
1.7.11.1


From 5364f084bfd48656aa8e97b0c0d3ac032137d649 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Fri, 13 Jul 2012 13:27:49 -0400
Subject: [PATCH 2/2] Issue #1683026: Add a function to format text with ANSI
 escape sequences.

---
 includes/drush.inc | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/includes/drush.inc b/includes/drush.inc
index 5da7b6e..c99cf37 100644
--- a/includes/drush.inc
+++ b/includes/drush.inc
@@ -1469,6 +1469,36 @@ function drush_ansi_color($color) {
   return '%s';
 }
 
+/**
+ * Map a format into the appropriate ANSI escape sequence. We assume that if
+ * the terminal doesn't support color, it doesn't support formatting either.
+ *
+ * @param $format
+ *   The format to display the text in, such as bold or underline.
+ *
+ * @return
+ *   A string suitable for use with sprintf(). If color is not available in the
+ *   current terminal, '%s' is returned.
+ */
+function drush_ansi_format($format) {
+  if (!drush_get_context('DRUSH_NOCOLOR')) {
+    switch ($format) {
+      case 'bold':
+        return "\033[1m%s\033[0m";
+      case 'italics':
+        return "\033[3m%s\033[0m";
+      case 'underline':
+        return "\033[4m%s\033[0m";
+      case 'inverse':
+        return "\033[7m%s\033[0m";
+      case 'strikethrough':
+        return "\033[9m%s\033[0m";
+    }
+  }
+
+  return '%s';
+}
+
 // Print all timers for the request.
 function drush_print_timers() {
   global $timers;
-- 
1.7.11.1

