From patchwork Mon Sep 12 19:54:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sever Oraz X-Patchwork-Id: 2081 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.33) for (single-drop); Wed, 14 Sep 2022 15:07:59 +0000 (UTC) Received: from mail.archlinux.org by mail.archlinux.org with LMTP id QJPwNk7uIWMv0AsAK+/4rw (envelope-from ) for ; Wed, 14 Sep 2022 15:07:58 +0000 Received: from lists.archlinux.org (lists.archlinux.org [95.217.236.249]) (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 645F7146DFCF; Wed, 14 Sep 2022 15:07:57 +0000 (UTC) Received: from mailman3.archlinux.org (unknown [10.0.0.37]) by lists.archlinux.org (Postfix) with ESMTP id 14A4B14AF0FF; Wed, 14 Sep 2022 15:07:56 +0000 (UTC) Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lists.archlinux.org (Postfix) with ESMTPS id ED85614A4439 for ; Mon, 12 Sep 2022 19:54:27 +0000 (UTC) Received: by mail-wr1-x434.google.com with SMTP id bo13so657966wrb.1 for ; Mon, 12 Sep 2022 12:54:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=rSGhmbFLg6ZYFUCOzhMT2Mn5QPGl5PdlwzCLX2PR8FE=; b=a5UUUpCWVQ/oakOeZGFTGkyJ/eE4CrCRkOBeSp5wfsOtNnXqIp2rUmZH5Dxldv0MIw eIbYuYrTiodO+5qSUfHCXxkzqPXR0EOpv7STkCM8VRKmuUkfE4jdCnnbW5J7HrXDGNOG Nf88CRJpwb6yySmlC9R7Ia6+qf96ZncwNoVL9bh2Yo/XGB1sNtns/993SIYgVm2TJgwd oZkQSulbULa6eZYZM7nC6NwVVJO49hgI5/0Iq8iFAbzzTHXlVcXTxt2KsOAwqhz79PLj a8sP6HVWVi4qUgBzGiqmxYu+ZIW0uPpg7m8mr0e/taVnUcJMy6i6hzfgf/N3YGhoSew4 et3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=rSGhmbFLg6ZYFUCOzhMT2Mn5QPGl5PdlwzCLX2PR8FE=; b=vbc2+0yO3DHHjdaqhoqCrEKv04xEf/f7JcdctrPKC+f3CnvXyfqU6hd42y9d5x+n+j tjUqNnyW0Tdn5YJCKB/lbxGMWpiza+QxrnhBSkxRMg5wPXhFs5m82EZh+ud2SnoiNZ5Q Sct1R5C11iOdOwrZsF2cHccOnLP1x9RODG2aoL8Jms70drYiUWtd0rxwPZTsJ2Di7ZEG kUASPWdVfTjNjyD1/qrbH/CXvbcTwnTDG9mIe12UXuoCplRdSVMR9g1vXJwqSFlWlgOM q05mOVuzqSAdU6w83PE5rg29mSE/kAOasuEeVftut18pM723UxuNfbvXR8b8MEGUEyAk K00w== X-Gm-Message-State: ACgBeo0VPccakJAKFjSrxdgtmzWqU+b+/Ca/5KT1idHTlxs6aNZuGsnp sMSHiIYSTpaiPVGElS1FLkjJ/vSqGt8= X-Google-Smtp-Source: AA6agR5RYMsoHXflsFW8CB4VOOnHoPx1s53ecLTe8Uor+Q+bHo7EAMsvoWFdBWSWbg7iW3ZicrxGvg== X-Received: by 2002:a5d:6f08:0:b0:22a:7e99:da83 with SMTP id ay8-20020a5d6f08000000b0022a7e99da83mr4531600wrb.544.1663012467524; Mon, 12 Sep 2022 12:54:27 -0700 (PDT) Received: from kvazaro.lan ([87.74.76.208]) by smtp.gmail.com with ESMTPSA id r15-20020a5d694f000000b00228c792aaaasm8153091wrw.100.2022.09.12.12.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Sep 2022 12:54:27 -0700 (PDT) From: severoraz@gmail.com To: pacman-dev@lists.archlinux.org Subject: [PATCH] pacman: Add space before percent sign in download progress string. Date: Mon, 12 Sep 2022 20:54:19 +0100 Message-Id: <20220912195419.3080257-1-severoraz@gmail.com> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 X-MailFrom: severoraz@gmail.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation Message-ID-Hash: FC2SOBYJN3NOHGRNP56VSPDBNS23VYSL X-Message-ID-Hash: FC2SOBYJN3NOHGRNP56VSPDBNS23VYSL X-Mailman-Approved-At: Wed, 14 Sep 2022 15:07:52 +0000 CC: Severo Raz X-Mailman-Version: 3.3.5 Precedence: list List-Id: Discussion list for pacman development Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Spamd-Result: default: False [2.39 / 15.00]; DATE_IN_PAST(1.00)[43]; MID_CONTAINS_FROM(1.00)[]; R_DKIM_REJECT(1.00)[gmail.com:s=20210112]; RCVD_IN_DNSWL_MED(-0.20)[95.217.236.249:from]; R_SPF_ALLOW(-0.20)[+ip4:95.217.236.249:c]; MAILLIST(-0.20)[mailman]; MIME_GOOD(-0.10)[text/plain]; DMARC_POLICY_SOFTFAIL(0.10)[gmail.com : SPF not aligned (relaxed),none]; HAS_LIST_UNSUB(-0.01)[]; FROM_NO_DN(0.00)[]; DKIM_TRACE(0.00)[gmail.com:-]; PREVIOUSLY_DELIVERED(0.00)[pacman-dev@lists.archlinux.org]; FREEMAIL_FROM(0.00)[gmail.com]; TO_DN_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::434:received]; NEURAL_HAM(-0.00)[-1.000]; FREEMAIL_CC(0.00)[gmail.com]; RCPT_COUNT_TWO(0.00)[2]; RCVD_COUNT_FIVE(0.00)[5]; FROM_NEQ_ENVFROM(0.00)[severoraz@gmail.com,pacman-dev-bounces@lists.archlinux.org]; RECEIVED_SPAMHAUS_PBL(0.00)[87.74.76.208:received]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; ARC_NA(0.00)[]; RCVD_TLS_LAST(0.00)[]; FORGED_SENDER_MAILLIST(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:24940, ipnet:95.217.0.0/16, country:DE]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspamd-Server: mail.archlinux.org X-Rspamd-Queue-Id: 645F7146DFCF Authentication-Results: mail.archlinux.org; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20210112 header.b=a5UUUpCW; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (mail.archlinux.org: domain of pacman-dev-bounces@lists.archlinux.org designates 95.217.236.249 as permitted sender) smtp.mailfrom=pacman-dev-bounces@lists.archlinux.org From: Severo Raz Also add explanatory comment and reduce use of magic numbers. Signed-off-by: Severo Raz --- src/pacman/callback.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/pacman/callback.c b/src/pacman/callback.c index aa6303bf..c48b887d 100644 --- a/src/pacman/callback.c +++ b/src/pacman/callback.c @@ -152,19 +152,28 @@ static int64_t get_update_timediff(int first_call) /* refactored from cb_trans_progress */ static void fill_progress(const int percent, const int proglen) { - /* 8 = 1 space + 1 [ + 1 ] + 5 for percent */ - const int hashlen = proglen > 8 ? proglen - 8 : 0; - const int hash = percent * hashlen / 100; - int i; + /* + * Format: + * " [###---] NNN %" + * where NNN is the space-padded percentage. + * + * thrshlen = strlen(" [] 100 %") = 9 + * perclen = strlen(" 100 %") = 6 + */ + const int threshlen = 9; + const int perclen = 6; + const int pbarlen = (proglen > threshlen) ? (proglen - threshlen) : 0; + + if(pbarlen > 0) { + const int remlen = (100 - percent) * pbarlen / 100; - if(hashlen > 0) { fputs(" [", stdout); - for(i = hashlen; i > 0; --i) { + for(int i = pbarlen; i > 0; --i) { /* if special progress bar enabled */ if(config->chomp) { - if(i > hashlen - hash) { + if(i > remlen) { putchar('-'); - } else if(i == hashlen - hash) { + } else if(i == remlen) { if(percent % 2 == 0) { fputs("\033[1;33mC\033[m", stdout); } else { @@ -176,7 +185,7 @@ static void fill_progress(const int percent, const int proglen) fputs("\033[0;37m \033[m", stdout); } } /* else regular progress bar */ - else if(i > hashlen - hash) { + else if(i > remlen) { putchar('#'); } else { putchar('-'); @@ -185,9 +194,8 @@ static void fill_progress(const int percent, const int proglen) putchar(']'); } /* print display percent after progress bar */ - /* 5 = 1 space + 3 digits + 1 % */ - if(proglen >= 5) { - printf(" %3d%%", percent); + if(proglen >= perclen) { + printf(" %3d %%", percent); } putchar('\r');