Use the same color and message/error/warning functions as makepkg in the
rest of the pacman scripts. Added a long and short opt for the three.

Also, split the message functions into the scripts/library directory, as
they are now the same across the board. The new term_colors.sh file is a
generic term-color file for use with the output_format functions.
makepkg does not use it as it has a configuration file, so it has an
added check for using color.

Signed-off-by: William Giokas <[email protected]>
---

Sorry, forgot to reset some of the re-arranging I did in makepkg.

 doc/pkgdelta.8.txt               |  3 +++
 doc/repo-add.8.txt               |  3 +++
 scripts/library/output_format.sh | 18 +++++++++++++-----
 scripts/library/term_colors.sh   | 22 ++++++++++++++++++++++
 scripts/makepkg.sh.in            | 26 +-------------------------
 scripts/pacman-key.sh.in         |  8 ++++++--
 scripts/pkgdelta.sh.in           | 10 ++++++++--
 scripts/repo-add.sh.in           |  5 +++++
 8 files changed, 61 insertions(+), 34 deletions(-)
 create mode 100644 scripts/library/term_colors.sh

diff --git a/doc/pkgdelta.8.txt b/doc/pkgdelta.8.txt
index 587320a..b864fd2 100644
--- a/doc/pkgdelta.8.txt
+++ b/doc/pkgdelta.8.txt
@@ -41,6 +41,9 @@ Options
 *-q, \--quiet*::
        Be quiet. Do not output anything but warnings and errors.
 
+*-c, \--color*::
+       Use color in output.
+
 Examples
 --------
 
diff --git a/doc/repo-add.8.txt b/doc/repo-add.8.txt
index 91f3ac6..b23142a 100644
--- a/doc/repo-add.8.txt
+++ b/doc/repo-add.8.txt
@@ -57,6 +57,9 @@ Common Options
        If the signature is invalid, an error is produced and the update does 
not
        proceed.
 
+*-c, \--color*::
+       Use color in output.
+
 repo-add Options
 ----------------
 *-d, \--delta*::
diff --git a/scripts/library/output_format.sh b/scripts/library/output_format.sh
index 9e890e7..8861551 100644
--- a/scripts/library/output_format.sh
+++ b/scripts/library/output_format.sh
@@ -1,21 +1,29 @@
+# Common message formats
+# Colors will be empty strings unless defined in the appropriate files.
+
+plain() {
+       local mesg=$1; shift
+       printf "${BOLD}    ${mesg}${ALL_OFF}\n" "$@" >&2
+}
+
 msg() {
        (( QUIET )) && return
        local mesg=$1; shift
-       printf "==> ${mesg}\n" "$@" >&1
+       printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&1
 }
 
 msg2() {
        (( QUIET )) && return
        local mesg=$1; shift
-       printf "  -> ${mesg}\n" "$@" >&1
+       printf "${BLUE}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&1
 }
 
 warning() {
        local mesg=$1; shift
-       printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
+  printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} 
${mesg}${ALL_OFF}\n" "$@" >&2
 }
 
 error() {
        local mesg=$1; shift
-       printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
-}
\ No newline at end of file
+       printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} 
${mesg}${ALL_OFF}\n" "$@" >&2
+}
diff --git a/scripts/library/term_colors.sh b/scripts/library/term_colors.sh
new file mode 100644
index 0000000..ef1469e
--- /dev/null
+++ b/scripts/library/term_colors.sh
@@ -0,0 +1,22 @@
+# check if messages are to be printed using color
+# Uses the variable 'USE_COLOR' to determine whether or not to be used.
+unset ALL_OFF BOLD BLUE GREEN RED YELLOW
+if [[ -t 2 && ! $USE_COLOR = "n" ]]; then
+       # prefer terminal safe colored and bold text when tput is supported
+       if tput setaf 0 &>/dev/null; then
+               ALL_OFF="$(tput sgr0)"
+               BOLD="$(tput bold)"
+               BLUE="${BOLD}$(tput setaf 4)"
+               GREEN="${BOLD}$(tput setaf 2)"
+               RED="${BOLD}$(tput setaf 1)"
+               YELLOW="${BOLD}$(tput setaf 3)"
+       else
+               ALL_OFF="\e[1;0m"
+               BOLD="\e[1;1m"
+               BLUE="${BOLD}\e[1;34m"
+               GREEN="${BOLD}\e[1;32m"
+               RED="${BOLD}\e[1;31m"
+               YELLOW="${BOLD}\e[1;33m"
+       fi
+fi
+readonly ALL_OFF BOLD BLUE GREEN RED YELLOW
diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index bd29d73..3f6a73f 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -92,31 +92,7 @@ shopt -s extglob
 
 ### SUBROUTINES ###
 
