Re: [PATCH] Add semver coloring
Message: 1 Date: Sun, 9 Jan 2022 16:41:26 +0000 From: Morgan Adamiec <morganamilo@archlinux.org> To: pacman-dev@lists.archlinux.org Subject: Re: [PATCH] Add semver coloring Message-ID: <6a5cf863-81e5-c663-a807-43b63f599ee6@archlinux.org> Content-Type: text/plain; charset=UTF-8
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@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 +
+ 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 +
+ newoffset += sprintf(newstring + newoffset, "%s", config->colstr.changed); + } + oldoffset += sprintf(oldstring + oldoffset, "%s",
+ 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
On 07/01/2022 19:57, David Gustavsson wrote: oldoffset, 2, "."); oldoffset, "%s", config->colstr.changed); old_semver[i]); 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)));
Package versioning does not follow semver so this patch doesn't really make sense. However I've implemented version diffing before in a way that matches how package versions are compared. This is implemented in both yay and paru so you'll have to adapt the code to c.
I figured in the cases where semver is not followed, the text is left unaffected, and a lot of packages do follow some kind of dot-separated versioning (Here's how the patch looked when I ran the other day: https://imgur.com/Zc1CeXz). But thinking about it, it *would* be much simpler to just compare letter by letter and color everything beyond the first differing letter in. I'll try it out some time this week.
participants (1)
-
David Gustavsson