Skip to content

Commit

Permalink
rename sensors item "label" to "component" to add a label option
Browse files Browse the repository at this point in the history
  • Loading branch information
acheronfail committed Jan 22, 2024
1 parent fae14f7 commit fae12c8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
6 changes: 3 additions & 3 deletions bin/sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use sysinfo::{ComponentExt, RefreshKind, System, SystemExt};
///
/// Each line contains a sensor and its temperature, in the following format:
///
/// TEMP:LABEL
/// TEMP:COMPONENT
///
/// Where TEMP is the temperature in Celsius, and LABEL is the name of the sensor.
/// The LABEL property can by used to configure bar items with type "sensors".
/// Where TEMP is the temperature in Celsius, and COMPONENT is the name of the sensor.
/// The COMPONENT property can by used to configure bar items with type "sensors".
struct Cli;

fn main() {
Expand Down
4 changes: 3 additions & 1 deletion sample_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ name = "cpu-temp"
interval = "5s"
# The name of the component to display temperature for.
# The `i3stat-sensors` command is provided and outputs a list of components that can be used.
label = "coretemp Package id 0"
component = "coretemp Package id 0"
# Optional: set a label to be shown in the item (useful if you have multiple sensor items)
label = " (cpu)"
# See: FLOAT FORMAT OPTIONS
pad = ' '
pad_count = 2
Expand Down
13 changes: 9 additions & 4 deletions src/bar_items/sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use crate::util::format::{float, FloatFormat};
pub struct Sensors {
#[serde(with = "crate::human_time")]
interval: Duration,
label: String,
#[serde(default)]
label: Option<String>,
component: String,
#[serde(flatten)]
float_fmt: FloatFormat,
}
Expand All @@ -40,10 +42,11 @@ impl BarItem for Sensors {
ctx.state.sys.refresh_components_list();
}

let label = self.label.as_deref().unwrap_or("");
loop {
let temp = {
let search = ctx.state.sys.components_mut().iter_mut().find_map(|c| {
if c.label() == self.label {
if c.label() == self.component {
c.refresh();
Some(c.temperature())
} else {
Expand All @@ -54,14 +57,16 @@ impl BarItem for Sensors {
match search {
Some(temp) => temp,
None => {
break Err(format!("no component found with label: {}", self.label).into())
break Err(
format!("no component found with name: {}", self.component).into()
)
}
}
};

let (icon, color) = Self::get_icon(&ctx.config.theme, temp as u32);
let temp = float(temp, &self.float_fmt);
let mut item = I3Item::new(format!("{} {}°C", icon, temp))
let mut item = I3Item::new(format!("{} {}°C{}", icon, temp, label))
.short_text(format!("{}C", temp))
.markup(I3Markup::Pango);

Expand Down
2 changes: 1 addition & 1 deletion tests/i3/screenshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ screenshot!(
json!({
"type": "sensors",
"interval": "1s",
"label": "name temp1"
"component": "name temp1"
}),
{
at_0: {
Expand Down

0 comments on commit fae12c8

Please sign in to comment.