Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion man/i3status.man
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,13 @@ when above degraded threshold can be customized with
For displaying the Nth CPU usage, you can use the %cpu<N> format string,
starting from %cpu0. This feature is currently not supported in FreeBSD.

CPU usage can also be represented graphically using %usagebar and %cpubar<N>
instead of, respectively, %usage and %cpu<N>. This feature requires the use
of a (preferrably, monospace) font implementing "Block Elements"

*Example order*: +cpu_usage+

*Example format*: +all: %usage CPU_0: %cpu0 CPU_1: %cpu1+
*Example format*: +all: %usage [%cpubar0%cpubar1]+

*Example max_threshold*: +75+

Expand Down
28 changes: 24 additions & 4 deletions src/print_cpu_usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ static struct cpu_usage prev_all = {0, 0, 0, 0, 0};
static struct cpu_usage *prev_cpus = NULL;
static struct cpu_usage *curr_cpus = NULL;

static const char *bars[] = {" ", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};

/*
* Reads the CPU utilization from /proc/stat and returns the usage as a
* percentage.
Expand Down Expand Up @@ -201,14 +203,27 @@ 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);
walk += strlen("usage");
if (BEGINS_WITH(walk + 1, "usagebar")) {
if (diff_usage > 98)
diff_usage = 98;
outwalk += sprintf(outwalk, "%s", bars[diff_usage / 11]);
walk += strlen("usagebar");
} else {
outwalk += sprintf(outwalk, "%02d%s", diff_usage, pct_mark);
walk += strlen("usage");
}
}
#if defined(__linux__)
else if (BEGINS_WITH(walk + 1, "cpu")) {
bool bar = false;
if (BEGINS_WITH(walk + 1, "cpubar"))
bar = true;
int number = -1;
int length = strlen("cpu");
sscanf(walk + 1, "cpu%d%n", &number, &length);
if (bar)
sscanf(walk + 1, "cpubar%d%n", &number, &length);
else
sscanf(walk + 1, "cpu%d%n", &number, &length);
if (number == -1) {
fprintf(stderr, "i3status: provided CPU number cannot be parsed\n");
} else if (number >= cpu_count) {
Expand All @@ -217,7 +232,12 @@ 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);
if (bar) {
if (cpu_diff_usage > 98)
cpu_diff_usage = 98;
outwalk += sprintf(outwalk, "%s", bars[cpu_diff_usage / 11]);
} else
outwalk += sprintf(outwalk, "%02d%s", cpu_diff_usage, pct_mark);
}
walk += length;
}
Expand Down