-plain() {
-       local mesg=$1; shift
-       printf "${BOLD}    ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-msg() {
-       local mesg=$1; shift
-       printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-msg2() {
-       local mesg=$1; shift
-       printf "${BLUE}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-warning() {
-       local mesg=$1; shift
-       printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} 
${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-error() {
-       local mesg=$1; shift
-       printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} 
${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
+m4_include(library/output_format.sh)
 
 ##
 # Special exit call for traps, Don't print any error messages when inside,
diff --git a/scripts/pacman-key.sh.in b/scripts/pacman-key.sh.in
index 8f75e2f..8fc069f 100644
--- a/scripts/pacman-key.sh.in
+++ b/scripts/pacman-key.sh.in
@@ -44,6 +44,7 @@ RECEIVE=0
 REFRESH=0
 UPDATEDB=0
 VERIFY=0
+USE_COLOR='n'
 
 DEFAULT_KEYSERVER='hkp://pool.sks-keyservers.net'
 
@@ -67,6 +68,7 @@ usage() {
        printf -- "$(gettext "  -r, --recv-keys           Fetch the specified 
keyids")\n"
        printf -- "$(gettext "  -u, --updatedb            Update the trustdb of 
pacman")\n"
        printf -- "$(gettext "  -v, --verify              Verify the file(s) 
specified by the signature(s)")\n"
+       printf -- "$(gettext "  -c, --color               Use color for 
pacman-key messages (gpg messages not affected)")\n"
        printf -- "$(gettext "  --edit-key                Present a menu for 
key management task on keyids")\n"
        printf -- "$(gettext "  --import                  Imports pubring.gpg 
from dir(s)")\n"
        printf -- "$(gettext "  --import-trustdb          Imports ownertrust 
values from trustdb.gpg in dir(s)")\n"
@@ -511,8 +513,8 @@ if ! type gettext &>/dev/null; then
        }
 fi
 
-OPT_SHORT="adefhlruvV"
-OPT_LONG=('add' 'config:' 'delete' 'edit-key' 'export' 'finger' 'gpgdir:'
+OPT_SHORT="acdefhlruvV"
+OPT_LONG=('add' 'color' 'config:' 'delete' 'edit-key' 'export' 'finger' 
'gpgdir:'
           'help' 'import' 'import-trustdb' 'init' 'keyserver:' 'list-keys' 
'list-sigs'
           'lsign-key' 'populate' 'recv-keys' 'refresh-keys' 'updatedb'
           'verify' 'version')
@@ -548,6 +550,7 @@ while (( $# )); do
                --refresh-keys)   REFRESH=1 ;;
                -u|--updatedb)    UPDATEDB=1 ;;
                -v|--verify)      VERIFY=1 ;;
+               -c|--color)       USE_COLOR='y' ;;
 
                -h|--help)        usage; exit 0 ;;
                -V|--version)     version; exit 0 ;;
