pacman: Add space before percent sign in download progress string.

Message ID 20220912195419.3080257-1-severoraz@gmail.com
State New
Headers show
Series pacman: Add space before percent sign in download progress string. | expand

Commit Message

Sever Oraz Sept. 12, 2022, 7:54 p.m. UTC
From: Severo Raz <severoraz@gmail.com>

Also add explanatory comment and reduce use of magic numbers.

Signed-off-by: Severo Raz <severoraz@gmail.com>
---
 src/pacman/callback.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)

Comments

Allan McRae Sept. 14, 2022, 9:47 p.m. UTC | #1
On 13/9/22 05:54, severoraz@gmail.com wrote:
> From: Severo Raz <severoraz@gmail.com>
> 
> Also add explanatory comment and reduce use of magic numbers.
> 

Why add a space?  I'm guessing there is some locale where that happens, 
but I can not find one.

Allan
Chris Down Sept. 14, 2022, 9:56 p.m. UTC | #2
Allan McRae writes:
>Why add a space?  I'm guessing there is some locale where that 
>happens, but I can not find one.

It's common or mandatory in French, German, and some other languages[0].

It seems a bit strange an otherwise English-by-default interface with LANG=C, 
so at least this should be gated on such locales.

0: https://en.wikipedia.org/wiki/Percent_sign#Form_and_spacing
Sever Oraz Sept. 26, 2022, 8:33 p.m. UTC | #3
Apologies, I should have included a reference to FS#49104 [1]. As you both
have suggested, it is used in some locales, but more generally, by
adherents to the SI [2] and to the ISO 31-0 [4], which include the
scientific and engineering communities even in English speaking countries.
It is possibly a good idea to insert this space conditionally if the locale
in use specifies adherence to the SI; however, since there doesn't seem to
be a universally followed rule regarding this topic [3], it may be also be
a good idea to default to a well-defined and widely-followed norm where
this is regulated, such as the SI brochure and ISO 31-0. Let me know what
you think is best.

[1]:
https://bugs.archlinux.org/index.php?do=details&action=details.addvote&task_id=49104
[2]: https://en.wikipedia.org/wiki/International_System_of_Units
[3]:
https://english.stackexchange.com/questions/3281/should-there-be-a-space-before-a-percent-sign
Chris Down Sept. 28, 2022, 11:12 a.m. UTC | #4
Sever Oraz writes:
>Apologies, I should have included a reference to FS#49104 [1]. As you both
>have suggested, it is used in some locales, but more generally, by
>adherents to the SI [2] and to the ISO 31-0 [4], which include the
>scientific and engineering communities even in English speaking countries.

...but it's extremely rare to see it outside of contexts which care about this 
standard :-)

>It is possibly a good idea to insert this space conditionally if the locale
>in use specifies adherence to the SI; however, since there doesn't seem to
>be a universally followed rule regarding this topic [3], it may be also be
>a good idea to default to a well-defined and widely-followed norm where
>this is regulated, such as the SI brochure and ISO 31-0. Let me know what
>you think is best.

I am against it, at least for LANG=C or LANG=en*. It's extremely rare to 
intentionally space the percent sign like this like this in these kinds of 
user-facing application contexts in English, regardless of what some standard 
says.

As some anecdotes:

- English Wikipedia, no space: https://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style/Dates_and_numbers#Percentages
- NASA, no space: https://climate.nasa.gov/scientific-consensus/
- Royal Society, no space: https://royalsociety.org/topics-policy/projects/uk-research-and-european-union/role-of-eu-researcher-collaboration-and-mobility/snapshot-of-the-UK-research-workforce/

We do not use the spaced percent in contexts like this whatsoever, so what some 
standard says is meaningless. What's normal is descriptive, not prescriptive.
Sever Oraz Sept. 28, 2022, 11:37 a.m. UTC | #5
You bring good arguments. Would you support the spacing of the percent sign 
where the locale adheres to metric units? (i.e. LC_MEASUREMENT=1)

In countries that legally adhere to the SI, the absence of percentage sign spacing is erroneous.

Patch

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');