[PATCH] pacman: Add space before percent sign in download progress string.
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(-) 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'); -- 2.37.3
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
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
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-bef...
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#Pe... - NASA, no space: https://climate.nasa.gov/scientific-consensus/ - Royal Society, no space: https://royalsociety.org/topics-policy/projects/uk-research-and-european-uni... 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.
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.
participants (4)
-
Allan McRae
-
Chris Down
-
Sever Oraz
-
severoraz@gmail.com