From 52e9f6f63b74db2a6a1d67524851649b18794950 Mon Sep 17 00:00:00 2001 From: Felix Buehler Date: Sat, 2 Jun 2018 02:32:25 +0200 Subject: able to print percentage its now possible to have percentage before and after a variable. except for the date. But percentage with dates does not make much sense to me, so i skipped it. --- src/print_battery_info.c | 11 ++++++++--- src/print_cpu_temperature.c | 8 +++++--- src/print_cpu_usage.c | 9 +++++---- src/print_disk_info.c | 28 +++++++++++----------------- src/print_eth_info.c | 8 +++++--- src/print_ipv6_addr.c | 7 ++++--- src/print_load.c | 14 +++++++------- src/print_mem.c | 32 +++++++++++++------------------- src/print_path_exists.c | 8 +++++--- src/print_run_watch.c | 8 +++++--- src/print_time.c | 7 ++++--- src/print_volume.c | 12 +++++++----- src/print_wireless_info.c | 25 ++++++++++--------------- 13 files changed, 89 insertions(+), 88 deletions(-) diff --git a/src/print_battery_info.c b/src/print_battery_info.c index db4d7bd..8c85192 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -622,10 +622,8 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "status")) { + } else if (BEGINS_WITH(walk + 1, "status")) { const char *statusstr; switch (batt_info.status) { case CS_CHARGING: @@ -643,6 +641,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char outwalk += sprintf(outwalk, "%s", statusstr); 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); @@ -650,6 +649,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char outwalk += sprintf(outwalk, "%.02f%s", batt_info.percentage_remaining, pct_mark); } walk += strlen("percentage"); + } else if (BEGINS_WITH(walk + 1, "remaining")) { if (batt_info.seconds_remaining >= 0) { int seconds, hours, minutes; @@ -668,6 +668,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } walk += strlen("remaining"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); + } else if (BEGINS_WITH(walk + 1, "emptytime")) { if (batt_info.seconds_remaining >= 0) { time_t empty_time = time(NULL) + batt_info.seconds_remaining; @@ -683,12 +684,16 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char } walk += strlen("emptytime"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); + } else if (BEGINS_WITH(walk + 1, "consumption")) { if (batt_info.present_rate >= 0) outwalk += sprintf(outwalk, "%1.2fW", batt_info.present_rate / 1e6); walk += strlen("consumption"); EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT(); + + } else { + *(outwalk++) = '%'; } } diff --git a/src/print_cpu_temperature.c b/src/print_cpu_temperature.c index feae3ec..569ea60 100644 --- a/src/print_cpu_temperature.c +++ b/src/print_cpu_temperature.c @@ -250,11 +250,13 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const for (walk = selected_format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "degrees")) { + + } else if (BEGINS_WITH(walk + 1, "degrees")) { outwalk += sprintf(outwalk, "%s", temperature.formatted_value); walk += strlen("degrees"); + + } else { + *(outwalk++) = '%'; } } diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c index c1ea3fd..615fe5d 100644 --- a/src/print_cpu_usage.c +++ b/src/print_cpu_usage.c @@ -144,15 +144,13 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const for (walk = selected_format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "usage")) { + } else if (BEGINS_WITH(walk + 1, "usage")) { outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark); walk += strlen("usage"); } #if defined(LINUX) - if (BEGINS_WITH(walk + 1, "cpu")) { + else if (BEGINS_WITH(walk + 1, "cpu")) { int number = 0; sscanf(walk + 1, "cpu%d", &number); if (number < 0 || number >= cpu_count) { @@ -172,6 +170,9 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const walk += strlen("cpu") + padding; } #endif + else { + *(outwalk++) = '%'; + } } for (int i = 0; i < cpu_count; i++) diff --git a/src/print_disk_info.c b/src/print_disk_info.c index 770e718..bc43da0 100644 --- a/src/print_disk_info.c +++ b/src/print_disk_info.c @@ -172,47 +172,41 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch for (walk = selected_format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "free")) { + } else if (BEGINS_WITH(walk + 1, "free")) { outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type); walk += strlen("free"); - } - if (BEGINS_WITH(walk + 1, "used")) { + } else if (BEGINS_WITH(walk + 1, "used")) { outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type); walk += strlen("used"); - } - if (BEGINS_WITH(walk + 1, "total")) { + } else if (BEGINS_WITH(walk + 1, "total")) { outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type); walk += strlen("total"); - } - if (BEGINS_WITH(walk + 1, "avail")) { + } else if (BEGINS_WITH(walk + 1, "avail")) { outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type); walk += strlen("avail"); - } - if (BEGINS_WITH(walk + 1, "percentage_free")) { + } else if (BEGINS_WITH(walk + 1, "percentage_free")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark); walk += strlen("percentage_free"); - } - if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) { + } else if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark); walk += strlen("percentage_used_of_avail"); - } - if (BEGINS_WITH(walk + 1, "percentage_used")) { + } else if (BEGINS_WITH(walk + 1, "percentage_used")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark); walk += strlen("percentage_used"); - } - if (BEGINS_WITH(walk + 1, "percentage_avail")) { + } else if (BEGINS_WITH(walk + 1, "percentage_avail")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks, pct_mark); walk += strlen("percentage_avail"); + + } else { + *(outwalk++) = '%'; } } diff --git a/src/print_eth_info.c b/src/print_eth_info.c index 996ce3b..2fc25a1 100644 --- a/src/print_eth_info.c +++ b/src/print_eth_info.c @@ -175,15 +175,17 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons for (walk = format_up; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "ip")) { + } else if (BEGINS_WITH(walk + 1, "ip")) { outwalk += sprintf(outwalk, "%s", ip_address); walk += strlen("ip"); + } else if (BEGINS_WITH(walk + 1, "speed")) { outwalk += print_eth_speed(outwalk, interface); walk += strlen("speed"); + + } else { + *(outwalk++) = '%'; } } diff --git a/src/print_ipv6_addr.c b/src/print_ipv6_addr.c index a24119f..a50bf39 100644 --- a/src/print_ipv6_addr.c +++ b/src/print_ipv6_addr.c @@ -133,12 +133,13 @@ void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, con for (walk = format_up; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "ip")) { + } else if (BEGINS_WITH(walk + 1, "ip")) { outwalk += sprintf(outwalk, "%s", addr_string); walk += strlen("ip"); + + } else { + *(outwalk++) = '%'; } } END_COLOR; diff --git a/src/print_load.c b/src/print_load.c index 6da2519..e0ba677 100644 --- a/src/print_load.c +++ b/src/print_load.c @@ -29,21 +29,21 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char for (walk = selected_format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "1min")) { + + } else if (BEGINS_WITH(walk + 1, "1min")) { outwalk += sprintf(outwalk, "%1.2f", loadavg[0]); walk += strlen("1min"); - } - if (BEGINS_WITH(walk + 1, "5min")) { + } else if (BEGINS_WITH(walk + 1, "5min")) { outwalk += sprintf(outwalk, "%1.2f", loadavg[1]); walk += strlen("5min"); - } - if (BEGINS_WITH(walk + 1, "15min")) { + } else if (BEGINS_WITH(walk + 1, "15min")) { outwalk += sprintf(outwalk, "%1.2f", loadavg[2]); walk += strlen("15min"); + + } else { + *(outwalk++) = '%'; } } diff --git a/src/print_mem.c b/src/print_mem.c index a37fa29..f9284f3 100644 --- a/src/print_mem.c +++ b/src/print_mem.c @@ -159,51 +159,45 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha for (walk = selected_format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "total")) { + + } else if (BEGINS_WITH(walk + 1, "total")) { outwalk += print_bytes_human(outwalk, ram_total); walk += strlen("total"); - } - if (BEGINS_WITH(walk + 1, "used")) { + } else if (BEGINS_WITH(walk + 1, "used")) { outwalk += print_bytes_human(outwalk, ram_used); walk += strlen("used"); - } - if (BEGINS_WITH(walk + 1, "free")) { + } else if (BEGINS_WITH(walk + 1, "free")) { outwalk += print_bytes_human(outwalk, ram_free); walk += strlen("free"); - } - if (BEGINS_WITH(walk + 1, "available")) { + } else if (BEGINS_WITH(walk + 1, "available")) { outwalk += print_bytes_human(outwalk, ram_available); walk += strlen("available"); - } - if (BEGINS_WITH(walk + 1, "shared")) { + } else if (BEGINS_WITH(walk + 1, "shared")) { outwalk += print_bytes_human(outwalk, ram_shared); walk += strlen("shared"); - } - if (BEGINS_WITH(walk + 1, "percentage_free")) { + } else if (BEGINS_WITH(walk + 1, "percentage_free")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_free / ram_total, pct_mark); walk += strlen("percentage_free"); - } - if (BEGINS_WITH(walk + 1, "percentage_available")) { + } else if (BEGINS_WITH(walk + 1, "percentage_available")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_available / ram_total, pct_mark); walk += strlen("percentage_available"); - } - if (BEGINS_WITH(walk + 1, "percentage_used")) { + } else if (BEGINS_WITH(walk + 1, "percentage_used")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_used / ram_total, pct_mark); walk += strlen("percentage_used"); - } - if (BEGINS_WITH(walk + 1, "percentage_shared")) { + } else if (BEGINS_WITH(walk + 1, "percentage_shared")) { outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_shared / ram_total, pct_mark); walk += strlen("percentage_shared"); + + } else { + *(outwalk++) = '%'; } } diff --git a/src/print_path_exists.c b/src/print_path_exists.c index 65bc9c8..7bdbe30 100644 --- a/src/print_path_exists.c +++ b/src/print_path_exists.c @@ -25,15 +25,17 @@ void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const for (; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "title")) { + } else if (BEGINS_WITH(walk + 1, "title")) { outwalk += sprintf(outwalk, "%s", title); walk += strlen("title"); + } else if (BEGINS_WITH(walk + 1, "status")) { outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no")); walk += strlen("status"); + + } else { + *(outwalk++) = '%'; } } diff --git a/src/print_run_watch.c b/src/print_run_watch.c index b108f8f..0887521 100644 --- a/src/print_run_watch.c +++ b/src/print_run_watch.c @@ -23,15 +23,17 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c for (; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "title")) { + } else if (BEGINS_WITH(walk + 1, "title")) { outwalk += sprintf(outwalk, "%s", title); walk += strlen("title"); + } else if (BEGINS_WITH(walk + 1, "status")) { outwalk += sprintf(outwalk, "%s", (running ? "yes" : "no")); walk += strlen("status"); + + } else { + *(outwalk++) = '%'; } } diff --git a/src/print_time.c b/src/print_time.c index 3ed32b0..3a6c4cc 100644 --- a/src/print_time.c +++ b/src/print_time.c @@ -57,13 +57,14 @@ void print_time(yajl_gen json_gen, char *buffer, const char *title, const char * for (walk = format; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "time")) { + } else if (BEGINS_WITH(walk + 1, "time")) { strftime(timebuf, sizeof(timebuf), format_time, &tm); maybe_escape_markup(timebuf, &outwalk); walk += strlen("time"); + + } else { + *(outwalk++) = '%'; } } } diff --git a/src/print_volume.c b/src/print_volume.c index e28a132..c5cf55b 100644 --- a/src/print_volume.c +++ b/src/print_volume.c @@ -35,15 +35,17 @@ static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume) { for (; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "%")) { + + } else if (BEGINS_WITH(walk + 1, "%")) { outwalk += sprintf(outwalk, "%s", pct_mark); walk += strlen("%"); - } - if (BEGINS_WITH(walk + 1, "volume")) { + + } else if (BEGINS_WITH(walk + 1, "volume")) { outwalk += sprintf(outwalk, "%d%s", ivolume, pct_mark); walk += strlen("volume"); + + } else { + *(outwalk++) = '%'; } } return outwalk; diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index dcfde52..d495b47 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -534,10 +534,8 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, for (; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "quality")) { + } else if (BEGINS_WITH(walk + 1, "quality")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) { if (info.quality_max) outwalk += sprintf(outwalk, format_quality, PERCENT_VALUE(info.quality, info.quality_max), pct_mark); @@ -547,9 +545,8 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, *(outwalk++) = '?'; } walk += strlen("quality"); - } - if (BEGINS_WITH(walk + 1, "signal")) { + } else if (BEGINS_WITH(walk + 1, "signal")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { if (info.signal_level_max) outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); @@ -559,9 +556,8 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, *(outwalk++) = '?'; } walk += strlen("signal"); - } - if (BEGINS_WITH(walk + 1, "noise")) { + } else if (BEGINS_WITH(walk + 1, "noise")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { if (info.noise_level_max) outwalk += sprintf(outwalk, "%3d%s", PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); @@ -571,9 +567,8 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, *(outwalk++) = '?'; } walk += strlen("noise"); - } - if (BEGINS_WITH(walk + 1, "essid")) { + } else if (BEGINS_WITH(walk + 1, "essid")) { #ifdef IW_ESSID_MAX_SIZE if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) maybe_escape_markup(info.essid, &outwalk); @@ -581,23 +576,20 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, #endif *(outwalk++) = '?'; walk += strlen("essid"); - } - if (BEGINS_WITH(walk + 1, "frequency")) { + } else if (BEGINS_WITH(walk + 1, "frequency")) { if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY) outwalk += sprintf(outwalk, "%1.1f GHz", info.frequency / 1e9); else *(outwalk++) = '?'; walk += strlen("frequency"); - } - if (BEGINS_WITH(walk + 1, "ip")) { + } else if (BEGINS_WITH(walk + 1, "ip")) { outwalk += sprintf(outwalk, "%s", ip_address); walk += strlen("ip"); } - #ifdef LINUX - if (BEGINS_WITH(walk + 1, "bitrate")) { + else if (BEGINS_WITH(walk + 1, "bitrate")) { char br_buffer[128]; print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate); @@ -606,6 +598,9 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, walk += strlen("bitrate"); } #endif + else { + *(outwalk++) = '%'; + } } out: -- cgit v1.2.3