time_t empty_time;
struct tm *empty_tm;
char buf[1024];
- char remainingbuf[256];
char emptytimebuf[256];
char consumptionbuf[256];
const char *walk, *last;
int full_design = -1,
remaining = -1,
present_rate = -1,
- voltage = -1;
+ voltage = -1,
+ seconds_remaining = -1;
float percentage_remaining = -1;
charging_status_t status = CS_DISCHARGING;
- memset(remainingbuf, '\0', sizeof(remainingbuf));
memset(emptytimebuf, '\0', sizeof(emptytimebuf));
memset(consumptionbuf, '\0', sizeof(consumptionbuf));
if (present_rate > 0 && status != CS_FULL) {
float remaining_time;
- int seconds, hours, minutes, seconds_remaining;
if (status == CS_CHARGING)
remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
else if (status == CS_DISCHARGING)
seconds_remaining = (int)(remaining_time * 3600.0);
- hours = seconds_remaining / 3600;
- seconds = seconds_remaining - (hours * 3600);
- minutes = seconds / 60;
- seconds -= (minutes * 60);
-
if (status == CS_DISCHARGING && low_threshold > 0) {
if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
START_COLOR("color_bad");
}
}
- if (hide_seconds)
- (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d",
- max(hours, 0), max(minutes, 0));
- else
- (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
- max(hours, 0), max(minutes, 0), max(seconds, 0));
-
empty_time = time(NULL);
empty_time += seconds_remaining;
empty_tm = localtime(&empty_time);
return;
}
- remaining = sysctl_rslt;
+ hide_seconds = true;
+ seconds_remaining = sysctl_rslt * 60;
if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
OUTPUT_FULL_TEXT(format_down);
return;
full_design = sysctl_rslt;
if (state == ACPI_BATT_STAT_DISCHARG) {
- int hours, minutes;
- minutes = remaining;
- hours = minutes / 60;
- minutes -= (hours * 60);
- (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d",
- max(hours, 0), max(minutes, 0));
if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
START_COLOR("color_bad");
colorful_output = true;
- } else if (strcasecmp(threshold_type, "time") == 0 && remaining < (u_int)low_threshold) {
+ } else if (strcasecmp(threshold_type, "time") == 0 && seconds_remaining < 60 * low_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
/* Can't give a meaningful value for remaining minutes if we're charging. */
if (status != CS_CHARGING) {
- (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d", apm_info.minutes_left / 60, apm_info.minutes_left % 60);
- } else {
- (void)snprintf(remainingbuf, sizeof(remainingbuf), "%s", "(CHR)");
+ seconds_remaining = apm_info.minutes_left * 60;
}
if (colorful_output)
* remaining' figure, so we must deduce it.
*/
float remaining_time;
- int seconds, hours, minutes, seconds_remaining;
if (status == CS_CHARGING)
remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
seconds_remaining = (int)(remaining_time * 3600.0);
- hours = seconds_remaining / 3600;
- seconds = seconds_remaining - (hours * 3600);
- minutes = seconds / 60;
- seconds -= (minutes * 60);
-
if (status != CS_CHARGING) {
- if (hide_seconds)
- (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d",
- max(hours, 0), max(minutes, 0));
- else
- (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
- max(hours, 0), max(minutes, 0), max(seconds, 0));
-
if (low_threshold > 0) {
if (strcasecmp(threshold_type, "time") == 0 && ((float)seconds_remaining / 60.0) < (u_int)low_threshold) {
START_COLOR("color_bad");
colorful_output = true;
}
}
- } else {
- if (hide_seconds)
- (void)snprintf(remainingbuf, sizeof(remainingbuf), "(%02d:%02d until full)",
- max(hours, 0), max(minutes, 0));
- else
- (void)snprintf(remainingbuf, sizeof(remainingbuf), "(%02d:%02d:%02d until full)",
- max(hours, 0), max(minutes, 0), max(seconds, 0));
}
empty_time = time(NULL);
((float)present_rate / 1000.0 / 1000.0));
#endif
-#define EAT_SPACE_FROM_OUTPUT_IF_EMPTY(_buf) \
+#define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \
do { \
- if (strlen(_buf) == 0) { \
+ if (outwalk == prevoutwalk) { \
if (outwalk > buffer && isspace(outwalk[-1])) \
outwalk--; \
else if (isspace(*(walk + 1))) \
} while (0)
for (walk = format; *walk != '\0'; walk++) {
+ char *prevoutwalk = outwalk;
+
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
walk += strlen("percentage");
} else if (BEGINS_WITH(walk + 1, "remaining")) {
- outwalk += sprintf(outwalk, "%s", remainingbuf);
+ if (seconds_remaining >= 0) {
+ int seconds, hours, minutes;
+
+ hours = seconds_remaining / 3600;
+ seconds = seconds_remaining - (hours * 3600);
+ minutes = seconds / 60;
+ seconds -= (minutes * 60);
+
+ if (hide_seconds)
+ outwalk += sprintf(outwalk, "%02d:%02d",
+ max(hours, 0), max(minutes, 0));
+ else
+ outwalk += sprintf(outwalk, "%02d:%02d:%02d",
+ max(hours, 0), max(minutes, 0), max(seconds, 0));
+ }
walk += strlen("remaining");
- EAT_SPACE_FROM_OUTPUT_IF_EMPTY(remainingbuf);
+ EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
} else if (BEGINS_WITH(walk + 1, "emptytime")) {
outwalk += sprintf(outwalk, "%s", emptytimebuf);
walk += strlen("emptytime");
- EAT_SPACE_FROM_OUTPUT_IF_EMPTY(emptytimebuf);
+ EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
} else if (BEGINS_WITH(walk + 1, "consumption")) {
outwalk += sprintf(outwalk, "%s", consumptionbuf);
walk += strlen("consumption");
- EAT_SPACE_FROM_OUTPUT_IF_EMPTY(consumptionbuf);
+ EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
}
}