[PATCH] Add semver coloring

David Gustavsson david.e.gustavsson at gmail.com
Fri Jan 7 19:57:10 UTC 2022


When both 'verbose' and 'color' is set, the (dot-separated) parts of the
version strings where a change has occured will be highlighted in bold
face -- if there are two or more levels.

Signed-off-by: David Gustavsson <david.e.gustavsson at gmail.com>
---
 src/pacman/conf.c |  2 ++
 src/pacman/conf.h |  1 +
 src/pacman/util.c | 64 ++++++++++++++++++++++++++++++++++++++++++-----
 src/pacman/util.h |  1 +
 4 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index f9edf75b..707bd421 100644
--- a/src/pacman/conf.c
+++ b/src/pacman/conf.c
@@ -80,6 +80,7 @@ void enable_colors(int colors)
 		colstr->err     = BOLDRED;
 		colstr->faint   = GREY46;
 		colstr->nocolor = NOCOLOR;
+		colstr->changed = BOLD;
 	} else {
 		colstr->colon   = ":: ";
 		colstr->title   = "";
@@ -91,6 +92,7 @@ void enable_colors(int colors)
 		colstr->err     = "";
 		colstr->faint   = "";
 		colstr->nocolor = "";
+		colstr->changed = "";
 	}
 }
 
diff --git a/src/pacman/conf.h b/src/pacman/conf.h
index f7916ca9..82a958a9 100644
--- a/src/pacman/conf.h
+++ b/src/pacman/conf.h
@@ -33,6 +33,7 @@ typedef struct __colstr_t {
 	const char *err;
 	const char *faint;
 	const char *nocolor;
+	const char *changed;
 } colstr_t;
 
 typedef struct __config_repo_t {
diff --git a/src/pacman/util.c b/src/pacman/util.c
index c0d62826..e8bfdd09 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -858,13 +858,52 @@ static alpm_list_t *create_verbose_row(pm_target_t *target)
 	add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
 
 	/* old and new versions */
-	pm_asprintf(&str, "%s",
-			target->remove != NULL ? alpm_pkg_get_version(target->remove) : "");
-	add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
+	const char *old_version = target->remove != NULL ? alpm_pkg_get_version(target->remove) : "";
+	char old_version_copy[strlen(old_version)+1];
+	strcpy(old_version_copy, (char *)old_version);
+	const char *new_version = target->install != NULL ? alpm_pkg_get_version(target->install) : "";
+	char new_version_copy[strlen(new_version)+1];
+	strcpy(new_version_copy, (char *)new_version);
+
+	char *old_semver[10];
+	char *new_semver[10];
+	int old_semver_num = semver(old_semver, old_version_copy);
+	int new_semver_num = semver(new_semver, new_version_copy);
+
+	if (old_semver_num > 1 && new_semver_num == old_semver_num) {
+		char oldstring[100];
+		char newstring[100];
+		int oldoffset = 0;
+		int newoffset = 0;
+		for (int i = 0; i < old_semver_num; i++) {
+			if (i>0) {
+				oldoffset += snprintf(oldstring + oldoffset, 2, ".");
+				newoffset += snprintf(newstring + newoffset, 2, ".");
+			}
 
-	pm_asprintf(&str, "%s",
-			target->install != NULL ? alpm_pkg_get_version(target->install) : "");
-	add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
+			if (strcmp(old_semver[i], new_semver[i]) != 0) {
+				/* This level changed, color in */
+				oldoffset += sprintf(oldstring + oldoffset, "%s", config->colstr.changed);
+				newoffset += sprintf(newstring + newoffset, "%s", config->colstr.changed);
+			}
+			oldoffset += sprintf(oldstring + oldoffset, "%s", old_semver[i]);
+			newoffset += sprintf(newstring + newoffset, "%s", new_semver[i]);
+
+			oldoffset += sprintf(oldstring + oldoffset, "%s", config->colstr.nocolor);
+			newoffset += sprintf(newstring + newoffset, "%s", config->colstr.nocolor);
+		}
+		pm_asprintf(&str, "%s", oldstring);
+		add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
+		pm_asprintf(&str, "%s", newstring);
+		add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
+	} else {
+
+		pm_asprintf(&str, "%s", old_version);
+		add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
+
+		pm_asprintf(&str, "%s", new_version);
+		add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
+	}
 
 	/* and size */
 	size -= target->remove ? alpm_pkg_get_isize(target->remove) : 0;
@@ -1849,3 +1888,16 @@ void console_erase_line(void)
 {
 	printf("\x1B[K");
 }
+
+/* Splits version string into semver parts (major.minor.patch). Returns number of found elements. */
+int semver(char **out, char *version)
+{
+	int i = 0;
+	char *token = strtok(version, ".");
+	while (token != NULL) {
+		out[i] = token;
+		token = strtok(NULL, ".");
+		i++;
+	}
+	return i;
+}
diff --git a/src/pacman/util.h b/src/pacman/util.h
index 52e79915..a119a2f6 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -88,6 +88,7 @@ void console_cursor_move_down(unsigned int lines);
 void console_cursor_move_end(void);
 /* Erases line from the current cursor position till the end of the line */
 void console_erase_line(void);
+int semver(char **out, char *version);
 
 int pm_printf(alpm_loglevel_t level, const char *format, ...) __attribute__((format(printf,2,3)));
 int pm_asprintf(char **string, const char *format, ...) __attribute__((format(printf,2,3)));
-- 
2.34.1



More information about the pacman-dev mailing list