[pacman-dev] [PATCH v2] Dull version colour numbers in summary

Allan McRae allan at archlinux.org
Tue Mar 17 01:33:07 UTC 2020


On 15/3/20 2:14 pm, Carson Black wrote:
> Version colour numbers are dulled in the non-verbose transaction summary
> when colours are enabled.
> 
> To prevent a regression, this patch also adds handling of strings with
> ANSI codes to string_length as to not break the transaction summary's
> output functions when colour codes are in the package name strings.
> 
> Signed-off-by: Carson Black <uhhadd at gmail.com>
> ---
>  src/pacman/conf.c | 40 ++++++++++++++++++++----------------
>  src/pacman/conf.h |  1 +
>  src/pacman/util.c | 52 ++++++++++++++++++++++++++++++++++++-----------
>  3 files changed, 63 insertions(+), 30 deletions(-)
> 
> diff --git a/src/pacman/conf.c b/src/pacman/conf.c
> index f9de386f..142dffcb 100644
> --- a/src/pacman/conf.c
> +++ b/src/pacman/conf.c
> @@ -63,30 +63,34 @@ config_t *config = NULL;
>  #define BOLDCYAN      "\033[1;36m"
>  #define BOLDWHITE     "\033[1;37m"
>  
> +#define FAINTBLACK    "\033[0;90m"

Why leave a space above?

Also, I note this makes almost no difference on white background terminals.

> +
>  void enable_colors(int colors)
>  {
>  	colstr_t *colstr = &config->colstr;
>  
>  	if(colors == PM_COLOR_ON) {
> -		colstr->colon   = BOLDBLUE "::" BOLD " ";
> -		colstr->title   = BOLD;
> -		colstr->repo    = BOLDMAGENTA;
> -		colstr->version = BOLDGREEN;
> -		colstr->groups  = BOLDBLUE;
> -		colstr->meta    = BOLDCYAN;
> -		colstr->warn    = BOLDYELLOW;
> -		colstr->err     = BOLDRED;
> -		colstr->nocolor = NOCOLOR;
> +		colstr->colon    = BOLDBLUE "::" BOLD " ";
> +		colstr->title    = BOLD;
> +		colstr->repo     = BOLDMAGENTA;
> +		colstr->version  = BOLDGREEN;
> +		colstr->groups   = BOLDBLUE;
> +		colstr->meta     = BOLDCYAN;
> +		colstr->warn     = BOLDYELLOW;
> +		colstr->err      = BOLDRED;
> +		colstr->subtitle = FAINTBLACK;
> +		colstr->nocolor  = NOCOLOR;

Ugh...  just remove the extra indenting when you touch this.  It serves
little purpose and just generates churn.

>  	} else {
> -		colstr->colon   = ":: ";
> -		colstr->title   = "";
> -		colstr->repo    = "";
> -		colstr->version = "";
> -		colstr->groups  = "";
> -		colstr->meta    = "";
> -		colstr->warn    = "";
> -		colstr->err     = "";
> -		colstr->nocolor = "";
> +		colstr->colon    = ":: ";
> +		colstr->title    = "";
> +		colstr->repo     = "";
> +		colstr->version  = "";
> +		colstr->groups   = "";
> +		colstr->meta     = "";
> +		colstr->warn     = "";
> +		colstr->err      = "";
> +		colstr->subtitle = "";
> +		colstr->nocolor  = "";
>  	}
>  }
>  
> diff --git a/src/pacman/conf.h b/src/pacman/conf.h
> index d954e637..41154c80 100644
> --- a/src/pacman/conf.h
> +++ b/src/pacman/conf.h
> @@ -31,6 +31,7 @@ typedef struct __colstr_t {
>  	const char *meta;
>  	const char *warn;
>  	const char *err;
> +	const char *subtitle;
>  	const char *nocolor;
>  } colstr_t;
>  
> diff --git a/src/pacman/util.c b/src/pacman/util.c
> index a3a85bb9..7eb09fb0 100644
> --- a/src/pacman/util.c
> +++ b/src/pacman/util.c
> @@ -415,12 +415,40 @@ static size_t string_length(const char *s)
>  	if(!s || s[0] == '\0') {
>  		return 0;
>  	}
> -	/* len goes from # bytes -> # chars -> # cols */
> -	len = strlen(s) + 1;
> -	wcstr = calloc(len, sizeof(wchar_t));
> -	len = mbstowcs(wcstr, s, len);
> -	len = wcswidth(wcstr, len);
> -	free(wcstr);
> +	if(strstr(s, "\033")) {
> +		char* source = s;

util.c: In function ‘string_length’:
util.c:419:18: warning: initialization discards ‘const’ qualifier from
pointer target type [-Wdiscarded-qualifiers]
  419 |   char* source = s;
      |                  ^


> +		char* replaced = malloc(sizeof(char)*strlen(s));
> +		int mode = 0;
> +		int iter = 0;
> +		for(char character = *source; character != '\0'; character = *++source) {
> +			if(mode == 0) {
> +				if(character == '\033') {
> +					mode = 1;

Just do a while loop on detection of this character, until you hit the "m".

> +				} else {
> +					replaced[iter] = character;
> +					iter++;
> +				}
> +			} else if(mode == 1) {
> +				if (character == 'm') {
> +					mode = 0;
> +				}
> +			}
> +		}
> +		replaced[++iter] = '\0';
> +		len = iter+1;
> +		wcstr = calloc(len, sizeof(wchar_t));
> +		len = mbstowcs(wcstr, replaced, len);
> +		len = wcswidth(wcstr, len);
> +		free(wcstr);
> +		free(replaced);

There is something wrong here....   I have not had time to investigate,
but I get some line wrapping that I don't get without this patch:

http://allanmcrae.com/tmp/pacman-colour-test.png

You can also see what it looks like on white in that image too.

Allan


More information about the pacman-dev mailing list