+
+ const char *
+ battery_remaining(const char *bat)
+ {
+ uintmax_t charge_now, current_now, m, h;
+ double timeleft;
+ char path[PATH_MAX], state[12];
+
+ if (esnprintf(path, sizeof(path),
+ "/sys/class/power_supply/%s/status", bat) < 0) {
+ return NULL;
+ }
+ if (pscanf(path, "%12s", state) != 1) {
+ return NULL;
+ }
+
+ if (!pick(bat, "/sys/class/power_supply/%s/charge_now",
+ "/sys/class/power_supply/%s/energy_now", path,
+ sizeof(path)) ||
+ pscanf(path, "%ju", &charge_now) < 0) {
+ return NULL;
+ }
+
+ if (!strcmp(state, "Discharging")) {
+ if (!pick(bat, "/sys/class/power_supply/%s/current_now",
+ "/sys/class/power_supply/%s/power_now", path,
+ sizeof(path)) ||
+ pscanf(path, "%ju", ¤t_now) < 0) {
+ return NULL;
+ }
+
+ if (current_now == 0) {
+ return NULL;
+ }
+
+ timeleft = (double)charge_now / (double)current_now;
+ h = timeleft;
+ m = (timeleft - (double)h) * 60;
+
+ return bprintf("%juh %jum", h, m);
+ }
+
+ return "";
+ }