Skip to content

Commit

Permalink
refactor temperature core_inputs option
Browse files Browse the repository at this point in the history
  • Loading branch information
doums committed Aug 11, 2024
1 parent 94c7310 commit aacbb93
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
46 changes: 26 additions & 20 deletions baru.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -470,27 +470,33 @@ temperature:
# coretemp: String, default: /sys/devices/platform/coretemp.0/hwmon
#
# The path without the last directory level (because it varies on each kernel boot).
# Under this variant directory must be located the tempx_input files.
# Under this variable directory are located the input files (see below).
# For example on my machine it can be /sys/devices/platform/coretemp.0/hwmon/hwmon7 or hwmon6 etc.
# This last directory is resolved dynamically.
#
coretemp: /sys/devices/platform/coretemp.1/hwmon

# core_inputs: String, default: 1
#
# The average temperature is calculated with one or several tempx_input files.
# tempx_input files can contain the temperature of a cpu core.
# Based on his cpu (and number of cores), the user must find the correct file(s).
#
# Can be a number to select one file. eg. 1 for temp1_input.
# Can be a inclusive range to select several files, for example:
# 1..4
# temp1_input
# temp2_input
# temp3_input
# temp4_input
#
core_inputs: 2..5
# The variable directory is resolved dynamically.
#
coretemp: /sys/devices/platform/coretemp.0/hwmon

# core_inputs: u32 | List of u32 | String, default: 1
#
# The average temperature is calculated from one or several input files.
# Input files are files named `tempx_input` where x is a number.
# e.g. temp1_input temp2_input
# You can list them using `ls -l /sys/devices/platform/coretemp.0/hwmon/* `
# Input files can contain the temperature of a cpu core.
# Based on the cpu and its number of cores you want to provide
# the corresponding input file number(s).
# Tips: see the label files, e.g. `temp1_label`, to identify
# which input files reports cpu core temperature.
#
# Can be a number to select one input file, eg. 1 for temp1_input.
# Can be a list of number to select multiple input files, eg.
# core_inputs: [ 2, 6 ]
# to select temp2_input and temp6_input
# Can be a inclusive range to select multiple input files, eg.
# core_inputs: 2..3
# to select temp1_input temp2_input and temp3_input
#
core_inputs: 1

# high_level: u32, default: 75
#
Expand Down
1 change: 1 addition & 0 deletions lib/audio/src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void subscription_cb(pa_context *context, pa_subscription_event_type_t t, uint32
try_free_op(&m->server_op);
m->server_op = pa_context_get_server_info(m->context, server_info_cb, main);
break;
default:;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fn parse_status(line: &io::Result<String>) -> Option<String> {
None
}

fn find_attribute_prefix<'a, 'b>(path: &'a str) -> Result<&'b str, Error> {
fn find_attribute_prefix<'e>(path: &str) -> Result<&'e str, Error> {
let content = fs::read_to_string(path)?;
let mut unit = None;
if content.contains(&format!(
Expand Down
2 changes: 1 addition & 1 deletion src/cpu_freq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub fn run(
true => format!(
"{}/{}",
humanize(avg, config.unit),
humanize(config.max_freq as f32, config.unit)
humanize(config.max_freq, config.unit)
),
false => humanize(avg, config.unit),
};
Expand Down
38 changes: 25 additions & 13 deletions src/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ const LABEL: &str = "tem";
const HIGH_LABEL: &str = "!te";
const FORMAT: &str = "%l:%v";

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
enum CoreInputs {
Single(u32),
Range(String),
List(Vec<u32>),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
coretemp: Option<String>,
high_level: Option<u32>,
core_inputs: Option<String>,
core_inputs: Option<CoreInputs>,
tick: Option<u32>,
placeholder: Option<String>,
label: Option<String>,
Expand Down Expand Up @@ -75,19 +83,23 @@ impl<'a> TryFrom<&'a MainConfig> for InternalConfig<'a> {
high_label = v;
}
if let Some(i) = &c.core_inputs {
if let Ok(v) = i.parse::<u32>() {
inputs.push(v);
} else if let Some(caps) = re.captures(i) {
let start = caps.get(1).unwrap().as_str().parse::<u32>().unwrap();
let end = caps.get(2).unwrap().as_str().parse::<u32>().unwrap();
if start > end {
return Err(Error::new(error));
}
for i in start..end + 1 {
inputs.push(i)
match i {
CoreInputs::Single(n) => inputs.push(*n),
CoreInputs::Range(range) => {
if let Some(captured) = re.captures(range) {
let start = captured.get(1).unwrap().as_str().parse::<u32>().unwrap();
let end = captured.get(2).unwrap().as_str().parse::<u32>().unwrap();
if start > end {
return Err(Error::new(error));
}
for i in start..end + 1 {
inputs.push(i)
}
} else {
return Err(Error::new(error));
}
}
} else {
return Err(Error::new(error));
CoreInputs::List(list) => inputs = list.clone(),
}
}
}
Expand Down

0 comments on commit aacbb93

Please sign in to comment.