diff --git a/battery.c b/battery.c index fc8efe6..4bcaac4 100644 --- a/battery.c +++ b/battery.c @@ -3,11 +3,8 @@ #include #include -const char *icon[] = { - "\uf58d", /* 0% */ - "\uf579", "\uf57a", "\uf57b", "\uf57c", "\uf57d", /* 50% */ - "\uf57e", "\uf57f", "\uf580", "\uf581", "\uf578", /* 100% */ -}; +// maximum number of batteries supported +#define MAXBAT 2 typedef enum { CHARGED = 0, @@ -17,11 +14,19 @@ typedef enum { UNKNOWN = -1 } State; -#if defined(__linux__) +const char *icon[] = { + "\uf58d", /* 0% */ + "\uf579", "\uf57a", "\uf57b", "\uf57c", "\uf57d", /* 50% */ + "\uf57e", "\uf57f", "\uf580", "\uf581", "\uf578", /* 100% */ +}; +const char separator[] = " "; // space between battery indicators + void battery_state(int *nbats, int bat[], State state[]) { +#if defined(__linux__) FILE *fd; - int i = 0; - do { + int i; + + for (i = 0; i < MAXBAT; i++) { char filename[50], status[50]; // get current capacity @@ -45,15 +50,9 @@ void battery_state(int *nbats, int bat[], State state[]) { state[i] = CHARGING; else state[i] = UNKNOWN; - - // next - i++; - } while (1); + } *nbats = i; -} #elif defined(__FreeBSD__) -#include -void battery_state(int *nbats, int bat[], State state[]) { size_t len = sizeof(int); int status, life = 0; @@ -67,8 +66,10 @@ void battery_state(int *nbats, int bat[], State state[]) { bat[0] = life; state[0] = status; } -} +#else +#error "Unsupported" #endif +} int main() { char *color; @@ -76,11 +77,14 @@ int main() { State state[2]; battery_state(&nbats, life, state); + for (int i = 0; i < nbats; i++) { + if (i > 0) + printf("%s", separator); switch (state[i]) { - case CHARGED: /* fully charged */ - printf("%s %d%% ", icon[life[i] / 10], life[i]); + case CHARGED: /* fully charged */ + printf("%s %d%%", icon[life[i] / 10], life[i]); break; case DISCHARGING: /* discharging */ if (life[i] >= 50) @@ -90,17 +94,17 @@ int main() { else color = "colour196"; /* red */ - printf("#[fg=%s]%s#[fg=default] %d%% ", color, icon[life[i] / 10], + printf("#[fg=%s]%s#[fg=default] %d%%", color, icon[life[i] / 10], life[i]); break; case CHARGING: /* charging */ - printf("#[fg=yellow]\uf583#[fg=default] %d%% ", life[i]); + printf("#[fg=yellow]\uf583#[fg=default] %d%%", life[i]); break; case DISCONNECTED: /* disconnected */ - printf("\uf492 "); + printf("\uf492"); break; default: /* unknown code */ - printf("\uf590 "); + printf("\uf590"); } } -- libgit2 0.21.2