From f232212d1609d08e10cbacbf8ed94ec779daeca4 Mon Sep 17 00:00:00 2001 From: Ishan Kamat Date: Mon, 24 Apr 2023 03:50:33 -0500 Subject: [PATCH] introduce format_usage for cpu_usage should default to the status quo behavior if format_usage is not provided. should allow customization of cpu_usage output, following paradigm of format_percentage for battery info. --- i3status.c | 2 ++ include/i3status.h | 1 + man/i3status.man | 5 +++++ src/print_cpu_usage.c | 4 ++-- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/i3status.c b/i3status.c index 88fb90e0..fe98178a 100644 --- a/i3status.c +++ b/i3status.c @@ -393,6 +393,7 @@ int main(int argc, char *argv[]) { CFG_STR("format", "%usage", CFGF_NONE), CFG_STR("format_above_threshold", NULL, CFGF_NONE), CFG_STR("format_above_degraded_threshold", NULL, CFGF_NONE), + CFG_STR("format_usage", "%02d%s", CFGF_NONE), CFG_STR("path", "/proc/stat", CFGF_NONE), CFG_FLOAT("max_threshold", 95, CFGF_NONE), CFG_FLOAT("degraded_threshold", 90, CFGF_NONE), @@ -922,6 +923,7 @@ int main(int argc, char *argv[]) { .format = cfg_getstr(sec, "format"), .format_above_threshold = cfg_getstr(sec, "format_above_threshold"), .format_above_degraded_threshold = cfg_getstr(sec, "format_above_degraded_threshold"), + .format_usage = cfg_getstr(sec, "format_usage"), .path = cfg_getstr(sec, "path"), .max_threshold = cfg_getfloat(sec, "max_threshold"), .degraded_threshold = cfg_getfloat(sec, "degraded_threshold"), diff --git a/include/i3status.h b/include/i3status.h index fe44780b..6868104b 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -366,6 +366,7 @@ typedef struct { const char *format; const char *format_above_threshold; const char *format_above_degraded_threshold; + const char *format_usage; const char *path; const float max_threshold; const float degraded_threshold; diff --git a/man/i3status.man b/man/i3status.man index 725c27c6..bc0cd4eb 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -451,6 +451,9 @@ getting higher than the configured threshold. Defaults to 90. The output format when above degraded threshold can be customized with +format_above_degraded_threshold+. +CPU usage is by default displayed as a percentage zero-padded to 2 digits. If +you want the usage percentage to be shown in another format, use +format_usage+. + For displaying the Nth CPU usage, you can use the %cpu format string, starting from %cpu0. This feature is currently not supported in FreeBSD. @@ -466,6 +469,8 @@ starting from %cpu0. This feature is currently not supported in FreeBSD. *Example format_above_degraded_threshold*: +Warning above degraded threshold: %usage+ +*Example format_usage*: +"%02d%s"+ + === Memory Gets the memory usage from system on a Linux system from +/proc/meminfo+. Other diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c index a7ee2e46..818f5aa8 100644 --- a/src/print_cpu_usage.c +++ b/src/print_cpu_usage.c @@ -201,7 +201,7 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) { *(outwalk++) = *walk; } else if (BEGINS_WITH(walk + 1, "usage")) { - outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark); + outwalk += sprintf(outwalk, ctx->format_usage, diff_usage, pct_mark); walk += strlen("usage"); } #if defined(__linux__) @@ -217,7 +217,7 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) { int cpu_diff_idle = curr_cpus[number].idle - prev_cpus[number].idle; int cpu_diff_total = curr_cpus[number].total - prev_cpus[number].total; int cpu_diff_usage = (cpu_diff_total ? (1000 * (cpu_diff_total - cpu_diff_idle) / cpu_diff_total + 5) / 10 : 0); - outwalk += sprintf(outwalk, "%02d%s", cpu_diff_usage, pct_mark); + outwalk += sprintf(outwalk, ctx->format_usage, cpu_diff_usage, pct_mark); } walk += length; }