[arch-projects] [MKINITCPIO][PATCH 1/2] Fix colors display in some options

Dave Reisner d at falconindy.com
Mon Feb 4 20:01:59 EST 2013


On Tue, Feb 05, 2013 at 01:44:28AM +0100, Sébastien Luttringer wrote:
> Colors initialization was called after arg parsing. This disallow error messages
> and options called directly from arg parser to use colors.
> By example, call `mkinitcpio -k toto' or `mkinitcpio -L'.

-k isn't immediately processed, btw. This isn't a valid example. It's
potentially true only of -g, -H, -L, and -P.

> 
> This patch initialize colors when terminal is able to support it but disable it
> if users don't wants it (with -n).
> 
> Signed-off-by: Sébastien Luttringer <seblu at seblu.net>
> ---

I'm starting to have second thoughts about this patch. It means that you
could potentially have color or not based on the order of the flags
passed. That is...

  $ mkinitcpio -L -n # this will be in color
  $ mkinitcpio -n -L # this will not be in color

I tend to think missing out on color for a small number of options
versus this inconsistent behavior isn't worthwhile. I could look into
some refactoring so that the 4 options I mention above are colorized
properly, after arg parsing is done. It's probably equally wrong to be
doing work straight out of arg parsing.

>  functions  |  4 ++++
>  mkinitcpio | 11 ++++++-----
>  2 files changed, 10 insertions(+), 5 deletions(-)
> 
> diff --git a/functions b/functions
> index 4df91bc..b747f40 100644
> --- a/functions
> +++ b/functions
> @@ -693,4 +693,8 @@ try_enable_color() {
>      fi
>  }
>  
> +disable_colors() {
> +    unset "${!_color_@}"
> +}
> +
>  # vim: set ft=sh ts=4 sw=4 et:
> diff --git a/mkinitcpio b/mkinitcpio
> index 9802fd5..0863f3f 100755
> --- a/mkinitcpio
> +++ b/mkinitcpio
> @@ -348,6 +348,10 @@ parseopts "$_opt_short" "${_opt_long[@]}" -- "$@" || exit 1
>  set -- "${OPTRET[@]}"
>  unset _opt_short _opt_long OPTRET
>  
> +if [[ -t 1 ]]; then
> +    try_enable_color
> +fi
> +
>  while :; do
>      case $1 in
>          # --add remains for backwards compat
> @@ -380,7 +384,8 @@ while :; do
>              shift
>              _optpreset=$1 ;;
>          -n|--nocolor)
> -            _optcolor=0 ;;
> +            _optcolor=0
> +            disable_colors ;;
>          -v|--verbose)
>              _optquiet=0 ;;
>          -S|--skiphooks)
> @@ -413,10 +418,6 @@ while :; do
>      shift
>  done
>  
> -if [[ -t 1 ]] && (( _optcolor )); then
> -    try_enable_color
> -fi
> -
>  # insist that /proc and /dev be mounted (important for chroots)
>  # NOTE: avoid using mountpoint for this -- look for the paths that we actually
>  # use in mkinitcpio. Avoids issues like FS#26344.
> -- 
> Sébastien "Seblu" Luttringer
> 


More information about the arch-projects mailing list