@@ -557,6 +560,7 @@ while (( $# )); do
        shift
 done
 
+m4_include(library/term_colors.sh)
 
 if ! type -p gpg >/dev/null; then
        error "$(gettext "Cannot find the %s binary required for all %s 
operations.")" "gpg" "pacman-key"
diff --git a/scripts/pkgdelta.sh.in b/scripts/pkgdelta.sh.in
index f9b40c9..4ac58cc 100644
--- a/scripts/pkgdelta.sh.in
+++ b/scripts/pkgdelta.sh.in
@@ -29,6 +29,7 @@ export TEXTDOMAINDIR='@localedir@'
 declare -r myver='@PACKAGE_VERSION@'
 
 QUIET=0
+USE_COLOR='n'
 
 # minimal of package before deltas are generated (bytes)
 min_pkg_size=$((1024*1024))
@@ -54,6 +55,7 @@ This delta file can then be added to a database using 
repo-add.\n\n")"
        echo
        printf -- "$(gettext "Options:\n")"
        printf -- "$(gettext "  -q, --quiet       minimize output\n")"
+       printf -- "$(gettext "  -c, --color       colorize output\n")"
        printf -- "$(gettext "  --min-pkg-size    minimum package size before 
deltas are generated\n")"
        printf -- "$(gettext "  --max-delta-size  percent of new package above 
which the delta will be discarded\n")"
 }
@@ -154,8 +156,8 @@ create_xdelta()
        return 0
 }
 
-OPT_SHORT='hqV'
-OPT_LONG=('help' 'quiet' 'max-delta-size:' 'min-pkg-size:' 'version')
+OPT_SHORT='chqV'
+OPT_LONG=('help' 'color' 'quiet' 'max-delta-size:' 'min-pkg-size:' 'version')
 if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
        exit 1
 fi
@@ -173,6 +175,8 @@ while :; do
                        exit 0 ;;
                -q|--quiet)
                        QUIET=1;;
+               -c|--color)
+                       USE_COLOR='y';;
                --min-pkg-size)
                        if ! min_pkg_size=$(human_to_size "$2"); then
                                echo "invalid argument '$2' for option -- '$1'"
@@ -194,6 +198,8 @@ while :; do
        shift
 done
 
+m4_include(library/term_colors.sh)
+
 if (( $# != 2 )); then
        usage
        exit 1
diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in
index 3e18a1a..d126ae8 100644
--- a/scripts/repo-add.sh.in
+++ b/scripts/repo-add.sh.in
@@ -34,6 +34,7 @@ ONLYADDNEW=0
 WITHFILES=0
 SIGN=0
 VERIFY=0
+USE_COLOR='n'
 REPO_DB_FILE=
 LOCKFILE=
 CLEAN_LOCK=0
@@ -75,6 +76,7 @@ packages to remove can be specified on the command line.\n")"
        printf -- "$(gettext "  -s, --sign        sign database with GnuPG 
after update\n")"
        printf -- "$(gettext "  -k, --key <key>   use the specified key to sign 
the database\n")"
        printf -- "$(gettext "  -v, --verify      verify database's signature 
before update\n")"
+       printf -- "$(gettext "  -c, --color       use colors in output, similar 
to makepkg\n")"
        printf -- "$(gettext "\n\
 See %s(8) for more details and descriptions of the available options.\n")" $cmd
        printf "\n"
@@ -610,6 +612,7 @@ while (( $# )); do
                -d|--delta) DELTA=1;;
                -n|--new) ONLYADDNEW=1;;
                -f|--files) WITHFILES=1;;
+               -c|--color) USE_COLOR='y';;
                -s|--sign)
                        check_gpg
                        SIGN=1
@@ -642,6 +645,8 @@ while (( $# )); do
        shift
 done
 
+m4_include(library/term_colors.sh)
+
 REPO_DB_FILE=${args[0]}
 if [[ -z $REPO_DB_FILE ]]; then
        usage
-- 
1.8.2.rc1.24.g06d67b8


Reply via email to