bin/xbps-install/fetch_cb.c: optimize: only call isatty() once.

This commit is contained in:
Juan RP 2014-09-18 10:21:24 +02:00
parent 2550d3d006
commit 20326738ed

View File

@ -43,6 +43,8 @@
#include <xbps.h> #include <xbps.h>
#include "defs.h" #include "defs.h"
static int v_tty; /* stderr is a tty */
static void static void
get_time(struct timeval *tvp) get_time(struct timeval *tvp)
{ {
@ -142,7 +144,7 @@ stat_display(const struct xbps_fetch_cb_data *xfpd, void *cbdata)
(double)xfpd->file_dloaded) / (double)xfpd->file_size); (double)xfpd->file_dloaded) / (double)xfpd->file_size);
(void)xbps_humanize_number(totsize, (int64_t)xfpd->file_size); (void)xbps_humanize_number(totsize, (int64_t)xfpd->file_size);
} }
if (isatty(fileno(stdout))) if (v_tty)
fprintf(stderr, "%s: [%s %d%%] %s ETA: %s\033[K\r", fprintf(stderr, "%s: [%s %d%%] %s ETA: %s\033[K\r",
xfpd->file_name, totsize, percentage, xfpd->file_name, totsize, percentage,
stat_bps(xfpd, xfer), stat_eta(xfpd, xfer)); stat_bps(xfpd, xfer), stat_eta(xfpd, xfer));
@ -160,6 +162,7 @@ fetch_file_progress_cb(const struct xbps_fetch_cb_data *xfpd, void *cbdata)
if (xfpd->cb_start) { if (xfpd->cb_start) {
/* start transfer stats */ /* start transfer stats */
v_tty = isatty(STDERR_FILENO);
get_time(&xfer->start); get_time(&xfer->start);
xfer->last.tv_sec = xfer->last.tv_usec = 0; xfer->last.tv_sec = xfer->last.tv_usec = 0;
} else if (xfpd->cb_update) { } else if (xfpd->cb_update) {
@ -168,11 +171,12 @@ fetch_file_progress_cb(const struct xbps_fetch_cb_data *xfpd, void *cbdata)
} else if (xfpd->cb_end) { } else if (xfpd->cb_end) {
/* end transfer stats */ /* end transfer stats */
(void)xbps_humanize_number(size, (int64_t)xfpd->file_dloaded); (void)xbps_humanize_number(size, (int64_t)xfpd->file_dloaded);
if (isatty(fileno(stdout))) if (v_tty)
fprintf(stderr, "%s: %s [avg rate: %s]\033[K\n", fprintf(stderr, "%s: %s [avg rate: %s]\033[K\n",
xfpd->file_name, size, stat_bps(xfpd, xfer)); xfpd->file_name, size, stat_bps(xfpd, xfer));
else else
printf("%s: %s [avg rate: %s]\n", printf("%s: %s [avg rate: %s]\n",
xfpd->file_name, size, stat_bps(xfpd, xfer)); xfpd->file_name, size, stat_bps(xfpd, xfer));
} }
} }