1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-11-12 04:15:06 -05:00

* Refactored some functions to clean up variable declaration.

* Reduced magic number usage by fill_progress function (new).
* Some switch indent fixing.
* Remove use of log10 call.
This commit is contained in:
Dan McGee 2007-01-31 01:37:41 +00:00
parent 2ea88b9ff6
commit c35f713e75
2 changed files with 105 additions and 94 deletions

View File

@ -26,7 +26,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <math.h>
#include <libintl.h> #include <libintl.h>
#include <alpm.h> #include <alpm.h>
@ -42,13 +41,79 @@ extern config_t *config;
static int prevpercent=0; /* for less progressbar output */ static int prevpercent=0; /* for less progressbar output */
/* refactored function from cb_trans_evt */
static void retrieve_local(void *data1, void *data2)
{
const unsigned int maxcols = getcols();
char out[PATH_MAX];
unsigned int i;
MSG(NL, " %s [", (char*)data1);
STRNCPY(out, (char*)data2, maxcols-42);
MSG(CL, "%s", out);
for(i = strlen(out); i < maxcols-43; i++) {
MSG(CL, " ");
}
fputs(_("] 100% LOCAL "), stdout);
}
/* refactored from cb_trans_progress */
/* TODO with a little more work, we may be able to incorporate this
* into the download progress bar as well. */
static void fill_progress(const int percent, const int proglen)
{
const unsigned short chomp = alpm_option_get_chomp();
const unsigned int hashlen = proglen - 8;
const unsigned int hash = percent * hashlen / 100;
unsigned int lasthash = 0, mouth = 0;
unsigned int i;
printf(" [");
for(i = hashlen; i > 1; --i) {
/* if special progress bar enabled */
if(chomp) {
if(i > hashlen - hash) {
printf("-");
} else if(i == hashlen - hash) {
if(lasthash == hash) {
if(mouth) {
printf("\033[1;33mC\033[m");
} else {
printf("\033[1;33mc\033[m");
}
} else {
lasthash = hash;
mouth = mouth == 1 ? 0 : 1;
if(mouth) {
printf("\033[1;33mC\033[m");
} else {
printf("\033[1;33mc\033[m");
}
}
} else if(i%3 == 0) {
printf("\033[0;37mo\033[m");
} else {
printf("\033[0;37m \033[m");
}
} /* else regular progress bar */
else if(i > hashlen - hash) {
printf("#");
} else {
printf("-");
}
}
printf("] %3d%%\r", percent);
if(percent == 100) {
printf("\n");
}
}
/* Callback to handle transaction events /* Callback to handle transaction events
*/ */
void cb_trans_evt(pmtransevt_t event, void *data1, void *data2) void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
{ {
char str[LOG_STR_LEN] = ""; char str[LOG_STR_LEN] = "";
char out[PATH_MAX];
int i;
switch(event) { switch(event) {
case PM_TRANS_EVT_CHECKDEPS_START: case PM_TRANS_EVT_CHECKDEPS_START:
@ -155,14 +220,7 @@ void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
fflush(stdout); fflush(stdout);
break; break;
case PM_TRANS_EVT_RETRIEVE_LOCAL: case PM_TRANS_EVT_RETRIEVE_LOCAL:
MSG(NL, " %s [", (char*)data1); retrieve_local(data1, data2);
unsigned int maxcols = getcols();
STRNCPY(out, (char*)data2, maxcols-42);
MSG(CL, "%s", out);
for(i = strlen(out); i < maxcols-43; i++) {
MSG(CL, " ");
}
fputs(_("] 100% LOCAL "), stdout);
break; break;
} }
} }
@ -286,14 +344,12 @@ void cb_trans_conv(pmtransconv_t event, void *data1, void *data2,
} }
} }
void cb_trans_progress(pmtransprog_t event, char *pkgname, int percent, void cb_trans_progress(pmtransprog_t event, char *pkgname, const int percent,
int howmany, int remain) const int howmany, const int remain)
{ {
static int lasthash = 0, mouth = 0; /* size of line to allocate for text printing (e.g. not progressbar) */
int i, hash; const int infolen = 50;
long chomp = 0; int i, digits, textlen, pkglen, proglen;
unsigned int maxcols = getcols();
unsigned int maxpkglen, progresslen = maxcols - 57;
char *ptr = NULL; char *ptr = NULL;
if(config->noprogressbar) { if(config->noprogressbar) {
@ -306,103 +362,58 @@ void cb_trans_progress(pmtransprog_t event, char *pkgname, int percent,
set_output_padding(0); /* shut it off again */ set_output_padding(0); /* shut it off again */
} }
if (!pkgname) /* if no pkgname, percent is too high or unchanged, then return */
return; if (!pkgname || percent > 100 || percent == prevpercent) {
if (percent > 100)
return;
if(percent == prevpercent)
return; return;
}
prevpercent=percent; prevpercent=percent;
switch (event) { switch (event) {
case PM_TRANS_PROGRESS_ADD_START: case PM_TRANS_PROGRESS_ADD_START:
ptr = _("installing"); ptr = _("installing");
break; break;
case PM_TRANS_PROGRESS_UPGRADE_START: case PM_TRANS_PROGRESS_UPGRADE_START:
ptr = _("upgrading"); ptr = _("upgrading");
break; break;
case PM_TRANS_PROGRESS_REMOVE_START: case PM_TRANS_PROGRESS_REMOVE_START:
ptr = _("removing"); ptr = _("removing");
break; break;
case PM_TRANS_PROGRESS_CONFLICTS_START: case PM_TRANS_PROGRESS_CONFLICTS_START:
ptr = _("checking for file conflicts"); ptr = _("checking for file conflicts");
break; break;
} }
hash=percent*progresslen/100;
// if the package name is too long, then slice the ending /* find # of digits in package counts to scale output */
maxpkglen=46-strlen(ptr)-(3+2*(int)log10(howmany)); digits = 1;
if(strlen(pkgname)>maxpkglen) i = howmany;
pkgname[maxpkglen]='\0'; while((i /= 10)) {
++digits;
}
/* determine room left for non-digits text [not ( 1/12) part] */
textlen = infolen - 3 - (2 * digits);
/* room left for package name */
pkglen = textlen - strlen(ptr) - 1;
switch (event) { switch (event) {
case PM_TRANS_PROGRESS_ADD_START: case PM_TRANS_PROGRESS_ADD_START:
case PM_TRANS_PROGRESS_UPGRADE_START: case PM_TRANS_PROGRESS_UPGRADE_START:
case PM_TRANS_PROGRESS_REMOVE_START: case PM_TRANS_PROGRESS_REMOVE_START:
putchar('('); /* TODO clean up so digits and pkglen aren't passed twice */
for(i=0;i<(int)log10(howmany)-(int)log10(remain);i++) printf("(%*d/%*d) %s %-*.*s", digits, remain, digits, howmany,
putchar(' '); ptr, pkglen, pkglen, pkgname);
printf("%d/%d) %s %s ", remain, howmany, ptr, pkgname); break;
if (strlen(pkgname)<maxpkglen)
for (i=maxpkglen-strlen(pkgname)-1; i>0; i--) case PM_TRANS_PROGRESS_CONFLICTS_START:
putchar(' '); printf("(%*d/%*d) %-*s", digits, remain, digits, howmany,
break; textlen, ptr);
break;
case PM_TRANS_PROGRESS_CONFLICTS_START:
printf("%s (", ptr);
for(i=0;i<(int)log10(howmany)-(int)log10(remain);i++)
putchar(' ');
printf("%d/%d) ", remain, howmany);
for (i=maxpkglen; i>0; i--)
putchar(' ');
break;
} }
chomp = alpm_option_get_chomp(); /* call refactored fill progress function */
proglen = getcols() - infolen;
/* hide the cursor, prevent flicker during fancy graphics fill_progress(percent, proglen);
printf("\033[?25l\033[?1c[");
*/
printf("[");
for(i = progresslen; i > 0; --i) {
if(chomp) {
if(i > progresslen - hash) {
printf("-");
} else if(i == progresslen - hash) {
if(lasthash == hash) {
if(mouth) {
printf("\033[1;33mC\033[m");
} else {
printf("\033[1;33mc\033[m");
}
} else {
lasthash = hash;
mouth = mouth == 1 ? 0 : 1;
if(mouth) {
printf("\033[1;33mC\033[m");
} else {
printf("\033[1;33mc\033[m");
}
}
} else if(i%3 == 0) {
printf("\033[0;37mo\033[m");
} else {
printf("\033[0;37m \033[m");
}
} else if(i > progresslen - hash) {
printf("#");
} else {
printf("-");
}
}
printf("] %3d%%\r", percent);
if(percent == 100) {
printf("\n");
}
} }
/* vim: set ts=2 sw=2 noet: */ /* vim: set ts=2 sw=2 noet: */

View File

@ -29,8 +29,8 @@ void cb_trans_conv(pmtransconv_t event, void *data1, void *data2,
void *data3, int *response); void *data3, int *response);
/* callback to handle display of the progress bar for transactions */ /* callback to handle display of the progress bar for transactions */
void cb_trans_progress(pmtransprog_t event, char *pkgname, int percent, void cb_trans_progress(pmtransprog_t event, char *pkgname, const int percent,
int howmany, int remain); const int howmany, const int remain);
#endif /* _PM_TRANS_H */ #endif /* _PM_TRANS_H */