[pbuilder] 01/01: Support coloured output using ANSI control characters

Mattia Rizzolo mattia at debian.org
Mon Aug 1 20:54:14 UTC 2016


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

mattia pushed a commit to branch master
in repository pbuilder.

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

    Support coloured output using ANSI control characters
    
    For now only debug, warning and error messages are coloured respectively in
    blue, yellow and red, using regular (non-bold, non-italic).
    
    Possibly more changes, more colours,  shall appear in future!
    
    The feature is enabled only if a terminal supporting colours is detected, or if
    USECOLORS=yes is provided (=no for forcefully disabling).
    
    Closes: #398402
---
 pbuilder-modules        | 38 +++++++++++++++++++++++++++++++++-----
 pbuilderrc              |  5 +++++
 pbuilderrc.5            |  9 +++++++++
 t/test_pbuilder-modules | 15 +++++++++++++++
 4 files changed, 62 insertions(+), 5 deletions(-)

diff --git a/pbuilder-modules b/pbuilder-modules
index 6e24c28..0cd4aa9 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 USECOLORS if not already set
+    local color="$1" ; shift
+    local red='\033[0;31m'
+    local yellow='\033[1;33m'
+    local blue='\033[0;34m'
+    local reset='\033[0m'
+    case "$USECOLORS" in
+        yes)
+            printf "${!color}${*}${reset}\n"
+            ;;
+        no)
+            printf "${*}\n"
+            ;;
+        *)
+            printf "malformed value of USECOLORS: [%s]\n" "$USECOLORS" >&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 ${USECOLORS:-} ] || [ "$USECOLORS" = "auto" ]; then
+        [ $(tput colors) -ge 8 ] && USECOLORS=yes || USECOLORS=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.
diff --git a/t/test_pbuilder-modules b/t/test_pbuilder-modules
index dcc868b..18b6e74 100755
--- a/t/test_pbuilder-modules
+++ b/t/test_pbuilder-modules
@@ -2,6 +2,7 @@
 
 TD="$(dirname "$0")"
 LOGLEVEL=I
+USECOLORS=no
 if [ -n "$PBUILDER_CHECKOUT" ]; then
     . "$TD/testlib.sh"
     . "$PBUILDER_CHECKOUT/pbuilder-modules"
@@ -32,6 +33,20 @@ W: warn
 E: error" test_log
 expect_stderr "W: warn
 E: error" test_log
+LOGLEVEL=D
+expect_output "D: debug
+I: info
+W: warn
+E: error" test_log
+USECOLORS=yes
+expect_output "$(printf "\033[0;34mD: debug\033[0m
+\033[0mI: info\033[0m
+\033[1;33mW: warn\033[0m
+\033[0;31mE: error\033[0m")" test_log
+
+# return to regular/stable output
+LOGLEVEL=I
+USECOLORS=no
 
 # test the non-copy case
 function test_conditional_cp_a() {

-- 
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