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

Carson Black uhhadd at gmail.com
Wed Apr 15 00:51:03 UTC 2020


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 | 46 ++++++++++++++++++++++++++++++++++------------
 3 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index f9de386f..76c93aca 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 GREY46        "\033[38;5;243m"
 
 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   = GREY46;
 		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 = "";
 	}
 }
@@ -119,6 +122,7 @@ config_t *config_new(void)
 	newconfig->colstr.meta    = "";
 	newconfig->colstr.warn    = "";
 	newconfig->colstr.err     = "";
+	newconfig->colstr.faint   = "";
 	newconfig->colstr.nocolor = "";
 
 	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..6693ec75 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -415,12 +415,34 @@ 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* replaced = malloc(sizeof(char) * strlen(s));
+		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 +927,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);
 	}
-- 
2.26.0


More information about the pacman-dev mailing list