From abfe05c8bd54cf25e3d705e4899e9e9c8a8ea9a7 Mon Sep 17 00:00:00 2001 From: grmat Date: Sat, 16 Nov 2019 16:26:26 +0100 Subject: Introduce format_percentage for battery (#371) Allow custom output, backwards-compatible to integer_battery_capacity --- src/print_battery_info.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/print_battery_info.c b/src/print_battery_info.c index c8a1d3d..1240626 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -570,7 +570,7 @@ static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_ge return true; } -void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) { +void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, const char *format_percentage, bool hide_seconds) { const char *walk; char *outwalk = buffer; struct battery_info batt_info = { @@ -586,7 +586,9 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__) /* These OSes report battery stats in whole percent. */ - integer_battery_capacity = true; + if (strcmp("%.02f%s", format_percentage) == 0) { + format_percentage = "%.00f%s"; + } #endif #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__) /* These OSes report battery time in minutes. */ @@ -687,11 +689,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char walk += strlen("status"); } else if (BEGINS_WITH(walk + 1, "percentage")) { - if (integer_battery_capacity) { - outwalk += sprintf(outwalk, "%.00f%s", batt_info.percentage_remaining, pct_mark); - } else { - outwalk += sprintf(outwalk, "%.02f%s", batt_info.percentage_remaining, pct_mark); - } + outwalk += sprintf(outwalk, format_percentage, batt_info.percentage_remaining, pct_mark); walk += strlen("percentage"); } else if (BEGINS_WITH(walk + 1, "remaining")) { -- cgit v1.2.3