[pacman-dev] [PATCHv2 10/10] contrib: make pacdiff colors same as scripts

William Giokas 1007380 at gmail.com
Sun Mar 3 13:48:25 EST 2013


pacdiff already had colors, but tthey were specific to pacdiff, and had
no similar style to other pacman scripts. I also added in an ask()
function to the output_format.sh for use with read commands (no \n at
the end).

Signed-off-by: William Giokas <1007380 at gmail.com>
---

Note: this script does not have a --no-color option, though the original
did not either, and as it is more of an interactive application, I didn't
think it too big of an issue to not include this.

 contrib/pacdiff.sh.in            | 35 +++++++++--------------------------
 scripts/library/output_format.sh |  5 +++++
 2 files changed, 14 insertions(+), 26 deletions(-)

diff --git a/contrib/pacdiff.sh.in b/contrib/pacdiff.sh.in
index b41544f..47779d6 100644
--- a/contrib/pacdiff.sh.in
+++ b/contrib/pacdiff.sh.in
@@ -24,22 +24,11 @@ declare -r myver='@PACKAGE_VERSION@'
 diffprog=${DIFFPROG:-vimdiff}
 diffsearchpath=${DIFFSEARCHPATH:-/etc}
 locate=0
+USE_COLOR='y'
 
-if tput setaf 0 &>/dev/null; then
-	ALL_OFF="$(tput sgr0)"
-	BOLD="$(tput bold)"
-	BLUE="${BOLD}$(tput setaf 4)"
-	GREEN="${BOLD}$(tput setaf 2)"
-	YELLOW="${BOLD}$(tput setaf 3)"
-	PURPLE="${BOLD}$(tput setaf 5)"
-else
-	ALL_OFF="\e[1;0m"
-	BOLD="\e[1;1m"
-	BLUE="${BOLD}\e[1;34m"
-	GREEN="${BOLD}\e[1;32m"
-	YELLOW="${BOLD}\e[1;33m"
-	PURPLE="${BOLD}\e[1;35m"
-fi
+m4_include(../scripts/library/output_format.sh)
+
+m4_include(../scripts/library/term_colors.sh)
 
 usage() {
 	echo "$myname : a simple pacnew/pacorig/pacsave updater"
@@ -82,24 +71,18 @@ while IFS= read -u 3 -r -d '' pacfile; do
 	file="${pacfile%.pac*}"
 	file_type="pac${pacfile##*.pac}"
 
-	case $file_type in
-		pacnew) printf "$GREEN%s$ALL_OFF" "$file_type";;
-		pacorig) printf "$YELLOW%s$ALL_OFF" "$file_type";;
-		pacsave) printf "$BLUE%s$ALL_OFF" "$file_type";;
-	esac
-
-	printf " file found for $PURPLE%s$ALL_OFF\n" "$file"
+	msg "%s file found for %s" "$file_type" "$file"
 	if [ ! -f "$file" ]; then
-		echo "  $file does not exist"
+		warning "$file does not exist"
 		rm -iv "$pacfile"
 		continue
 	fi
 
 	if cmp -s "$pacfile" "$file"; then
-		echo "  Files are identical, removing..."
+		msg2 "Files are identical, removing..."
 		rm -v "$pacfile"
 	else
-		printf "  (V)iew, (S)kip, (R)emove %s, (O)verwrite with %s: [v/s/r/o] " "$file_type" "$file_type"
+		ask "(V)iew, (S)kip, (R)emove %s, (O)verwrite with %s: [v/s/r/o] " "$file_type" "$file_type"
 		while read c; do
 			case $c in
 				r|R) rm -v "$pacfile"; break ;;
@@ -108,7 +91,7 @@ while IFS= read -u 3 -r -d '' pacfile; do
 				$diffprog "$pacfile" "$file"
 				rm -iv "$pacfile"; break ;;
 				s|S) break ;;
-				*) printf "  Invalid answer. Try again: [v/s/r/o] "; continue ;;
+				*) ask "Invalid answer. Try again: [v/s/r/o] "; continue ;;
 			esac
 		done
 	fi
diff --git a/scripts/library/output_format.sh b/scripts/library/output_format.sh
index 2930200..2d5e6a3 100644
--- a/scripts/library/output_format.sh
+++ b/scripts/library/output_format.sh
@@ -18,6 +18,11 @@ msg2() {
 	printf "${BLUE}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
 }
 
+ask() {
+	local mesg=$1; shift
+	printf "${YELLOW}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}" "$@" >&1
+}
+
 warning() {
 	local mesg=$1; shift
 	printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-- 
1.8.2.rc1.24.g06d67b8



More information about the pacman-dev mailing list