[pacman-dev] [PATCH 1/2] callback.c : less magic progress bars
1 - Explain magic numbers 2 - There was a weird off by 1 mess in the progress bar. The code supposedly shared the width between 50 chars for text (textlen) and the rest for the progress bar (proglen = getcols() - textlen). But the code actually used textlen + 1 for the text and proglen - 1 for the progress bar (with haslen=1, the progress bar was actually empty), which was a bit confusing so I changed it. Signed-off-by: Xavier Chantry <shiningxc@gmail.com> --- src/pacman/callback.c | 23 +++++++++++++---------- 1 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/pacman/callback.c b/src/pacman/callback.c index 15f5423..a40a702 100644 --- a/src/pacman/callback.c +++ b/src/pacman/callback.c @@ -90,20 +90,20 @@ static float get_update_timediff(int first_call) static void fill_progress(const int bar_percent, const int disp_percent, const int proglen) { - const unsigned int hashlen = proglen - 8; - const unsigned int hash = bar_percent * hashlen / 100; - static unsigned int lasthash = 0, mouth = 0; - unsigned int i; + /* 8 = 1 space + 1 [ + 1 ] + 5 for percent */ + const int hashlen = proglen - 8; + const int hash = bar_percent * hashlen / 100; + static int lasthash = 0, mouth = 0; + int i; if(bar_percent == 0) { lasthash = 0; mouth = 0; } - /* magic numbers, how I loathe thee */ - if(proglen > 8) { + if(hashlen > 0) { printf(" ["); - for(i = hashlen; i > 1; --i) { + for(i = hashlen; i > 0; --i) { /* if special progress bar enabled */ if(config->chomp) { if(i > hashlen - hash) { @@ -139,7 +139,8 @@ static void fill_progress(const int bar_percent, const int disp_percent, printf("]"); } /* print display percent after progress bar */ - if(proglen > 5) { + /* 5 = 1 space + 3 digits + 1 % */ + if(proglen >= 5) { printf(" %3d%%", disp_percent); } @@ -376,7 +377,7 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent, ++digits; } /* determine room left for non-digits text [not ( 1/12) part] */ - textlen = infolen - 3 - (2 * digits); + textlen = infolen - 3 /* (/) */ - (2 * digits) - 1 /* space */; /* In order to deal with characters from all locales, we have to worry * about wide characters and their column widths. A lot of stuff is @@ -451,7 +452,8 @@ void cb_dl_total(off_t total) void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total) { const int infolen = 50; - const int filenamelen = infolen - 27; + /* explanation of magic 28 number at the end */ + const int filenamelen = infolen - 28; char *fname, *p; /* used for wide character width determination and printing */ int len, wclen, wcwid, padwid; @@ -613,6 +615,7 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total) } } + /* 1 space + filenamelen + 1 space + 7 for size + 1 + 7 for rate + 2 for /s + 1 space + 8 for eta */ printf(" %ls%-*s %6.1f%c %#6.1f%c/s %02u:%02u:%02u", wcfname, padwid, "", f_xfered, xfered_size, rate, rate_size, eta_h, eta_m, eta_s); -- 1.6.5.6
This fixes FS#17523 We always used a fixed value of 50 for textlen, which is often not enough for download progress bar. At least we can use a bigger width on large terminal (e.g. 60% of width) and keep 50 as minimum. before: nautilus-2.28.4-1-x... 5.7M 789.2K/s 00:00:07 [####################################] 100% after: nautilus-2.28.4-1-x86_64 5.7M 770.7K/s 00:00:08 [##############################] 100% Signed-off-by: Xavier Chantry <shiningxc@gmail.com> --- src/pacman/callback.c | 19 +++++++++++++++---- 1 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/pacman/callback.c b/src/pacman/callback.c index a40a702..b021d9d 100644 --- a/src/pacman/callback.c +++ b/src/pacman/callback.c @@ -320,7 +320,7 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent, float timediff; /* size of line to allocate for text printing (e.g. not progressbar) */ - const int infolen = 50; + int infolen; int tmp, digits, textlen; char *opr = NULL; /* used for wide character width determination and printing */ @@ -331,6 +331,11 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent, return; } + infolen = getcols() * 6 / 10; + if (infolen < 50) { + infolen = 50; + } + if(percent == 0) { timediff = get_update_timediff(1); } else { @@ -451,9 +456,8 @@ void cb_dl_total(off_t total) /* callback to handle display of download progress */ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total) { - const int infolen = 50; - /* explanation of magic 28 number at the end */ - const int filenamelen = infolen - 28; + int infolen; + int filenamelen; char *fname, *p; /* used for wide character width determination and printing */ int len, wclen, wcwid, padwid; @@ -474,6 +478,13 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total) return; } + infolen = getcols() * 6 / 10; + if (infolen < 50) { + infolen = 50; + } + /* explanation of magic 28 number at the end */ + filenamelen = infolen - 28; + /* only use TotalDownload if enabled and we have a callback value */ if(config->totaldownload && list_total) { /* sanity check */ -- 1.6.5.6
participants (1)
-
Xavier Chantry