1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-23 08:18:51 -05:00

* Hopefully fixed the download progres bar for real. We should no longer

have issues with crazy speeds being displayed.
* Minor string updates to remove unnecessary 1-off translations.
This commit is contained in:
Dan McGee 2007-02-08 20:44:47 +00:00
parent a94e22d924
commit b67e10d6b3
3 changed files with 78 additions and 71 deletions

View File

@ -29,6 +29,7 @@
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include <libintl.h> #include <libintl.h>
#include <math.h>
#include <alpm.h> #include <alpm.h>
/* pacman */ /* pacman */
@ -52,59 +53,58 @@ void log_progress(const char *filename, int xfered, int total)
const int infolen = 50; const int infolen = 50;
char *fname, *p; char *fname, *p;
struct timeval current_time; float rate = 0.0, timediff = 0.0;
float rate = 0.0;
unsigned int eta_h = 0, eta_m = 0, eta_s = 0; unsigned int eta_h = 0, eta_m = 0, eta_s = 0;
float total_timediff, timediff; int percent;
char rate_size = 'K', xfered_size = 'K';
if(config->noprogressbar) { if(config->noprogressbar) {
return; return;
} }
int percent = (int)((float)xfered) / ((float)total) * 100; /* this is basically a switch on xferred: 0, total, and anything else */
if(xfered == 0) {
if(xfered == 0) { /* set default starting values */
set_output_padding(1); /* we need padding from pm_fprintf output */
gettimeofday(&initial_time, NULL); gettimeofday(&initial_time, NULL);
xfered_last = 0; xfered_last = 0;
rate_last = 0.0; rate_last = 0.0;
timediff = get_update_timediff(1); timediff = get_update_timediff(1);
} else { rate = 0.0;
timediff = get_update_timediff(0); eta_s = 0;
} set_output_padding(1); /* we need padding from pm_fprintf output */
} else if(xfered == total) {
if(percent > 0 && percent < 100 && !timediff) {
/* only update the progress bar when
* a) we first start
* b) we end the progress
* c) it has been long enough since the last call
*/
return;
}
gettimeofday(&current_time, NULL);
total_timediff = current_time.tv_sec-initial_time.tv_sec
+ (float)(current_time.tv_usec-initial_time.tv_usec) / 1000000.0;
if(xfered == total) {
/* compute final values */ /* compute final values */
rate = (float)total / (total_timediff * 1024.0); struct timeval current_time;
if(total_timediff < 1.0 && total_timediff > 0.5) { float diff_sec, diff_usec;
/* round up so we don't display 00:00:00 for quick downloads all the time*/
eta_s = 1; gettimeofday(&current_time, NULL);
} else { diff_sec = current_time.tv_sec - initial_time.tv_sec;
eta_s = (unsigned int)total_timediff; diff_usec = current_time.tv_usec - initial_time.tv_usec;
} timediff = diff_sec + (diff_usec / 1000000.0);
rate = (float)total / (timediff * 1024.0);
/* round elapsed time to the nearest second */
eta_s = (int)floorf(timediff + 0.5);
set_output_padding(0); /* shut off padding */ set_output_padding(0); /* shut off padding */
} else { } else {
/* compute current average values */
timediff = get_update_timediff(0);
if(timediff < UPDATE_SPEED_SEC) {
/* return if the calling interval was too short */
return;
}
rate = (float)(xfered - xfered_last) / (timediff * 1024.0); rate = (float)(xfered - xfered_last) / (timediff * 1024.0);
/* average rate to reduce jumpiness */
rate = (float)(rate + 2*rate_last) / 3; rate = (float)(rate + 2*rate_last) / 3;
eta_s = (unsigned int)(total - xfered) / (rate * 1024.0); eta_s = (unsigned int)(total - xfered) / (rate * 1024.0);
rate_last = rate;
xfered_last = xfered;
} }
rate_last = rate; percent = (int)((float)xfered) / ((float)total) * 100;
xfered_last = xfered;
/* fix up time for display */ /* fix up time for display */
eta_h = eta_s / 3600; eta_h = eta_s / 3600;
eta_s -= eta_h * 3600; eta_s -= eta_h * 3600;
@ -121,8 +121,7 @@ void log_progress(const char *filename, int xfered, int total)
/* Awesome formatting for progress bar. We need a mess of Kb->Mb->Gb stuff /* Awesome formatting for progress bar. We need a mess of Kb->Mb->Gb stuff
* here. We'll use limit of 2048 for each until we get some empirical */ * here. We'll use limit of 2048 for each until we get some empirical */
char rate_size = 'K'; /* rate_size = 'K'; was set above */
char xfered_size = 'K';
if(rate > 2048.0) { if(rate > 2048.0) {
rate /= 1024.0; rate /= 1024.0;
rate_size = 'M'; rate_size = 'M';
@ -134,6 +133,7 @@ void log_progress(const char *filename, int xfered, int total)
} }
xfered /= 1024; /* convert to K by default */ xfered /= 1024; /* convert to K by default */
/* xfered_size = 'K'; was set above */
if(xfered > 2048) { if(xfered > 2048) {
xfered /= 1024; xfered /= 1024;
xfered_size = 'M'; xfered_size = 'M';

View File

@ -155,9 +155,9 @@ void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
break; break;
case PM_TRANS_EVT_SCRIPTLET_DONE: case PM_TRANS_EVT_SCRIPTLET_DONE:
if(!(long)data1) { if(!(long)data1) {
MSG(CL, _(" done.\n")); MSG(CL, _("done.\n"));
} else { } else {
MSG(CL, _(" failed.\n")); MSG(CL, _("failed.\n"));
} }
break; break;
case PM_TRANS_EVT_PRINTURI: case PM_TRANS_EVT_PRINTURI:

View File

@ -268,7 +268,9 @@ void display_targets(alpm_list_t *syncpkgs)
char *str; char *str;
alpm_list_t *i, *j; alpm_list_t *i, *j;
alpm_list_t *targets = NULL, *to_remove = NULL; alpm_list_t *targets = NULL, *to_remove = NULL;
unsigned long totalsize = 0, totalisize = 0, totalrsize = 0; /* TODO these are some messy variable names */
unsigned long size = 0, isize = 0, rsize = 0;
double mbsize = 0.0, mbisize = 0.0, mbrsize = 0.0;
for(i = syncpkgs; i; i = alpm_list_next(i)) { for(i = syncpkgs; i; i = alpm_list_next(i)) {
pmsyncpkg_t *sync = alpm_list_getdata(i); pmsyncpkg_t *sync = alpm_list_getdata(i);
@ -285,48 +287,53 @@ void display_targets(alpm_list_t *syncpkgs)
const char *name = alpm_pkg_get_name(rp); const char *name = alpm_pkg_get_name(rp);
if(!alpm_list_find_str(to_remove, name)) { if(!alpm_list_find_str(to_remove, name)) {
totalrsize += alpm_pkg_get_isize(rp); rsize += alpm_pkg_get_isize(rp);
to_remove = alpm_list_add(to_remove, strdup(name)); to_remove = alpm_list_add(to_remove, strdup(name));
} }
} }
} }
totalsize += alpm_pkg_get_size(pkg); size += alpm_pkg_get_size(pkg);
totalisize += alpm_pkg_get_isize(pkg); isize += alpm_pkg_get_isize(pkg);
asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
targets = alpm_list_add(targets, str); targets = alpm_list_add(targets, str);
} }
/* Convert byte sizes to MB */
mbsize = (double)(size) / (1024.0 * 1024.0);
mbisize = (double)(isize) / (1024.0 * 1024.0);
mbrsize = (double)(rsize) / (1024.0 * 1024.0);
if(to_remove) { if(to_remove) {
MSG(NL, "\n"); /* TODO ugly hack. printing a single NL should be easy */ MSG(NL, "\n"); /* TODO ugly hack. printing a single NL should be easy */
list_display(_("Remove:"), to_remove); list_display(_("Remove:"), to_remove);
FREELIST(to_remove); FREELIST(to_remove);
double rmb = (double)(totalrsize) / (1024.0 * 1024.0); if(mbrsize > 0) {
if(rmb > 0) { /* round up if size is really small */
if(rmb < 0.1) { if(mbrsize < 0.1) {
rmb = 0.1; mbrsize = 0.1;
} }
MSG(NL, _("\nTotal Removed Size: %.2f MB\n"), rmb); MSG(NL, _("\nTotal Removed Size: %.2f MB\n"), mbrsize);
} }
} }
MSG(NL, "\n"); /* TODO ugly hack. printing a single NL should be easy */ MSG(NL, "\n"); /* TODO ugly hack. printing a single NL should be easy */
list_display(_("Targets:"), targets); list_display(_("Targets:"), targets);
double mb = (double)(totalsize) / (1024.0 * 1024.0); /* round up if size is really small */
if(mb < 0.1) { if(mbsize < 0.1) {
mb = 0.1; mbsize = 0.1;
} }
MSG(NL, _("\nTotal Package Size: %.2f MB\n"), mb); MSG(NL, _("\nTotal Package Size: %.2f MB\n"), mbsize);
double umb = (double)(totalisize) / (1024.0 * 1024.0); if(mbisize > mbsize) {
if(umb > 0) { /*round up if size is really small */
if(umb < 0.1) { if(mbisize < 0.1) {
umb = 0.1; mbisize = 0.1;
} }
MSG(NL, _("Total Installed Size: %.2f MB\n"), umb); MSG(NL, _("Total Installed Size: %.2f MB\n"), mbisize);
} }
FREELIST(targets); FREELIST(targets);
@ -342,29 +349,29 @@ float get_update_timediff(int first_call)
{ {
float retval = 0.0; float retval = 0.0;
static struct timeval last_time = {0}; static struct timeval last_time = {0};
struct timeval this_time;
/* on first call, simply set the last time and return */
if(first_call) { if(first_call) {
/* always update on the first call */ gettimeofday(&last_time, NULL);
retval = 1.0;
} else { } else {
struct timeval this_time;
float diff_sec, diff_usec;
gettimeofday(&this_time, NULL); gettimeofday(&this_time, NULL);
diff_sec = this_time.tv_sec - last_time.tv_sec;
diff_usec = this_time.tv_usec - last_time.tv_usec;
float diff_sec = this_time.tv_sec - last_time.tv_sec; retval = diff_sec + (diff_usec / 1000000.0);
float diff_usec = this_time.tv_usec - last_time.tv_usec;
retval = diff_sec + (diff_usec / 1000000.0f);
/*printf("update time: %fs %fus = %f\n", diff_sec, diff_usec, retval);*/
/* return 0 and do not update last_time if interval was too short */
if(retval < UPDATE_SPEED_SEC) { if(retval < UPDATE_SPEED_SEC) {
/* maintain the last_time value for the next call */ retval = 0.0;
return(0.0); } else {
last_time = this_time;
/* printf("\nupdate retval: %f\n", retval); DEBUG*/
} }
} }
/* set the time for the next call */
gettimeofday(&last_time, NULL);
return(retval); return(retval);
} }