Skip to content

Commit

Permalink
blocks/cpu: Add critical/warning/info values in configuration (#1983)
Browse files Browse the repository at this point in the history
blocks/cpu: Add critical/warning/info values in configuration

Closes #1915
  • Loading branch information
IshanGrover2004 authored Jan 15, 2024
1 parent 061a7bf commit 718c27e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions examples/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ bat_charging = "|^| "

[[block]]
block = "cpu"
info_cpu = 20
warning_cpu = 50
critical_cpu = 90

[[block]]
block = "disk_space"
Expand Down
20 changes: 16 additions & 4 deletions src/blocks/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//! `format` | A string to customise the output of this block. See below for available placeholders. | `" $icon $utilization "`
//! `format_alt` | If set, block will switch between `format` and `format_alt` on every click | `None`
//! `interval` | Update interval in seconds | `5`
//! `info_cpu` | Percentage of CPU usage, where state is set to info | `30.0`
//! `warning_cpu` | Percentage of CPU usage, where state is set to warning | `60.0`
//! `critical_cpu` | Percentage of CPU usage, where state is set to critical | `90.0`
//!
//! Placeholder | Value | Type | Unit
//! -----------------|----------------------------------------------------------------------|--------|---------------
Expand All @@ -31,6 +34,9 @@
//! interval = 1
//! format = " $icon $barchart $utilization "
//! format_alt = " $icon $frequency{ $boost|} "
//! info_cpu = 20
//! warning_cpu = 50
//! critical_cpu = 90
//! ```
//!
//! # Icons Used
Expand All @@ -56,6 +62,12 @@ pub struct Config {
pub format_alt: Option<FormatConfig>,
#[default(5.into())]
pub interval: Seconds,
#[default(30.0)]
pub info_cpu: f64,
#[default(60.0)]
pub warning_cpu: f64,
#[default(90.0)]
pub critical_cpu: f64,
}

pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
Expand Down Expand Up @@ -126,10 +138,10 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let mut widget = Widget::new().with_format(format.clone());
widget.set_values(values);
widget.state = match utilization_avg {
x if x > 0.9 => State::Critical,
x if x > 0.6 => State::Warning,
x if x > 0.3 => State::Info,
widget.state = match utilization_avg * 100. {
x if x > config.critical_cpu => State::Critical,
x if x > config.warning_cpu => State::Warning,
x if x > config.info_cpu => State::Info,
_ => State::Idle,
};
api.set_widget(widget)?;
Expand Down

0 comments on commit 718c27e

Please sign in to comment.