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

Message ID 20200315041446.63198-2-uhhadd@gmail.com
State Superseded, archived
Headers show
Series [pacman-dev,v2] Dull version colour numbers in summary | expand

Commit Message

Carson Black March 15, 2020, 4:14 a.m. UTC
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@gmail.com>
---
 src/pacman/conf.c | 40 ++++++++++++++++++++----------------
 src/pacman/conf.h |  1 +
 src/pacman/util.c | 52 ++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 63 insertions(+), 30 deletions(-)

Comments

Allan McRae March 17, 2020, 1:33 a.m. UTC | #1
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@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

Patch

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"
+
 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;
 	} 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;
+		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;
+				} 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);
+	} 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.subtitle,
+					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.subtitle,
+					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.subtitle,
+					alpm_pkg_get_version(target->remove), config->colstr.nocolor, _("removal"), config->colstr.nocolor);
 		}
 		names = alpm_list_add(names, str);
 	}