diff options
author | Felix Buehler <account@buehler.rocks> | 2020-03-31 16:53:02 +0200 |
---|---|---|
committer | Felix Buehler <account@buehler.rocks> | 2020-03-31 17:05:48 +0200 |
commit | ae2566f28e4a54c47d68c87c87bf335521c5b13b (patch) | |
tree | 149df0d67cf7be61766d57cff3141ed30072b61b | |
parent | 101215bbc83b97de30b6b535bbe2e93d2e913804 (diff) |
use format_placeholder for wireless_info
-rw-r--r-- | src/print_wireless_info.c | 126 |
1 files changed, 62 insertions, 64 deletions
diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index 49421bb..37a963f 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -68,6 +68,8 @@ #include "i3status.h" +#define STRING_SIZE 30 + #define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0) #define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1) #define WIRELESS_INFO_FLAG_HAS_SIGNAL (1 << 2) @@ -542,81 +544,77 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, } } - for (; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; + char string_quality[STRING_SIZE]; + char string_signal[STRING_SIZE]; + char string_noise[STRING_SIZE]; + char string_essid[STRING_SIZE]; + char string_frequency[STRING_SIZE]; + char string_ip[STRING_SIZE]; + char string_bitrate[STRING_SIZE]; + + if (info.flags & WIRELESS_INFO_FLAG_HAS_QUALITY) { + if (info.quality_max) + snprintf(string_quality, STRING_SIZE, format_quality, PERCENT_VALUE(info.quality, info.quality_max), pct_mark); + else + snprintf(string_quality, STRING_SIZE, "%d", info.quality); + } else { + snprintf(string_quality, STRING_SIZE, "?"); + } - } 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); - else - outwalk += sprintf(outwalk, "%d", info.quality); - } else { - *(outwalk++) = '?'; - } - walk += strlen("quality"); - - } else if (BEGINS_WITH(walk + 1, "signal")) { - if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { - if (info.signal_level_max) - outwalk += sprintf(outwalk, format_signal, PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); - else - outwalk += sprintf(outwalk, "%d dBm", info.signal_level); - } else { - *(outwalk++) = '?'; - } - walk += strlen("signal"); - - } else if (BEGINS_WITH(walk + 1, "noise")) { - if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { - if (info.noise_level_max) - outwalk += sprintf(outwalk, format_noise, PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); - else - outwalk += sprintf(outwalk, "%d dBm", info.noise_level); - } else { - *(outwalk++) = '?'; - } - walk += strlen("noise"); + if (info.flags & WIRELESS_INFO_FLAG_HAS_SIGNAL) { + if (info.signal_level_max) + snprintf(string_signal, STRING_SIZE, format_signal, PERCENT_VALUE(info.signal_level, info.signal_level_max), pct_mark); + else + snprintf(string_signal, STRING_SIZE, "%d dBm", info.signal_level); + } else { + snprintf(string_signal, STRING_SIZE, "?"); + } - } else if (BEGINS_WITH(walk + 1, "essid")) { + if (info.flags & WIRELESS_INFO_FLAG_HAS_NOISE) { + if (info.noise_level_max) + snprintf(string_noise, STRING_SIZE, format_noise, PERCENT_VALUE(info.noise_level, info.noise_level_max), pct_mark); + else + snprintf(string_noise, STRING_SIZE, "%d dBm", info.noise_level); + } else { + snprintf(string_noise, STRING_SIZE, "?"); + } + + char *tmp = string_essid; #ifdef IW_ESSID_MAX_SIZE - if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) - maybe_escape_markup(info.essid, &outwalk); - else + if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) + maybe_escape_markup(info.essid, &tmp); + else #endif - *(outwalk++) = '?'; - walk += strlen("essid"); - - } 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"); - - } else if (BEGINS_WITH(walk + 1, "ip")) { - outwalk += sprintf(outwalk, "%s", ip_address); - walk += strlen("ip"); - } -#ifdef __linux__ - else if (BEGINS_WITH(walk + 1, "bitrate")) { - char br_buffer[128]; + snprintf(string_essid, STRING_SIZE, "?"); - print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate, format_bitrate); + if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY) + snprintf(string_frequency, STRING_SIZE, "%1.1f GHz", info.frequency / 1e9); + else + snprintf(string_frequency, STRING_SIZE, "?"); - outwalk += sprintf(outwalk, "%s", br_buffer); - walk += strlen("bitrate"); - } + snprintf(string_ip, STRING_SIZE, "%s", ip_address); + +#ifdef __linux__ + char br_buffer[128]; + print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate, format_bitrate); #endif - else { - *(outwalk++) = '%'; - } - } + + placeholder_t placeholders[] = { + {.name = "%quality", .value = string_quality}, + {.name = "%signal", .value = string_signal}, + {.name = "%noise", .value = string_noise}, + {.name = "%essid", .value = string_essid}, + {.name = "%frequency", .value = string_frequency}, + {.name = "%ip", .value = string_ip}, + {.name = "%bitrate", .value = string_bitrate}}; + + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(walk, &placeholders[0], num); out: END_COLOR; free(ipv4_address); free(ipv6_address); OUTPUT_FULL_TEXT(buffer); + free(buffer); } |