From b4454e4e952f85d929db76a37f63587701531fbd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <gentoo@mgorny.alt.pl>
Date: Mon, 12 Jul 2010 11:27:40 +0200
Subject: [PATCH] Support colorful output.

---
 sunrise-commit | 43 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/sunrise-commit b/sunrise-commit
index 0ed33d9..679abad 100755
--- a/sunrise-commit
+++ b/sunrise-commit
@@ -9,12 +9,12 @@ say() {
 }
 
 die() {
-	say "${@}"
+	say "${RED}${@}${RESET}"
 	exit 1
 }
 
 sayv() {
-	[ -n "${SC_VERBOSE}" ] && say "${@}"
+	[ -n "${SC_VERBOSE}" ] && say "${GREEN}${@}${RESET}"
 }
 
 req() {
@@ -112,13 +112,14 @@ Options:
 	-V, --version		print version string,
 
 	-c, --changelog		backwards compat (ignored),
+	-C, --nocolor		disable colorful output,
 	-v, --verbose		enable verbose output.
 _EOH_
 }
 
 # Guess what!
 main() {
-	local commitmsg
+	local commitmsg monochrome
 	unset SC_VERBOSE
 
 	while [ ${#} -gt 0 ]; do
@@ -135,6 +136,9 @@ main() {
 			-c|--changelog)
 				# Now changelog entries are implicit -- backwards compat.
 				;;
+			-C|--nocolor)
+				monochrome=1
+				;;
 			-v|--verbose)
 				SC_VERBOSE=1
 				;;
@@ -151,6 +155,25 @@ main() {
 		shift
 	done
 
+	if [ -n "${monochrome}" ]; then
+		RESET=
+		RED=
+		GREEN=
+		BGREEN=
+		YELLOW=
+		WHITE=
+	else
+		local esc
+		esc=$(printf '\033[')
+
+		RESET=${esc}0m
+		RED="${esc}1;31m"
+		GREEN=${esc}32m
+		BGREEN="${esc}1;32m"
+		YELLOW="${esc}1;33m"
+		WHITE="${esc}1;37m"
+	fi
+
 	[ -n "${commitmsg}" ] || die 'No commit message provided.'
 
 	find_repo
@@ -178,11 +201,12 @@ main() {
 
 				if [ -n "${bns}" ]; then
 					echo
-					local bn
+					local bn cbn
 					for bn in ${bns}; do
+						cbn=#${WHITE}${bn}${NORMAL}
 						wget -q http://bugs.gentoo.org/show_bug.cgi?id=${bn} -O - \
-							| sed -e 's; *<title>Gentoo Bug \([0-9]*\) - \(.*\)</title>;Bug \1: \2;p' \
-								-e "s; *<title>Gentoo \(Invalid Bug ID\)</title>;Bug ${bn}: !! \1;p" -e d 
+							| sed -e "s, *<title>Gentoo Bug \([0-9]*\) - \(.*\)</title>,Bug ${cbn}: ${BGREEN}\2${RESET},p" \
+								-e "s, *<title>Gentoo \(Invalid Bug ID\)</title>,Bug ${cbn}: ${YELLOW}!! \1${RESET},p" -e d 
 					done
 					echo
 				fi
@@ -197,14 +221,15 @@ main() {
 			if [ $? -eq 2 ]; then
 				old_repoman=
 
-				say 'Please update portage to newer version in order to have repoman supporting'
-				say "--ask option and thus delay the following question until after 'repoman full'."
+				say "${GREEN}Please update portage to newer version in order to have repoman supporting"
+				say "--ask option and thus delay the following question until after 'repoman full'.${RESET}"
 				say
 
 				while true; do
 					local answ
-					printf '%s' 'Commit changes? [Yes/No] ' >&2
+					printf '%s' "${WHITE}Commit changes?${RESET} [${BGREEN}Yes${RESET}/${RED}No${RESET}] ${GREEN}" >&2
 					read answ
+					printf '%s' "${RESET}"
 
 					case "${answ}" in
 						[yY]|[yY][eE]|[yY][eE][sS])