summaryrefslogtreecommitdiff
path: root/src/print_cpu_usage.c
diff options
context:
space:
mode:
authorMark Schreiber <mark7@alumni.cmu.edu>2016-04-13 09:07:12 -0700
committerMark Schreiber <mark7@alumni.cmu.edu>2016-04-13 09:11:32 -0700
commit1e8dab273d8542366c098dfcda37756db6b2bcd7 (patch)
tree4c9fbbee9124a01a080c6b6ff635d8a5d5304093 /src/print_cpu_usage.c
parentc094f47b457104c267dd55782efc34c47f05cf9d (diff)
Add CPU usage color thresholds
CPU usage had previously not supported the color option. Add support for a "degraded" state above which the degraded color is used, and a higher "bad" state above which the "bad" color is used. One possible use for these might be indicating whether one or all cores are saturated. Unlike the color settings for other, these are set high enough to be disabled by default. This is done because i3status determines CPU usage over only the last display interval, which means that, a user with a low refresh rate might see frequent, potentially-annoying color changes.
Diffstat (limited to 'src/print_cpu_usage.c')
-rw-r--r--src/print_cpu_usage.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c
index adf2d04..1753cf5 100644
--- a/src/print_cpu_usage.c
+++ b/src/print_cpu_usage.c
@@ -41,11 +41,12 @@ static int prev_idle = 0;
* percentage.
*
*/
-void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
+void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const float max_threshold, const float degraded_threshold) {
const char *walk;
char *outwalk = buffer;
int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
int diff_idle, diff_total, diff_usage;
+ bool colorful_output = false;
#if defined(LINUX)
static char statpath[512];
@@ -101,10 +102,21 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
continue;
}
+ if (diff_usage >= max_threshold) {
+ START_COLOR("color_bad");
+ colorful_output = true;
+ } else if (diff_usage >= degraded_threshold) {
+ START_COLOR("color_degraded");
+ colorful_output = true;
+ }
+
if (BEGINS_WITH(walk + 1, "usage")) {
outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark);
walk += strlen("usage");
}
+
+ if (colorful_output)
+ END_COLOR;
}
OUTPUT_FULL_TEXT(buffer);