[pacman-dev] Improve transaction summary appearance

Message ID 20200314032533.43939-2-uhhadd@gmail.com
State New
Headers show
Series
  • [pacman-dev] Improve transaction summary appearance
Related show

Commit Message

Carson Black March 14, 2020, 3:25 a.m. UTC
This patch attempts to improve transaction summary appearance with a few
tweaks:
- String changes to
  * Better inform the user
  * Look more visually appealing
- Using a hint of colour when colours are enabled to
  * Bring focus to the names of the packages being upgraded
  * Make it clear what operation is being taken on a package

Signed-off-by: Carson Black <uhhadd@gmail.com>
---
 src/pacman/conf.c |   8 ++++
 src/pacman/conf.h |   3 ++
 src/pacman/util.c | 119 ++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 109 insertions(+), 21 deletions(-)

Comments

Carson Black March 14, 2020, 3:31 a.m. UTC | #1
I'm aware that the quality of appearance is quite a subjective matter,
which is why I kept the changes evolutionary rather than
revolutionary.

C really isn't my forte, but this hasn't segfaulted for me (yet) and I
think the code is mostly memory safe after looking over it.

The changes being demonstrated with colours:
https://asciinema.org/a/RGEZf09JuEuqmjWfOcng4Kh8Q

-- Carson Black [ jan Pontaoski ]

Patch

diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index f9de386f..94a93f52 100644
--- a/src/pacman/conf.c
+++ b/src/pacman/conf.c
@@ -63,6 +63,8 @@  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;
@@ -76,6 +78,9 @@  void enable_colors(int colors)
 		colstr->meta    = BOLDCYAN;
 		colstr->warn    = BOLDYELLOW;
 		colstr->err     = BOLDRED;
+		colstr->remove  = BOLDRED;
+		colstr->install_upgrade = BOLDGREEN;
+		colstr->subtitle = FAINTBLACK;
 		colstr->nocolor = NOCOLOR;
 	} else {
 		colstr->colon   = ":: ";
@@ -86,6 +91,9 @@  void enable_colors(int colors)
 		colstr->meta    = "";
 		colstr->warn    = "";
 		colstr->err     = "";
+		colstr->install_upgrade = "";
+		colstr->remove = "";
+		colstr->subtitle = "";
 		colstr->nocolor = "";
 	}
 }
diff --git a/src/pacman/conf.h b/src/pacman/conf.h
index d954e637..a238ac42 100644
--- a/src/pacman/conf.h
+++ b/src/pacman/conf.h
@@ -31,6 +31,9 @@  typedef struct __colstr_t {
 	const char *meta;
 	const char *warn;
 	const char *err;
+	const char *remove;
+	const char *install_upgrade;
+	const char *subtitle;
 	const char *nocolor;
 } colstr_t;
 
diff --git a/src/pacman/util.c b/src/pacman/util.c
index a3a85bb9..022ca4ce 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)
@@ -896,6 +924,9 @@  static void _display_targets(alpm_list_t *targets, int verbose)
 		}
 	}
 
+	uint install_targets = 0;
+	uint remove_targets = 0;
+
 	/* form data for both verbose and non-verbose display */
 	for(i = targets; i; i = alpm_list_next(i)) {
 		pm_target_t *target = i->data;
@@ -905,20 +936,66 @@  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));
+			const char* target_name = alpm_pkg_get_name(target->install);
+			pm_asprintf(
+				&str,
+				// green first-char boldwhite rest-of-string nocolor dash subtitle version nocolor
+				"%s%.1s%s%s%s-%s%s%s",
+				config->colstr.install_upgrade,
+				target_name,
+				config->colstr.title,
+				target_name+1,
+				config->colstr.nocolor,
+				config->colstr.subtitle,
+				alpm_pkg_get_version(target->install),
+				config->colstr.nocolor
+			);
+			install_targets++;
 		} else if(isize == 0) {
-			pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(target->remove),
-					alpm_pkg_get_version(target->remove));
+			const char* target_name = alpm_pkg_get_name(target->remove);
+			pm_asprintf(
+				&str,
+				// green first-char boldwhite rest-of-string nocolor dash subtitle version nocolor
+				"%s%.1s%s%s%s-%s%s%s",
+				config->colstr.remove,
+				target_name,
+				config->colstr.title,
+				target_name+1,
+				config->colstr.nocolor,
+				config->colstr.subtitle,
+				alpm_pkg_get_version(target->remove),
+				config->colstr.nocolor
+			);
+			remove_targets++;
 		} else {
-			pm_asprintf(&str, "%s-%s [%s]", alpm_pkg_get_name(target->remove),
-					alpm_pkg_get_version(target->remove), _("removal"));
+			const char* target_name = alpm_pkg_get_name(target->remove);
+			pm_asprintf(
+				&str,
+				// green first-char boldwhite rest-of-string nocolor dash subtitle version nocolor removal
+				"%s%.1s%s%s%s-%s%s %s[%s]",
+				config->colstr.remove,
+				target_name,
+				config->colstr.title,
+				target_name+1,
+				config->colstr.nocolor,
+				config->colstr.subtitle,
+				alpm_pkg_get_version(target->remove),
+				config->colstr.nocolor,
+				_("removal")
+			);
+			remove_targets++;
 		}
 		names = alpm_list_add(names, str);
 	}
 
 	/* print to screen */
-	pm_asprintf(&str, "%s (%zu)", _("Packages"), alpm_list_count(targets));
+	if (install_targets && !remove_targets) {
+		pm_asprintf(&str, _("Installing %s%u%s Packages  "), config->colstr.install_upgrade, install_targets, config->colstr.title);
+	} else if (remove_targets && !install_targets) {
+		pm_asprintf(&str, _("Removing %s%u%s Packages  "), config->colstr.remove, remove_targets, config->colstr.title);
+	} else {
+		pm_asprintf(&str, _("Installing %s%u%s Packages and Removing %s%u%s Packages  "), config->colstr.install_upgrade, install_targets, config->colstr.remove, config->colstr.remove, remove_targets, config->colstr.title);
+	}
 	printf("\n");
 
 	cols = getcols();