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/main.c | 7 ++++--- dimension/progressbar.c | 27 +++++++++++++++++++++++---- dimension/progressbar.h | 5 ++++- dimension/utility.h | 8 +++++--- 4 files changed, 36 insertions(+), 11 deletions(-) (limited to 'dimension') diff --git a/dimension/main.c b/dimension/main.c index fa7f5a6..cc140fd 100644 --- a/dimension/main.c +++ b/dimension/main.c @@ -196,7 +196,7 @@ main(int argc, char **argv) { } /* Realize the input */ - printf("Parsing scene ...\n"); + printf("Parsing scene ...\n"); dmnsn_scene *scene = dmnsn_realize(input_file, symtable); if (!scene) { fprintf(stderr, "Error realizing input file!\n"); @@ -242,7 +242,8 @@ main(int argc, char **argv) { return EXIT_FAILURE; } - dmnsn_progressbar("Rendering scene ", render_progress); + dmnsn_progressbar("Rendering scene with %u threads", render_progress, + scene->nthreads); if (dmnsn_finish_progress(render_progress) != 0) { dmnsn_delete_scene(scene); @@ -259,7 +260,7 @@ main(int argc, char **argv) { return EXIT_FAILURE; } - dmnsn_progressbar("Writing PNG ", output_progress); + dmnsn_progressbar("Writing PNG", output_progress); if (dmnsn_finish_progress(output_progress) != 0) { fclose(output_file); 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); diff --git a/dimension/progressbar.h b/dimension/progressbar.h index 9ee7d91..7f4948e 100644 --- a/dimension/progressbar.h +++ b/dimension/progressbar.h @@ -21,8 +21,11 @@ #define PROGRESSBAR_H #include "../libdimension/dimension.h" +#include "utility.h" /* For DMNSN_PRINTF_WARN */ /* Print a progress bar of the progress of `progress' */ -void dmnsn_progressbar(const char *str, const dmnsn_progress *progress); +void dmnsn_progressbar(const char *format, const dmnsn_progress *progress, + ...) + DMNSN_PRINTF_WARN(1, 3); #endif /* PROGRESSBAR_H */ diff --git a/dimension/utility.h b/dimension/utility.h index d4ebda4..beb818e 100644 --- a/dimension/utility.h +++ b/dimension/utility.h @@ -17,11 +17,13 @@ * along with this program. If not, see . * *************************************************************************/ -#ifndef __GNUC__ - #define __attribute__(x) +#if defined(__GNUC__) || defined(__attribute__) + #define DMNSN_PRINTF_WARN(f, a) __attribute__((format (printf, f, a))) +#else + #define DMNSN_PRINTF_WARN(f, a) #endif /* Print a parsing diagnostic to stderr */ void dmnsn_diagnostic(const char *filename, int line, int col, const char *format, ...) - __attribute__ ((format (printf, 4, 5))); + DMNSN_PRINTF_WARN(4, 5); -- cgit v1.2.3