/* * Update the stats display */ static void stat_display(const struct xbps_fetch_cb_data *xfpd, void *cbdata) { struct xferstat *xfer = cbdata; struct timeval now; char totsize[8]; int percentage; get_time(&now); if (now.tv_sec <= xfer->last.tv_sec) return; xfer->last = now; if (xfpd->file_size == -1) { percentage = 0; snprintf(totsize, 3, "0B"); } else { percentage = (int)((double)(100.0 * (double)xfpd->file_dloaded) / (double)xfpd->file_size); (void)xbps_humanize_number(totsize, (int64_t)xfpd->file_size); } if (v_tty) fprintf(stderr, "%s: [%s %d%%] %s ETA: %s\033[K\r", xfpd->file_name, totsize, percentage, stat_bps(xfpd, xfer), stat_eta(xfpd, xfer)); else printf("%s: [%s %d%%] %s ETA: %s\n", xfpd->file_name, totsize, percentage, stat_bps(xfpd, xfer), stat_eta(xfpd, xfer)); }
void fetch_file_progress_cb(const struct xbps_fetch_cb_data *xfpd, void *cbdata) { struct xferstat *xfer = cbdata; char size[8]; if (xfpd->cb_start) { /* start transfer stats */ v_tty = isatty(STDOUT_FILENO); get_time(&xfer->start); xfer->last.tv_sec = xfer->last.tv_usec = 0; } else if (xfpd->cb_update) { /* update transfer stats */ stat_display(xfpd, xfer); } else if (xfpd->cb_end) { /* end transfer stats */ (void)xbps_humanize_number(size, (int64_t)xfpd->file_dloaded); if (v_tty) fprintf(stderr, "%s: %s [avg rate: %s]\033[K\n", xfpd->file_name, size, stat_bps(xfpd, xfer)); else printf("%s: %s [avg rate: %s]\n", xfpd->file_name, size, stat_bps(xfpd, xfer)); } }
/* * Update the stats display */ static void stat_display(struct xferstat *xs, int force) { struct timeval now; int ctty_pgrp; /* check if we're the foreground process */ if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 || (pid_t)ctty_pgrp != pgrp) return; gettimeofday(&now, NULL); if (!force && now.tv_sec <= xs->last.tv_sec) return; xs->last = now; fprintf(stderr, "\r%-46.46s", xs->name); if (xs->size <= 0) { setproctitle("%s [%s]", xs->name, stat_bytes(xs->rcvd)); fprintf(stderr, " %s", stat_bytes(xs->rcvd)); } else { setproctitle("%s [%d%% of %s]", xs->name, (int)((100.0 * xs->rcvd) / xs->size), stat_bytes(xs->size)); fprintf(stderr, "%3d%% of %s", (int)((100.0 * xs->rcvd) / xs->size), stat_bytes(xs->size)); } fprintf(stderr, " %s", stat_bps(xs)); if (xs->size > 0 && xs->rcvd > 0 && xs->last.tv_sec >= xs->start.tv_sec + 10) fprintf(stderr, " %s", stat_eta(xs)); }
/* * Finalize the transfer statistics */ static void stat_end(struct xferstat *xs) { gettimeofday(&xs->last, NULL); if (v_tty && v_level > 0) { stat_display(xs, 1); putc('\n', stderr); } else if (v_level > 0) { fprintf(stderr, " %s %s\n", stat_bytes(xs->size), stat_bps(xs)); } }
/* * Finalize the transfer statistics */ static void stat_end(struct xferstat *xs) { char bytes[16], bps[16], eta[16]; gettimeofday(&xs->last, NULL); if (v_progress) { stat_display(xs, 2); putc('\n', stderr); } else if (v_level > 0) { stat_bytes(bytes, sizeof bytes, xs->rcvd); stat_bps(bps, sizeof bps, xs); stat_eta(eta, sizeof eta, xs); fprintf(stderr, " %s %s %s\n", bytes, bps, eta); } }
/* * Update the stats display */ static void stat_display(struct xferstat *xs, int force) { char bytes[16], bps[16], eta[16]; struct timeval now; int ctty_pgrp; /* check if we're the foreground process */ if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) != 0 || (pid_t)ctty_pgrp != pgrp) return; gettimeofday(&now, NULL); if (!force && now.tv_sec <= xs->last.tv_sec) return; xs->last2 = xs->last; xs->last = now; fprintf(stderr, "\r%-46.46s", xs->name); if (xs->rcvd >= xs->size) { stat_bytes(bytes, sizeof bytes, xs->rcvd); setproctitle("%s [%s]", xs->name, bytes); fprintf(stderr, " %s", bytes); } else { stat_bytes(bytes, sizeof bytes, xs->size); setproctitle("%s [%d%% of %s]", xs->name, (int)((100.0 * xs->rcvd) / xs->size), bytes); fprintf(stderr, "%3d%% of %s", (int)((100.0 * xs->rcvd) / xs->size), bytes); } if (force == 2) { xs->lastrcvd = xs->offset; xs->last2 = xs->start; } stat_bps(bps, sizeof bps, xs); fprintf(stderr, " %s", bps); if ((xs->size > 0 && xs->rcvd > 0 && xs->last.tv_sec >= xs->start.tv_sec + 3) || force == 2) { stat_eta(eta, sizeof eta, xs); fprintf(stderr, " %s", eta); } xs->lastrcvd = xs->rcvd; }
/* * Update the stats display */ static void stat_display(struct xferstat *xs, int force) { struct timeval now; #if !defined(__minix) int ctty_pgrp; #endif /* !defined(__minix) */ /* Minix returns "Not a typewriter error" */ #if defined(TIOCGPGRP) && !defined(__minix) /* check if we're the foreground process */ if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 || (pid_t)ctty_pgrp != pgrp) return; #endif gettimeofday(&now, NULL); if (!force && now.tv_sec <= xs->last.tv_sec) return; xs->last = now; fprintf(stderr, "\r%-46.46s", xs->name); if (xs->size <= 0) { #if HAVE_SETPROCTITLE setproctitle("%s [%s]", xs->name, stat_bytes(xs->rcvd)); #endif fprintf(stderr, " %s", stat_bytes(xs->rcvd)); } else { #if HAVE_SETPROCTITLE setproctitle("%s [%d%% of %s]", xs->name, (int)((100.0 * xs->rcvd) / xs->size), stat_bytes(xs->size)); #endif fprintf(stderr, "%3d%% of %s", (int)((100.0 * xs->rcvd) / xs->size), stat_bytes(xs->size)); } fprintf(stderr, " %s", stat_bps(xs)); if (xs->size > 0 && xs->rcvd > 0 && xs->last.tv_sec >= xs->start.tv_sec + 10) fprintf(stderr, " %s", stat_eta(xs)); fflush(stderr); }