From patchwork Fri Jan 7 19:57:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gustavsson X-Patchwork-Id: 2010 Return-Path: Delivered-To: patchwork@archlinux.org Received: from mail.archlinux.org [2a01:4f9:c010:3052::1] by patchwork.archlinux.org with IMAP (fetchmail-6.4.25) for (single-drop); Fri, 07 Jan 2022 19:57:50 +0000 (UTC) Received: from mail.archlinux.org by mail.archlinux.org with LMTP id YP0POD2b2GGWLwsAK+/4rw (envelope-from ) for ; Fri, 07 Jan 2022 19:57:49 +0000 Received: from lists.archlinux.org (lists.archlinux.org [IPv6:2a01:4f9:c010:9eb4::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.archlinux.org (Postfix) with ESMTPS id A6C65BFABE1; Fri, 7 Jan 2022 19:57:48 +0000 (UTC) Received: from lists.archlinux.org (localhost [IPv6:::1]) by lists.archlinux.org (Postfix) with ESMTP id 83E69B84900; Fri, 7 Jan 2022 19:57:48 +0000 (UTC) X-Original-To: pacman-dev@lists.archlinux.org Delivered-To: pacman-dev@lists.archlinux.org Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lists.archlinux.org (Postfix) with ESMTPS id 7FF9BB848F8 for ; Fri, 7 Jan 2022 19:57:46 +0000 (UTC) Received: by mail-lf1-x136.google.com with SMTP id u13so19275109lff.12 for ; Fri, 07 Jan 2022 11:57:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4+IQVNf3inMqSJtQlMawG95x8t1UnBBcrd+nrykAqOk=; b=iBpKDvy22BNFN3KuS48aU/VhlMgqTRaBeyvH1+Xmh7yUH4+/iTg+p7mcqQIxxhFc/k F9SkyjJgD5VQWKX/dVDK3GL7lmaEFmQvsPGvPK1NJqSWtTeW418GsikYIKi/GsDgpH+b 3OYm/ckheuFQdOvcx2J8CW5EnCVVXhZgPrsopuDVrGVfXLBxZ6t5qvbSjxQCQ1Ikf5al 4+U2GHul260WbeYD8Y0KCE9fapgnmkwY44g3R4bcCeggqLat7SCEc7z0bNxs8H9ycQTI 6FyFKwPlU/6noBQ5Ow8//Ka0MIqaVBpfEAg4agFYJOoVPSye1cuTtrTNPbeLYfdcUShw P8Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4+IQVNf3inMqSJtQlMawG95x8t1UnBBcrd+nrykAqOk=; b=22ysjICiaW+bIBPcUrkpN/TTVGP8ZWJF693Q3wbbVcxBbdOllvJur8lavcfMg/pbnP 0q8l9YCXaPlmGHhdqW8vNeI+COb0QtU07wFNgP2EaefyWa4LQQcXusfQDhjSY4QXtURy uAFpAYBnM7aIqVlgcF/4OMETe+jpE5WmbNqlxkB1DShaMAVfmLSwY46YsSCuYIxg5bEN LxRxPVOctdKCaoMCbGUdLORefYcwtgBC9oGS7WtDroZ24spEhhQQNZdkpfPUaXIlbE/z 13jcejYIzuQAmlsbi1x8/keHo7L6wyHDeUD9A7MQXPKduEoCwN4y4C5300OQlXaHSGJS Enxw== X-Gm-Message-State: AOAM533hIwHxcRFtbSTtzMAw3PEOpW5TGgFtLo5+IdL1sIbQMjXbuzhK h4PaOOzfBWwtT7uavsrxhdsKUNhTh5stGw== X-Google-Smtp-Source: ABdhPJwJEIR7Xh69QmXVrjg1wne+zvNm2gMhfJeWytzMqnYiKkrkQZN5stpM9kPBWmJ46Om/H2x6NA== X-Received: by 2002:a2e:b90d:: with SMTP id b13mr51798172ljb.253.1641585465741; Fri, 07 Jan 2022 11:57:45 -0800 (PST) Received: from localhost.localdomain (83-233-18-159.cust.bredband2.com. [83.233.18.159]) by smtp.gmail.com with ESMTPSA id e8sm696695lfr.112.2022.01.07.11.57.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jan 2022 11:57:45 -0800 (PST) From: David Gustavsson To: pacman-dev@lists.archlinux.org Cc: David Gustavsson Subject: [PATCH] Add semver coloring Date: Fri, 7 Jan 2022 20:57:10 +0100 Message-Id: <20220107195710.123511-1-david.e.gustavsson@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-BeenThere: pacman-dev@lists.archlinux.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: Discussion list for pacman development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: pacman-dev-bounces@lists.archlinux.org Sender: "pacman-dev" Authentication-Results: mail.archlinux.org; dkim=pass header.d=gmail.com header.s=20210112 header.b=iBpKDvy2; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mail.archlinux.org: domain of pacman-dev-bounces@lists.archlinux.org designates 2a01:4f9:c010:9eb4::1 as permitted sender) smtp.mailfrom=pacman-dev-bounces@lists.archlinux.org X-Rspamd-Queue-Id: A6C65BFABE1 X-Spamd-Result: default: False [-1.41 / 15.00]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; RCVD_DKIM_ARC_DNSWL_MED(-0.50)[]; R_MISSING_CHARSET(0.50)[]; MAILLIST(-0.20)[mailman]; RCVD_IN_DNSWL_MED(-0.20)[2a01:4f9:c010:9eb4::1:from]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20210112]; R_SPF_ALLOW(-0.20)[+ip6:2a01:4f9:c010:9eb4::1:c]; MIME_GOOD(-0.10)[text/plain]; HAS_LIST_UNSUB(-0.01)[]; FROM_HAS_DN(0.00)[]; FROM_NEQ_ENVFROM(0.00)[davidegustavsson@gmail.com,pacman-dev-bounces@lists.archlinux.org]; TAGGED_RCPT(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[pacman-dev@lists.archlinux.org]; NEURAL_HAM(-0.00)[-1.000]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::136:received]; RCVD_COUNT_FIVE(0.00)[5]; RCVD_VIA_SMTP_AUTH(0.00)[]; TAGGED_FROM(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; DKIM_TRACE(0.00)[gmail.com:+]; RCPT_COUNT_TWO(0.00)[2]; FREEMAIL_FROM(0.00)[gmail.com]; TO_DN_SOME(0.00)[]; ARC_NA(0.00)[]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; RCVD_TLS_LAST(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_CC(0.00)[gmail.com]; ASN(0.00)[asn:24940, ipnet:2a01:4f9::/32, country:DE]; FORGED_SENDER_MAILLIST(0.00)[] X-Rspamd-Server: mail.archlinux.org 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 --- 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)));