File: gauge.c

package info (click to toggle)
reiser4progs 1.0.6-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,348 kB
  • ctags: 3,714
  • sloc: ansic: 33,468; sh: 8,489; makefile: 1,012
file content (130 lines) | stat: -rw-r--r-- 2,213 bytes parent folder | download | duplicates (4)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
   reiser4progs/COPYING.
   
   gauge.c -- common for all progs gauge fucntions. */

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

#include <aal/libaal.h>
#include <misc/misc.h>

#define GAUGE_BITS_SIZE 4

aal_gauge_t *current_gauge = NULL;

static int misc_gauge_time(aal_gauge_time_t *time) {
	struct timeval t;
	uint64_t delta;
	uint64_t usec;
	
	if (!time->gap) 
		return 1;
	
	gettimeofday(&t, NULL);

	usec = t.tv_usec / 1000;
	
	if ((uint64_t)t.tv_sec < time->sec)
		goto next;
	
	if (((uint64_t)t.tv_sec == time->sec) && (usec < time->misec))
		goto next;

	delta = (t.tv_sec  - time->sec) * 1000 + (usec - time->misec);
	
	if (delta > time->gap) 
		goto next;
	
	return 0;
	
 next:
	time->sec = t.tv_sec;
	time->misec = usec;
	return 1;
}

static inline void misc_gauge_blit(void) {
	static short bitc = 0;
	static const char bits[] = "|/-\\";

	fprintf(stderr, "%c", bits[bitc]);
	bitc++;
	bitc %= GAUGE_BITS_SIZE;
}

void misc_progress_handler(aal_gauge_t *gauge) {
	if (!isatty(2))
		return;

	setlinebuf(stderr);

	if (gauge->state == GS_ACTIVE ||
	    gauge->state == GS_RESUME)
	{
		/* Show gauge once per rate->time.interval. */
		if (!misc_gauge_time(&gauge->time))
			return;
	}

	misc_wipe_line(stderr);

	if (gauge->state == GS_PAUSE)
		goto done;

	if (gauge->label[0] != '\0')
		fprintf(stderr, "\r%s", gauge->label);
	
	if (gauge->state == GS_DONE) {
		current_gauge = NULL;
		
		if (gauge->label[0] != '\0')
			fprintf(stderr, "done\n");

		goto done;
	}

	if (gauge->state == GS_START) {
		current_gauge = gauge;
		misc_gauge_time(&gauge->time);
		goto done;
	}

	if (gauge->value_func)
		gauge->value_func(gauge);
	
	if (gauge->value != -1) {
		uint32_t width, count;
		
		width = misc_screen_width();
		if (width < 10)
			goto done;
		
		width -= 10;
		
		if (width > 50)
			width = 50;
		
		fprintf(stderr, "[");
		count = width * gauge->value / 100;
		width -=  count;
		while (count--) {
			fprintf(stderr, "=");
		}
		
		misc_gauge_blit();
		
		while(width--) {
			fprintf(stderr, " ");
		}
		
		fprintf(stderr, "] %lld%%", gauge->value);
	} else {
		misc_gauge_blit();
	}

 done:
	fflush(stderr);
}