module/cpu: make ‘content’ particle a template

Before this patch, the cpu module instantiated a single particle (the
‘content’ particle), with one tag ("cpu") representing the total CPU
usage, and then one tag (cpuN) for each core.

This makes it cumbersome to configure, since you need to explicitly
reference each cpuN tag to get per-core usage.

This patch rewrites this, so that ‘content’ is now a template. It’s
instantiated once to represent the total CPU usage, and then once for
each core.

Each instance has a "cpu" tag, representing the CPU usage of that
core (or total usage). It also has an "id" tag. The ID is 0..n for
actual cores, and -1 for total usage.

This means you can do something like this in your config:

- cpu:
    content:
      map:
        conditions:
          id < 0: {string: {text: "Total: {cpu}%"}}
          id >= 0: {string: {text: "Core #{id}: {cpu}%"}}

Closes #207
This commit is contained in:
Daniel Eklöf 2022-12-14 11:49:14 +01:00
parent 2e0e1a402f
commit b195bc4dcb
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
4 changed files with 135 additions and 55 deletions

View file

@ -3,17 +3,24 @@ yambar-modules-cpu(5)
# NAME
cpu - This module provides the CPU usage
# DESCRIPTION
This module reports CPU usage, in percent. The _content_ particle is a
template that is instantiated once for each core, and once for the
total CPU usage.
# TAGS
[[ *Name*
:[ *Type*
:[ *Description*
| id
: int
: Core ID. 0..n represents individual cores, and -1 represents the
total usage
| cpu
: range
: Current usage of the whole CPU in percent
| cpu<0..X>
: range
: Current usage of CPU core X in percent
: Current usage of CPU core {id}, in percent
# CONFIGURATION
@ -24,19 +31,49 @@ cpu - This module provides the CPU usage
| interval
: int
: no
: Refresh interval of the CPU usage stats in ms (default=500). Cannot be less then 500 ms
: Refresh interval of the CPU usage stats in ms (default=500). Cannot
be less then 500 ms
# EXAMPLES
## Display total CPU usage as a number
```
bar:
left:
- cpu:
interval: 2500
content:
string: {text: "{cpu1}%"}
map:
conditions:
id < 0:
- string: {text: , font: Font Awesome 6 Free:style=solid}
- string: {text: "{cpu}%"}
```
## Display a vertical bar for each core
```
bar:
left:
- cpu:
interval: 2500
content:
map:
conditions:
id >= 0:
- ramp:
tag: cpu
items:
- string: {text: ▁}
- string: {text: ▂}
- string: {text: ▃}
- string: {text: ▄}
- string: {text: ▅}
- string: {text: ▆}
- string: {text: ▇}
- string: {text: █}
```
# SEE ALSO
*yambar-modules*(5), *yambar-particles*(5), *yambar-tags*(5), *yambar-decorations*(5)