Skip to content

cpu_usage: apply thresholds to every displayed value - #563

Open
verpeteren wants to merge 3 commits into
i3:mainfrom
verpeteren:cpu_colors
Open

verpeteren wants to merge 3 commits into
i3:mainfrom
verpeteren:cpu_colors

Conversation

@verpeteren

@verpeteren verpeteren commented Nov 11, 2026 •

Copy link
Copy Markdown

What

cpu_usage currently colors the whole line with color_degraded / color_bad based on the overall usage. With a format that shows many CPUs, that does not tell which CPU is busy, and a single busy CPU usually does not move the overall usage enough to color anything.

This PR applies degraded_threshold and max_threshold to every displayed value on its own: %usage is colored based on the overall usage, each %cpu<N> based on the usage of that CPU.

examples

config

general {
        colors = true
        interval = 5
}

order += "cpu_usage"

cpu_usage {
    max_threshold = 10
    degraded_threshold = 5
    format = "%cpu0 %cpu1 %cpu2 %cpu3 %cpu4 %cpu5 %cpu6 %cpu7 %cpu8 %cpu9 %cpu10 %cpu11 %cpu12 %cpu13 %cpu14 %cpu15 %cpu16 %cpu17 %cpu18 %cpu19 %cpu20 %cpu21 %cpu22 %cpu23 %cpu24 %cpu25 %cpu26 %cpu27 %cpu28 %cpu29 %cpu30 %cpu31 %cpu32 %cpu33 %cpu34 %cpu35 %cpu36 %cpu37 %cpu38 %cpu39 | %usage"
}

screenshots

cpu_colors cpu_colors_2 cpu_colors_3 cpu_colors_4

Commits

  1. Extract threshold color selection into usage_color(): refactoring only, no functional change.
  2. Apply thresholds to every displayed value: the actual change, plus a new test case and the man page update.
  3. Pad usage values to three characters: %3d instead of %02d, so the width of the output does not change when a value switches between 99% and 100%. This one is independent of the other two and can be dropped if it is not wanted.

Behavior changes

  • The literal text of the format is no longer colored, only the values.
  • format_above_threshold / format_above_degraded_threshold are unchanged: they are still selected based on the overall usage.
  • Values are padded with blanks to three characters ( 8%, 42%, 100%) instead of zero-padded to two (08%).

Output formats

Output format Coloring of a single value
term, xmobar color() / endcolor()
dzen2, lemonbar color() plus an explicit reset (^fg() / %{F-}), because endcolor() does not reset the color for these. This is kept local to print_cpu_usage.c.
i3bar with markup = "pango" <span foreground='…'>…</span>
i3bar without pango markup not possible, so the whole block is colored based on the overall usage, as before
none no colors

Because colored values are a lot longer than their visible width, the values are now written with snprintf bounded by the buffer size. A value that does not fit is dropped.

Open question (reason for the draft)

For i3bar, per-value colors need markup = "pango" in the general section, which also changes how the formats of all other modules are parsed. Alternatives would be to set "markup": "pango" only on the cpu_usage block (which breaks for bars that do not use a pango font), or to emit one block per value. I went with the opt-in because it cannot break existing setups, but I am happy to change it.

Testing

  • ninja test passes after each of the three commits.
  • New test case 027-cpu-usage-colors (xmobar output) covers a degraded, a bad and an uncolored value. The expected outputs of 010, 011 and 022 are updated for the three character padding.
  • Checked the output manually for term, xmobar, dzen2, lemonbar, i3bar (with and without pango markup), none and with colors = false.
  • Not tested on the BSDs. %cpu<N> is Linux only, the %usage path is shared.
  • Formatted with clang-format 19; I did not have clang-format-12 (used by the CI) available.

No functional change. This prepares for applying the thresholds to each displayed value instead of to the whole line.
Color %usage and each %cpu<N> based on its own value instead of coloring the whole line based on the overall usage. The remaining text of the format is no longer colored.

i3bar can only color a whole block, so this needs markup = "pango" there. Without it the whole block is still colored based on the overall usage.
Pad with blanks instead of a leading zero, so that the width of the output does not change when a value switches between 99% and 100%.
@verpeteren
verpeteren marked this pull request as ready for review November 11, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant