[pbuilder] 01/01: first shot at colored output, #398402

Mattia Rizzolo mattia at debian.org
Sun Jul 24 16:23:42 UTC 2016


This is an automated email from the git hooks/post-receive script.

mattia pushed a commit to branch colors
in repository pbuilder.

commit b068dae3ea5405c8c3f41d18bbc7b402447cf850
Author: Mattia Rizzolo <mattia at debian.org>
Date:   Sun Jul 24 16:22:24 2016 +0000

    first shot at colored output, #398402
---
 pbuilder-modules | 38 +++++++++++++++++++++++++++++++++-----
 pbuilderrc       |  5 +++++
 pbuilderrc.5     |  9 +++++++++
 3 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/pbuilder-modules b/pbuilder-modules
index 6e24c28..2f9fa6d 100644
--- a/pbuilder-modules
+++ b/pbuilder-modules
@@ -96,22 +96,44 @@ EOF
 #  E: error message
 #  W: warning message
 #  I: informational message
+_log() {
+    set -u
+    term_supports_colors  # defines COLORS
+    local color="$1" ; shift
+    local red='\033[0;31m'
+    local yellow='\033[1;33m'
+    local blue='\033[0;34m'
+    local reset='\033[0m'
+    case "$COLORS" in
+        yes)
+            printf "${!color}${*}${reset}\n"
+            ;;
+        no)
+            printf "${*}\n"
+            ;;
+        *)
+            printf "malformed value of COLORS: [%s]\n" "$COLORS" >&2
+            exit 1
+            ;;
+    esac
+    set +u
+}
 function log() {
     case "$*" in
         "E: "*)
-            echo "$*" >&2
+            _log 'red' "$*" >&2
             ;;
         "W: "*)
-            echo "$*" >&2
+            _log 'yellow' "$*" >&2
             ;;
         "I: "*)
-            echo "$*"
+            _log 'reset' "$*"
             ;;
         "D: "*)
-            echo "$*"
+            _log 'blue' "$*"
             ;;
         *)
-            echo "malformed log message: $*"
+            echo "malformed log message: $*" >&2
             exit 1
             ;;
     esac
@@ -150,6 +172,12 @@ _contains() {
     return 1
 }
 
+term_supports_colors() {
+    if [ -z ${COLORS:-} ] || [ "$COLORS" = "auto" ]; then
+        [ $(tput colors) -ge 8 ] && COLORS=yes || COLORS=no
+    fi
+}
+
 # test whether a directory is empty
 # fails if "$*" exists but isn't a directory
 # fails and outputs garbage if "$*" doesn't actually exist
diff --git a/pbuilderrc b/pbuilderrc
index c99118c..9302e68 100644
--- a/pbuilderrc
+++ b/pbuilderrc
@@ -7,6 +7,11 @@
 # I => errors, warnings and informational
 # D => all of the above and debug messages
 LOGLEVEL=I
+# if positive, some log messagges (errors, warnings, debugs) will be colored
+# auto => try automatically detection
+# yes  => always use colors
+# no   => never use colors
+USECOLORS=auto
 
 BASETGZ=/var/cache/pbuilder/base.tgz
 #EXTRAPACKAGES=""
diff --git a/pbuilderrc.5 b/pbuilderrc.5
index c36ab7e..9fee459 100644
--- a/pbuilderrc.5
+++ b/pbuilderrc.5
@@ -286,6 +286,15 @@ Specify how much output you want from pbuilder, valid values are
 .BR D
 (everything including some debug messages).
 .TP
+.BI "USECOLORS=" "auto"
+Specify whether you'd like to see colored output from pbuilder.  Valid values are
+.B auto
+(to try to automatically detect whether the terminal supports them),
+.B yes
+(to always use colors), and
+.B no
+(to never use colors).
+.TP
 .BI "MIRRORSITE=" "http://www.jp.debian.org/debian"
 Specify the mirror site which contains the
 main Debian distribution.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pbuilder/pbuilder.git



More information about the Pbuilder-maint mailing list