Commit fbcc53ca48c7742457915ed1c9d9e590fc0356e2
1 parent
8e1ef252
Exists in
master
refactor
Showing
1 changed file
with
26 additions
and
22 deletions
Show diff stats
battery.c
... | ... | @@ -3,11 +3,8 @@ |
3 | 3 | #include <string.h> |
4 | 4 | #include <sys/types.h> |
5 | 5 | |
6 | -const char *icon[] = { | |
7 | - "\uf58d", /* 0% */ | |
8 | - "\uf579", "\uf57a", "\uf57b", "\uf57c", "\uf57d", /* 50% */ | |
9 | - "\uf57e", "\uf57f", "\uf580", "\uf581", "\uf578", /* 100% */ | |
10 | -}; | |
6 | +// maximum number of batteries supported | |
7 | +#define MAXBAT 2 | |
11 | 8 | |
12 | 9 | typedef enum { |
13 | 10 | CHARGED = 0, |
... | ... | @@ -17,11 +14,19 @@ typedef enum { |
17 | 14 | UNKNOWN = -1 |
18 | 15 | } State; |
19 | 16 | |
20 | -#if defined(__linux__) | |
17 | +const char *icon[] = { | |
18 | + "\uf58d", /* 0% */ | |
19 | + "\uf579", "\uf57a", "\uf57b", "\uf57c", "\uf57d", /* 50% */ | |
20 | + "\uf57e", "\uf57f", "\uf580", "\uf581", "\uf578", /* 100% */ | |
21 | +}; | |
22 | +const char separator[] = " "; // space between battery indicators | |
23 | + | |
21 | 24 | void battery_state(int *nbats, int bat[], State state[]) { |
25 | +#if defined(__linux__) | |
22 | 26 | FILE *fd; |
23 | - int i = 0; | |
24 | - do { | |
27 | + int i; | |
28 | + | |
29 | + for (i = 0; i < MAXBAT; i++) { | |
25 | 30 | char filename[50], status[50]; |
26 | 31 | |
27 | 32 | // get current capacity |
... | ... | @@ -45,15 +50,9 @@ void battery_state(int *nbats, int bat[], State state[]) { |
45 | 50 | state[i] = CHARGING; |
46 | 51 | else |
47 | 52 | state[i] = UNKNOWN; |
48 | - | |
49 | - // next | |
50 | - i++; | |
51 | - } while (1); | |
53 | + } | |
52 | 54 | *nbats = i; |
53 | -} | |
54 | 55 | #elif defined(__FreeBSD__) |
55 | -#include <sys/sysctl.h> | |
56 | -void battery_state(int *nbats, int bat[], State state[]) { | |
57 | 56 | size_t len = sizeof(int); |
58 | 57 | int status, life = 0; |
59 | 58 | |
... | ... | @@ -67,8 +66,10 @@ void battery_state(int *nbats, int bat[], State state[]) { |
67 | 66 | bat[0] = life; |
68 | 67 | state[0] = status; |
69 | 68 | } |
70 | -} | |
69 | +#else | |
70 | +#error "Unsupported" | |
71 | 71 | #endif |
72 | +} | |
72 | 73 | |
73 | 74 | int main() { |
74 | 75 | char *color; |
... | ... | @@ -76,11 +77,14 @@ int main() { |
76 | 77 | State state[2]; |
77 | 78 | |
78 | 79 | battery_state(&nbats, life, state); |
80 | + | |
79 | 81 | for (int i = 0; i < nbats; i++) { |
82 | + if (i > 0) | |
83 | + printf("%s", separator); | |
80 | 84 | |
81 | 85 | switch (state[i]) { |
82 | - case CHARGED: /* fully charged */ | |
83 | - printf("%s %d%% ", icon[life[i] / 10], life[i]); | |
86 | + case CHARGED: /* fully charged */ | |
87 | + printf("%s %d%%", icon[life[i] / 10], life[i]); | |
84 | 88 | break; |
85 | 89 | case DISCHARGING: /* discharging */ |
86 | 90 | if (life[i] >= 50) |
... | ... | @@ -90,17 +94,17 @@ int main() { |
90 | 94 | else |
91 | 95 | color = "colour196"; /* red */ |
92 | 96 | |
93 | - printf("#[fg=%s]%s#[fg=default] %d%% ", color, icon[life[i] / 10], | |
97 | + printf("#[fg=%s]%s#[fg=default] %d%%", color, icon[life[i] / 10], | |
94 | 98 | life[i]); |
95 | 99 | break; |
96 | 100 | case CHARGING: /* charging */ |
97 | - printf("#[fg=yellow]\uf583#[fg=default] %d%% ", life[i]); | |
101 | + printf("#[fg=yellow]\uf583#[fg=default] %d%%", life[i]); | |
98 | 102 | break; |
99 | 103 | case DISCONNECTED: /* disconnected */ |
100 | - printf("\uf492 "); | |
104 | + printf("\uf492"); | |
101 | 105 | break; |
102 | 106 | default: /* unknown code */ |
103 | - printf("\uf590 "); | |
107 | + printf("\uf590"); | |
104 | 108 | } |
105 | 109 | } |
106 | 110 | ... | ... |