Battery All-In-One Function

- Condense separate battery functions into one that handles icons & dividers
- Only shows battery information if it exists
This commit is contained in:
Sravan Balaji
2020-08-06 23:44:03 -04:00
parent 6175a0caa3
commit 1d22eea138
3 changed files with 68 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../util.h"
@@ -115,6 +116,70 @@
return "";
}
const char *
battery_all_in_one(const char *bat)
{
char path[PATH_MAX];
// Handle case where battery isn't present
if (esnprintf(path, sizeof(path),
"/sys/class/power_supply/%s/capacity", bat) < 0) {
return "";
}
int percentage = atoi(battery_perc(bat));
char *bat_icon = "";
char *bat_status = "";
char *bat_rem = "";
char *div = "";
// Add battery percentage icon
if (percentage < 20) {
bat_icon = "";
}
else if (percentage < 30) {
bat_icon = "";
}
else if (percentage < 40) {
bat_icon = "";
}
else if (percentage < 50) {
bat_icon = "";
}
else if (percentage < 60) {
bat_icon = "";
}
else if (percentage < 70) {
bat_icon = "";
}
else if (percentage < 80) {
bat_icon = "";
}
else if (percentage < 90) {
bat_icon = "";
}
else if (percentage < 100) {
bat_icon = "";
}
else if (percentage == 100) {
bat_icon = "";
}
if (!strcmp(battery_state(bat), "+")) {
bat_status = "";
bat_rem = "";
}
else {
bat_status = "";
const char *temp = battery_remaining(bat);
bat_rem = (char*) malloc(sizeof(temp) + 2);
sprintf(bat_rem, " (%s)", temp);
}
return bprintf("%s %s%s %i%%%s %s ", div, bat_icon, bat_status, percentage, bat_rem, div);
}
#elif defined(__OpenBSD__)
#include <fcntl.h>
#include <machine/apmvar.h>

View File

@@ -21,6 +21,7 @@ static const char divider_symbol[] = "";
* NULL on OpenBSD/FreeBSD
* battery_remaining battery remaining HH:MM battery name (BAT0)
* NULL on OpenBSD/FreeBSD
* battery_all_in_one all-in-one battery status battery name (BAT0)
* check_updates num available updates NULL
* cpu_perc cpu usage in percent NULL
* cpu_freq cpu frequency in MHz NULL
@@ -76,11 +77,7 @@ static const struct arg args[] = {
{ ram_perc, " %s%%", NULL },
{ divider, " %s ", divider_symbol },
{ disk_perc, " %s%%", "/home" },
{ divider, " %s ", divider_symbol },
{ battery_state, " %s", battery_name }, // TODO: Make the battery icon change with the level and charging status
{ battery_perc, "%s%%", battery_name },
{ battery_remaining, " (%s)", battery_name },
{ divider, " %s ", divider_symbol },
{ battery_all_in_one, "%s", battery_name, },
{ datetime, " %s", "%a %x" },
{ divider, " %s ", divider_symbol },
{ datetime, " %s", "%I:%M:%S %p" },

View File

@@ -4,6 +4,7 @@
const char *battery_perc(const char *);
const char *battery_state(const char *);
const char *battery_remaining(const char *);
const char *battery_all_in_one(const char *bat);
/* check updates */
const char *check_updates(void);