summaryrefslogtreecommitdiffstats
path: root/dimension/progressbar.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-02-23 15:08:22 -0500
committerTavian Barnes <tavianator@gmail.com>2010-02-23 15:08:22 -0500
commit64294f4ac5b1b41e95f20a827c2aead301becb92 (patch)
treeb116b3bd71f634de1bec72b2d60501285e13071d /dimension/progressbar.c
parentc76c0f017fa884c4b9a04ff7fa3b43d331c82d84 (diff)
downloaddimension-64294f4ac5b1b41e95f20a827c2aead301becb92.tar.xz
Make progress bars better.
Diffstat (limited to 'dimension/progressbar.c')
-rw-r--r--dimension/progressbar.c27
1 files changed, 23 insertions, 4 deletions
diff --git a/dimension/progressbar.c b/dimension/progressbar.c
index fb26d4c..c366d4d 100644
--- a/dimension/progressbar.c
+++ b/dimension/progressbar.c
@@ -18,16 +18,35 @@
*************************************************************************/
#include "progressbar.h"
+#include <sys/ioctl.h>
+#include <stdarg.h>
#include <stdio.h>
+#include <unistd.h>
void
-dmnsn_progressbar(const char *str, const dmnsn_progress *progress)
+dmnsn_progressbar(const char *format, const dmnsn_progress *progress, ...)
{
- const unsigned int increments = 32;
- unsigned int i;
+ va_list ap;
+ va_start(ap, progress);
+
+ int len = vprintf(format, ap) + 1;
+ if (len < 1)
+ len = 1;
+ printf(" ");
+
+ va_end(ap);
+
+ unsigned int increments = 48;
+
+ /* Try to fill the terminal with the progress bar; this is non-portable */
+ struct winsize ws;
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0) {
+ increments = ws.ws_col - (len % ws.ws_col);
+ }
- printf("%s", str);
fflush(stdout);
+
+ unsigned int i;
for (i = 0; i < increments; ++i) {
dmnsn_wait_progress(progress, ((double)(i + 1))/increments);