]> git.sur5r.net Git - i3/i3status/blobdiff - src/print_cpu_usage.c
Added unit test case for path_exists
[i3/i3status] / src / print_cpu_usage.c
index 68437b3cc9e1836ef755c1e7b251a4c8734de43e..1753cf53044ea8cd307f00124507211ec695023b 100644 (file)
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/sysctl.h>
+#if defined(__OpenBSD__)
+#include <sys/sched.h>
+#else
 #include <sys/dkstat.h>
 #endif
+#endif
 
 #if defined(__DragonFly__)
 #include <sys/param.h>
@@ -37,15 +41,16 @@ 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;
-    char buf[1024];
     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];
+    char buf[1024];
     strcpy(statpath, "/proc/stat");
     if (!slurp(statpath, buf, sizeof(buf)) ||
         sscanf(buf, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system, &curr_idle) != 4)
@@ -97,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%%", diff_usage);
+            outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark);
             walk += strlen("usage");
         }
+
+        if (colorful_output)
+            END_COLOR;
     }
 
     OUTPUT_FULL_TEXT(buffer);