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,11 +3,8 @@ | ||
3 | #include <string.h> | 3 | #include <string.h> |
4 | #include <sys/types.h> | 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 | typedef enum { | 9 | typedef enum { |
13 | CHARGED = 0, | 10 | CHARGED = 0, |
@@ -17,11 +14,19 @@ typedef enum { | @@ -17,11 +14,19 @@ typedef enum { | ||
17 | UNKNOWN = -1 | 14 | UNKNOWN = -1 |
18 | } State; | 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 | void battery_state(int *nbats, int bat[], State state[]) { | 24 | void battery_state(int *nbats, int bat[], State state[]) { |
25 | +#if defined(__linux__) | ||
22 | FILE *fd; | 26 | FILE *fd; |
23 | - int i = 0; | ||
24 | - do { | 27 | + int i; |
28 | + | ||
29 | + for (i = 0; i < MAXBAT; i++) { | ||
25 | char filename[50], status[50]; | 30 | char filename[50], status[50]; |
26 | 31 | ||
27 | // get current capacity | 32 | // get current capacity |
@@ -45,15 +50,9 @@ void battery_state(int *nbats, int bat[], State state[]) { | @@ -45,15 +50,9 @@ void battery_state(int *nbats, int bat[], State state[]) { | ||
45 | state[i] = CHARGING; | 50 | state[i] = CHARGING; |
46 | else | 51 | else |
47 | state[i] = UNKNOWN; | 52 | state[i] = UNKNOWN; |
48 | - | ||
49 | - // next | ||
50 | - i++; | ||
51 | - } while (1); | 53 | + } |
52 | *nbats = i; | 54 | *nbats = i; |
53 | -} | ||
54 | #elif defined(__FreeBSD__) | 55 | #elif defined(__FreeBSD__) |
55 | -#include <sys/sysctl.h> | ||
56 | -void battery_state(int *nbats, int bat[], State state[]) { | ||
57 | size_t len = sizeof(int); | 56 | size_t len = sizeof(int); |
58 | int status, life = 0; | 57 | int status, life = 0; |
59 | 58 | ||
@@ -67,8 +66,10 @@ void battery_state(int *nbats, int bat[], State state[]) { | @@ -67,8 +66,10 @@ void battery_state(int *nbats, int bat[], State state[]) { | ||
67 | bat[0] = life; | 66 | bat[0] = life; |
68 | state[0] = status; | 67 | state[0] = status; |
69 | } | 68 | } |
70 | -} | 69 | +#else |
70 | +#error "Unsupported" | ||
71 | #endif | 71 | #endif |
72 | +} | ||
72 | 73 | ||
73 | int main() { | 74 | int main() { |
74 | char *color; | 75 | char *color; |
@@ -76,11 +77,14 @@ int main() { | @@ -76,11 +77,14 @@ int main() { | ||
76 | State state[2]; | 77 | State state[2]; |
77 | 78 | ||
78 | battery_state(&nbats, life, state); | 79 | battery_state(&nbats, life, state); |
80 | + | ||
79 | for (int i = 0; i < nbats; i++) { | 81 | for (int i = 0; i < nbats; i++) { |
82 | + if (i > 0) | ||
83 | + printf("%s", separator); | ||
80 | 84 | ||
81 | switch (state[i]) { | 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 | break; | 88 | break; |
85 | case DISCHARGING: /* discharging */ | 89 | case DISCHARGING: /* discharging */ |
86 | if (life[i] >= 50) | 90 | if (life[i] >= 50) |
@@ -90,17 +94,17 @@ int main() { | @@ -90,17 +94,17 @@ int main() { | ||
90 | else | 94 | else |
91 | color = "colour196"; /* red */ | 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 | life[i]); | 98 | life[i]); |
95 | break; | 99 | break; |
96 | case CHARGING: /* charging */ | 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 | break; | 102 | break; |
99 | case DISCONNECTED: /* disconnected */ | 103 | case DISCONNECTED: /* disconnected */ |
100 | - printf("\uf492 "); | 104 | + printf("\uf492"); |
101 | break; | 105 | break; |
102 | default: /* unknown code */ | 106 | default: /* unknown code */ |
103 | - printf("\uf590 "); | 107 | + printf("\uf590"); |
104 | } | 108 | } |
105 | } | 109 | } |
106 | 110 |