progress.c 2.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Copyright (C) 2015  Bernhard Nortmann <bernhard.nortmann@web.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "progress.h"

#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

/* Less reliable than clock_gettime, but does not require linking with -lrt */
inline double gettime(void)
{
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return tv.tv_sec + (double)tv.tv_usec / 1000000.;
}

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* Private progress state variable */

typedef struct {
	progress_cb_t callback;
	size_t total;
	size_t done;
} progress_private_t;

static progress_private_t progress = {
	.callback = NULL,
};

/* 'External' API */

void progress_start(progress_cb_t callback, size_t expected_total)
{
	progress.callback = callback;
	progress.total = expected_total;
	progress.done = 0;
}

52
/* Update progress status, passing information to the callback function. */
53
54
55
56
57
58
59
60
61
void progress_update(size_t bytes_done)
{
	progress.done += bytes_done;
	if (progress.callback)
		progress.callback(progress.total, progress.done);
}

/* Callback function implementing a simple progress bar written to stdout */
void progress_bar(size_t total, size_t done)
62
{
63
64
65
66
67
68
69
70
71
72
73
74
	static const int WIDTH = 60; /* # of characters to use for progress bar */

	float ratio = total > 0 ? (float)done / total : 0;
	int i, pos = WIDTH * ratio;

	printf("\r%3.0f%% [", ratio * 100); /* current percentage */
	for (i = 0; i < pos; i++) putchar('=');
	for (i = pos; i < WIDTH; i++) putchar(' ');
	printf("] ");

	if (done >= total) putchar('\n'); /* output newline when complete */
	fflush(stdout);
75
}