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

Allan McRae allan at archlinux.org
Tue Apr 14 04:58:42 UTC 2020


On 30/3/20 6:08 am, 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 |  4 ++++
>  src/pacman/conf.h |  1 +
>  src/pacman/util.c | 52 ++++++++++++++++++++++++++++++++++++-----------
>  3 files changed, 45 insertions(+), 12 deletions(-)
> 
> diff --git a/src/pacman/conf.c b/src/pacman/conf.c
> index f9de386f..d0b8e135 100644
> --- a/src/pacman/conf.c
> +++ b/src/pacman/conf.c
> @@ -62,6 +62,7 @@ config_t *config = NULL;
>  #define BOLDMAGENTA   "\033[1;35m"
>  #define BOLDCYAN      "\033[1;36m"
>  #define BOLDWHITE     "\033[1;37m"
> +#define FAINTBLACK    "\033[38;5;243m"

Just use the assigned colour name: GREY46

>  
>  void enable_colors(int colors)
>  {
> @@ -76,6 +77,7 @@ void enable_colors(int colors)
>  		colstr->meta    = BOLDCYAN;
>  		colstr->warn    = BOLDYELLOW;
>  		colstr->err     = BOLDRED;
> +		colstr->faint   = FAINTBLACK;
>  		colstr->nocolor = NOCOLOR;
>  	} else {
>  		colstr->colon   = ":: ";
> @@ -86,6 +88,7 @@ void enable_colors(int colors)
>  		colstr->meta    = "";
>  		colstr->warn    = "";
>  		colstr->err     = "";
> +		colstr->faint   = "";
>  		colstr->nocolor = "";
>  	}
>  }
> @@ -120,6 +123,7 @@ config_t *config_new(void)
>  	newconfig->colstr.warn    = "";
>  	newconfig->colstr.err     = "";
>  	newconfig->colstr.nocolor = "";
> +	newconfig->colstr.faint   = "";

Move up a line to be consistent with all other colour string orders.

>  
>  	return newconfig;
>  }
> diff --git a/src/pacman/conf.h b/src/pacman/conf.h
> index d954e637..e1df24b7 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 *faint;
>  	const char *nocolor;
>  } colstr_t;
>  
> diff --git a/src/pacman/util.c b/src/pacman/util.c
> index a3a85bb9..5042e0e3 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")) {
> +		const char* source = s;
> +		char* replaced = malloc(sizeof(char)*strlen(s));

Add spaces around "*"

> +		int mode = 0;
> +		int iter = 0;
> +		for(char character = *source; character != '\0'; character = *++source) {
> +			if(mode == 0) {
> +				if(character == '\033') {
> +					mode = 1;
> +				} else {
> +					replaced[iter] = character;
> +					iter++;
> +				}
> +			} else if(mode == 1) {
> +				if (character == 'm') {
> +					mode = 0;
> +				}
> +			}
> +		}

Lot of unneeded stuff going on there.   Replace that entire block with:

int iter = 0;
for(; *s; s++) {
	if(*s == '\033') {
		while(*s != 'm') {
			s++;
		}
	} else {
		replaced[iter] = *s;
		iter++;
	}
}


> +		replaced[iter] = '\0';
> +		len = iter;
> +		wcstr = calloc(len, sizeof(wchar_t));
> +		len = mbstowcs(wcstr, replaced, len);
> +		len = wcswidth(wcstr, len);
> +		free(wcstr);
> +		free(replaced);
> +	} else {
> +		/* 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);
> +	}
>  
>  	return len;
>  }
> @@ -905,14 +933,14 @@ static void _display_targets(alpm_list_t *targets, int verbose)
>  		}
>  
>  		if(target->install) {
> -			pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(target->install),
> -					alpm_pkg_get_version(target->install));
> +			pm_asprintf(&str, "%s%s-%s%s", alpm_pkg_get_name(target->install), config->colstr.faint,
> +					alpm_pkg_get_version(target->install), config->colstr.nocolor);
>  		} else if(isize == 0) {
> -			pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(target->remove),
> -					alpm_pkg_get_version(target->remove));
> +			pm_asprintf(&str, "%s%s-%s%s", alpm_pkg_get_name(target->remove), config->colstr.faint,
> +					alpm_pkg_get_version(target->remove), config->colstr.nocolor);
>  		} else {
> -			pm_asprintf(&str, "%s-%s [%s]", alpm_pkg_get_name(target->remove),
> -					alpm_pkg_get_version(target->remove), _("removal"));
> +			pm_asprintf(&str, "%s%s-%s %s[%s]%s", alpm_pkg_get_name(target->remove), config->colstr.faint,
> +					alpm_pkg_get_version(target->remove), config->colstr.nocolor, _("removal"), config->colstr.nocolor);
>  		}
>  		names = alpm_list_add(names, str);
>  	}
> 


More information about the pacman-dev mailing list