-
Notifications
You must be signed in to change notification settings - Fork 1
/
athr_widget_perc.c
75 lines (61 loc) · 1.65 KB
/
athr_widget_perc.c
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
#include "athr_widget_perc.h"
#include "athr_array.h"
#include "athr_widget.h"
#include "athr_widget_perc.h"
#include <assert.h>
#include <string.h>
/* Non null-terminated sequence of characters with percentage.
* From 0% to 100%. Buff needs to have room for 4 characters. */
static void perc_buff(char *buff, unsigned perc)
{
assert(perc <= 100);
if (perc / 100)
*buff = '1';
else
*buff = ' ';
++buff;
if (perc / 10)
*buff = (char)('0' + (perc % 100) / 10);
else
*buff = ' ';
++buff;
*buff = (char)('0' + (perc % 100 % 10));
++buff;
*buff = '%';
}
static void update(struct athr_widget *x, double consumed, double speed)
{
(void)speed;
struct athr_widget_perc *eta = x->derived;
unsigned perc = 0;
eta->consumed = consumed;
if (eta->consumed == 1.0)
perc = 100;
else
perc = (unsigned)(eta->consumed * 100);
memcpy(x->canvas.buff, ATHR_WIDGET_PERC_EXAMPLE,
athr_array_size(ATHR_WIDGET_PERC_EXAMPLE) - 1);
/* Skip the first space */
perc_buff(x->canvas.buff + 1, perc);
}
static void finish(struct athr_widget *x, double total_elapsed)
{
(void)total_elapsed;
update(x, 1.0f, 0.0f);
}
static unsigned min_len(struct athr_widget const *x)
{
(void)x;
return ATHR_WIDGET_PERC_LEN;
}
static unsigned max_len(struct athr_widget const *x)
{
(void)x;
return ATHR_WIDGET_PERC_LEN;
}
static struct athr_widget_vtable const vtable = {update, finish, min_len,
max_len};
void athr_widget_perc_create(struct athr_widget_perc *x)
{
widget_setup((struct athr_widget *)x, &vtable);
}