Merge pull request #402 from Stunkymonkey/format_placeholder-wireless_info

use format_placeholder for wireless_info
This commit is contained in:
Ingo Bürk 2020-04-08 08:41:14 +02:00 committed by GitHub
commit 582432fcc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
@ -517,7 +519,12 @@ void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface,
if (ipv6_address == NULL) {
START_COLOR("color_bad");
outwalk += sprintf(outwalk, "%s", format_down);
goto out;
END_COLOR;
free(ipv4_address);
free(ipv6_address);
OUTPUT_FULL_TEXT(buffer);
return;
} else {
prefer_ipv4 = false;
}
@ -542,81 +549,76 @@ 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];
} 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");
} 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);
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];
print_bitrate(br_buffer, sizeof(br_buffer), info.bitrate, format_bitrate);
outwalk += sprintf(outwalk, "%s", br_buffer);
walk += strlen("bitrate");
}
#endif
else {
*(outwalk++) = '%';
}
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, "?");
}
out:
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, "?");
}
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, &tmp);
else
#endif
snprintf(string_essid, STRING_SIZE, "?");
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, "?");
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
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);
END_COLOR;
free(ipv4_address);
free(ipv6_address);
OUTPUT_FULL_TEXT(buffer);
free(buffer);
}