i3status/src/print_load.c

63 lines
1.8 KiB
C
Raw Permalink Normal View History

// vim:ts=4:sw=4:expandtab
2019-01-23 07:56:40 +00:00
#include <config.h>
#include "i3status.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2012-03-25 18:55:55 +00:00
#include <yajl/yajl_gen.h>
2012-04-08 12:05:47 +00:00
#include <yajl/yajl_version.h>
2020-03-31 12:56:21 +00:00
#include "i3status.h"
#define STRING_SIZE 10
2021-11-02 20:25:15 +00:00
void print_load(load_ctx_t *ctx) {
char *outwalk = ctx->buf;
/* Get load */
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
double loadavg[3];
2021-11-02 20:25:15 +00:00
const char *selected_format = ctx->format;
bool colorful_output = false;
if (getloadavg(loadavg, 3) == -1)
goto error;
2021-11-02 20:25:15 +00:00
if (loadavg[0] >= ctx->max_threshold) {
START_COLOR("color_bad");
colorful_output = true;
2021-11-02 20:25:15 +00:00
if (ctx->format_above_threshold != NULL)
selected_format = ctx->format_above_threshold;
}
2020-03-31 12:56:21 +00:00
char string_loadavg_1[STRING_SIZE];
char string_loadavg_5[STRING_SIZE];
char string_loadavg_15[STRING_SIZE];
2020-03-31 12:56:21 +00:00
snprintf(string_loadavg_1, STRING_SIZE, "%1.2f", loadavg[0]);
snprintf(string_loadavg_5, STRING_SIZE, "%1.2f", loadavg[1]);
snprintf(string_loadavg_15, STRING_SIZE, "%1.2f", loadavg[2]);
2020-03-31 12:56:21 +00:00
placeholder_t placeholders[] = {
{.name = "%1min", .value = string_loadavg_1},
{.name = "%5min", .value = string_loadavg_5},
{.name = "%15min", .value = string_loadavg_15}};
2020-03-31 12:56:21 +00:00
const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
2021-11-02 20:25:15 +00:00
char *formatted = format_placeholders(selected_format, &placeholders[0], num);
OUTPUT_FORMATTED;
free(formatted);
if (colorful_output)
END_COLOR;
*outwalk = '\0';
2021-11-02 20:25:15 +00:00
OUTPUT_FULL_TEXT(ctx->buf);
return;
2011-08-25 21:24:06 +00:00
error:
#endif
OUTPUT_FULL_TEXT("cant read load");
(void)fputs("i3status: Cannot read system load using getloadavg()\n", stderr);
}