From: Severo Raz <[email protected]>

Also add explanatory comment and reduce use of magic numbers.

Signed-off-by: Severo Raz <[email protected]>
---
 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

_______________________________________________
Pacman-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to