From 64294f4ac5b1b41e95f20a827c2aead301becb92 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 23 Feb 2010 15:08:22 -0500 Subject: Make progress bars better. --- dimension/progressbar.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'dimension/progressbar.c') 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 +#include #include +#include 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); -- cgit v1.2.3