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

Carson Black uhhadd at gmail.com
Sun Mar 15 03:18:08 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 | 40 +++++++++++++++-------------
 src/pacman/conf.h |  1 +
 src/pacman/util.c | 68 +++++++++++++++++++++++++++++++++--------------
 3 files changed, 71 insertions(+), 38 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"
+
 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..42e35360 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -409,20 +409,48 @@ char *strreplace(const char *str, const char *needle, const char *replace)
 
 static size_t string_length(const char *s)
 {
-	int len;
-	wchar_t *wcstr;
-
-	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);
-
-	return len;
+    int len;
+    wchar_t *wcstr;
+
+    if(!s || s[0] == '\0') {
+        return 0;
+    }
+    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;
 }
 
 static void add_table_cell(alpm_list_t **row, char *label, int mode)
@@ -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);
 	}
-- 
2.25.1


More information about the pacman-dev mailing list