[pacman-dev] [PATCHv2 01/10] script: Add color to library/output_format.sh

Allan McRae allan at archlinux.org
Sun Mar 3 17:59:36 EST 2013


On 04/03/13 04:48, William Giokas wrote:
> Use the same colors as makepkg when printing messages using the
> functions in output_format.sh.
> 
> Signed-off-by: William Giokas <1007380 at gmail.com>
> ---
> 
> Originally msg and msg2 printed to stdout, but that messed with makepkgs sed
> expressions, and they originally printed to sdterr in makepkg, so now they
> print to the right place.
> 
>  scripts/library/README           |  6 +++++-
>  scripts/library/output_format.sh | 18 +++++++++++++-----
>  scripts/library/term_colors.sh   | 21 +++++++++++++++++++++
>  3 files changed, 39 insertions(+), 6 deletions(-)
>  create mode 100644 scripts/library/term_colors.sh
> 
> diff --git a/scripts/library/README b/scripts/library/README
> index 0fa0f84..5f2961c 100644
> --- a/scripts/library/README
> +++ b/scripts/library/README
> @@ -6,7 +6,7 @@ Provides basic output formatting functions with levels 'msg', 'msg2',
>  'warning' and 'error'.  The 'msg' amd 'msg2' functions print to stdout
>  and can be silenced by defining 'QUIET'.  The 'warning' and 'error'
>  functions print to stderr with the appropriate prefix added to the
> -message.
> +message. Also includes optional color output.
> 

Repeating myself, but note the documentation is right about what should
point to stdout...

>  parseopts.sh:
>  A getopt_long-like parser which portably supports longopts and shortopts
> @@ -39,3 +39,7 @@ as mawk or busybox awk.
>  size_to_human.sh:
>  The reverse of human_to_size, this function takes an integer byte size and
>  prints its in human readable format, with SI prefixes (e.g. MiB, TiB).
> +
> +term_colors.sh
> +Set the terminal colors for the common message formats. Uses the variable
> +`USE_COLOR` to determine when to be used.

Is there any reason to have this separate from output_format?

> diff --git a/scripts/library/output_format.sh b/scripts/library/output_format.sh
> index 9e890e7..2930200 100644
> --- a/scripts/library/output_format.sh
> +++ b/scripts/library/output_format.sh
> @@ -1,21 +1,29 @@
> +# Common message formats
> +
> +plain() {
> +  (( QUIET )) && return
> +  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" "$@" >&2
>  }
>  
>  msg2() {
>  	(( QUIET )) && return
>  	local mesg=$1; shift
> -	printf "  -> ${mesg}\n" "$@" >&1
> +	printf "${BLUE}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
>  }
>  
>  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..a675247
> --- /dev/null
> +++ b/scripts/library/term_colors.sh
> @@ -0,0 +1,21 @@
> +# check if messages are to be printed using color
> +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
> 



More information about the pacman-dev mailing list