summaryrefslogtreecommitdiffstats
path: root/tests/tests.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-07-09 00:32:30 +0000
committerTavian Barnes <tavianator@gmail.com>2009-07-09 00:32:30 +0000
commit9f1b759e2fac0b15a7ef5a7a527ba66dbdc319b6 (patch)
tree2bd10bdeca7c6b1a9d11a4ef719e13b3d8ed999b /tests/tests.c
parent533fa6d364105c1f6438156d969e48aa9199cddc (diff)
downloaddimension-9f1b759e2fac0b15a7ef5a7a527ba66dbdc319b6.tar.xz
Enable drawing progres bars in the background.
Diffstat (limited to 'tests/tests.c')
-rw-r--r--tests/tests.c77
1 files changed, 76 insertions, 1 deletions
diff --git a/tests/tests.c b/tests/tests.c
index e91e290..cbf7ff8 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -229,11 +229,83 @@ dmnsn_display_flush(dmnsn_display *display)
/* Print a progress bar of the progress of `progress' */
void
-progressbar(const char *str, const dmnsn_progress *progress)
+dmnsn_progressbar(const char *str, const dmnsn_progress *progress)
+{
+ dmnsn_progress *barprogress = dmnsn_progressbar_async(str, progress);
+ if (dmnsn_finish_progress(barprogress) != 0) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH,
+ "Progress bar background thread failed.");
+ }
+}
+
+typedef struct {
+ dmnsn_progress *barprogress;
+ const char *str;
+ const dmnsn_progress *progress;
+} dmnsn_progressbar_payload;
+
+/* Progress bar thread callback */
+static void *dmnsn_progressbar_thread(void *ptr);
+
+/* Print a progress bar in the background */
+dmnsn_progress *
+dmnsn_progressbar_async(const char *str, const dmnsn_progress *progress)
+{
+ dmnsn_progress *barprogress = dmnsn_new_progress();
+ dmnsn_progressbar_payload *payload;
+
+ if (barprogress) {
+ payload = malloc(sizeof(dmnsn_progressbar_payload));
+ if (!payload) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH,
+ "Couldn't allocate progress bar payload.");
+ }
+
+ payload->barprogress = barprogress;
+ payload->str = str;
+ payload->progress = progress;
+
+ /* Create the worker thread */
+ if (pthread_create(&barprogress->thread, NULL, &dmnsn_progressbar_thread,
+ payload)
+ != 0) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH,
+ "Couldn't start progress bar background thread.");
+ }
+ }
+
+ return barprogress;
+}
+
+/* Actual progress bar implementation */
+static int dmnsn_progressbar_impl(dmnsn_progress *barprogress, const char *str,
+ const dmnsn_progress *progress);
+
+/* Progress bar thread callback */
+static void *
+dmnsn_progressbar_thread(void *ptr)
+{
+ dmnsn_progressbar_payload *payload = ptr;
+ int *retval = malloc(sizeof(int));
+ if (retval) {
+ *retval = dmnsn_progressbar_impl(payload->barprogress,
+ payload->str, payload->progress);
+ }
+ dmnsn_done_progress(payload->barprogress);
+ free(payload);
+ return retval;
+}
+
+/* Actual progress bar implementation */
+static int
+dmnsn_progressbar_impl(dmnsn_progress *barprogress,
+ const char *str, const dmnsn_progress *progress)
{
const unsigned int increments = 32;
unsigned int i;
+ dmnsn_new_progress_element(barprogress, increments);
+
printf("%s|", str);
fflush(stdout);
for (i = 0; i < increments; ++i) {
@@ -241,7 +313,10 @@ progressbar(const char *str, const dmnsn_progress *progress)
printf("=");
fflush(stdout);
+ dmnsn_increment_progress(barprogress);
}
printf("|\n");
fflush(stdout);
+
+ return 0;
}