disk: fix colors (+param struct)
This commit is contained in:
parent
ffedf14066
commit
9a6f96b309
14
i3status.c
14
i3status.c
@ -784,7 +784,19 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
CASE_SEC_TITLE("disk") {
|
||||
SEC_OPEN_MAP("disk_info");
|
||||
print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_below_threshold"), cfg_getstr(sec, "format_not_mounted"), cfg_getstr(sec, "prefix_type"), cfg_getstr(sec, "threshold_type"), cfg_getfloat(sec, "low_threshold"));
|
||||
disk_info_ctx_t ctx = {
|
||||
.json_gen = json_gen,
|
||||
.buf = buffer,
|
||||
.buflen = sizeof(buffer),
|
||||
.path = title,
|
||||
.format = cfg_getstr(sec, "format"),
|
||||
.format_below_threshold = cfg_getstr(sec, "format_below_threshold"),
|
||||
.format_not_mounted = cfg_getstr(sec, "format_not_mounted"),
|
||||
.prefix_type = cfg_getstr(sec, "prefix_type"),
|
||||
.threshold_type = cfg_getstr(sec, "threshold_type"),
|
||||
.low_threshold = cfg_getfloat(sec, "low_threshold"),
|
||||
};
|
||||
print_disk_info(&ctx);
|
||||
SEC_CLOSE_MAP;
|
||||
}
|
||||
|
||||
|
@ -244,7 +244,20 @@ typedef struct {
|
||||
|
||||
void print_ipv6_info(ipv6_info_ctx_t *ctx);
|
||||
|
||||
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold);
|
||||
typedef struct {
|
||||
yajl_gen json_gen;
|
||||
char *buf;
|
||||
const size_t buflen;
|
||||
const char *path;
|
||||
const char *format;
|
||||
const char *format_below_threshold;
|
||||
const char *format_not_mounted;
|
||||
const char *prefix_type;
|
||||
const char *threshold_type;
|
||||
const double low_threshold;
|
||||
} disk_info_ctx_t;
|
||||
|
||||
void print_disk_info(disk_info_ctx_t *ctx);
|
||||
|
||||
typedef struct {
|
||||
yajl_gen json_gen;
|
||||
|
@ -116,13 +116,14 @@ static bool below_threshold(struct statvfs buf, const char *prefix_type, const c
|
||||
* human readable manner.
|
||||
*
|
||||
*/
|
||||
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) {
|
||||
const char *selected_format = format;
|
||||
char *outwalk = buffer;
|
||||
void print_disk_info(disk_info_ctx_t *ctx) {
|
||||
const char *selected_format = ctx->format;
|
||||
char *outwalk = ctx->buf;
|
||||
bool colorful_output = false;
|
||||
bool mounted = false;
|
||||
#define json_gen ctx->json_gen
|
||||
|
||||
INSTANCE(path);
|
||||
INSTANCE(ctx->path);
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
|
||||
struct statfs buf;
|
||||
@ -141,12 +142,12 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
|
||||
#else
|
||||
struct statvfs buf;
|
||||
|
||||
if (statvfs(path, &buf) == -1) {
|
||||
if (statvfs(ctx->path, &buf) == -1) {
|
||||
/* If statvfs errors, e.g., due to the path not existing,
|
||||
* we consider the device not mounted. */
|
||||
mounted = false;
|
||||
} else {
|
||||
char *sanitized = sstrdup(path);
|
||||
char *sanitized = sstrdup(ctx->path);
|
||||
if (strlen(sanitized) > 1 && sanitized[strlen(sanitized) - 1] == '/')
|
||||
sanitized[strlen(sanitized) - 1] = '\0';
|
||||
FILE *mntentfile = setmntent("/etc/mtab", "r");
|
||||
@ -171,14 +172,14 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
|
||||
#endif
|
||||
|
||||
if (!mounted) {
|
||||
if (format_not_mounted == NULL)
|
||||
format_not_mounted = "";
|
||||
selected_format = format_not_mounted;
|
||||
} else if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
|
||||
if (ctx->format_not_mounted == NULL)
|
||||
ctx->format_not_mounted = "";
|
||||
selected_format = ctx->format_not_mounted;
|
||||
} else if (ctx->low_threshold > 0 && below_threshold(buf, ctx->prefix_type, ctx->threshold_type, ctx->low_threshold)) {
|
||||
START_COLOR("color_bad");
|
||||
colorful_output = true;
|
||||
if (format_below_threshold != NULL)
|
||||
selected_format = format_below_threshold;
|
||||
if (ctx->format_below_threshold != NULL)
|
||||
selected_format = ctx->format_below_threshold;
|
||||
}
|
||||
|
||||
char string_free[STRING_SIZE];
|
||||
@ -190,10 +191,10 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
|
||||
char string_percentage_used[STRING_SIZE];
|
||||
char string_percentage_avail[STRING_SIZE];
|
||||
|
||||
print_bytes_human(string_free, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bfree, prefix_type);
|
||||
print_bytes_human(string_used, (uint64_t)buf.f_frsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
|
||||
print_bytes_human(string_total, (uint64_t)buf.f_frsize * (uint64_t)buf.f_blocks, prefix_type);
|
||||
print_bytes_human(string_avail, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bavail, prefix_type);
|
||||
print_bytes_human(string_free, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bfree, ctx->prefix_type);
|
||||
print_bytes_human(string_used, (uint64_t)buf.f_frsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), ctx->prefix_type);
|
||||
print_bytes_human(string_total, (uint64_t)buf.f_frsize * (uint64_t)buf.f_blocks, ctx->prefix_type);
|
||||
print_bytes_human(string_avail, (uint64_t)buf.f_frsize * (uint64_t)buf.f_bavail, ctx->prefix_type);
|
||||
snprintf(string_percentage_free, STRING_SIZE, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark);
|
||||
snprintf(string_percentage_used_of_avail, STRING_SIZE, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark);
|
||||
snprintf(string_percentage_used, STRING_SIZE, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark);
|
||||
@ -210,12 +211,13 @@ void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const ch
|
||||
{.name = "%percentage_avail", .value = string_percentage_avail}};
|
||||
|
||||
const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
|
||||
buffer = format_placeholders(selected_format, &placeholders[0], num);
|
||||
char *formatted = format_placeholders(selected_format, &placeholders[0], num);
|
||||
OUTPUT_FORMATTED;
|
||||
free(formatted);
|
||||
|
||||
if (colorful_output)
|
||||
END_COLOR;
|
||||
|
||||
*outwalk = '\0';
|
||||
OUTPUT_FULL_TEXT(buffer);
|
||||
free(buffer);
|
||||
OUTPUT_FULL_TEXT(ctx->buf);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user