diff --git a/src/main.rs b/src/main.rs index 0331555..92d5a62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use std::io::{self, Write}; use std::process::Command; use std::sync::mpsc; use std::time::Duration; +use std::fs; +use std::path::Path; use chrono::Local; use signal_hook::consts::SIGUSR1; @@ -10,6 +12,7 @@ use signal_hook::iterator::Signals; enum Update { Clock(String), Volume(String), + Temp(String), } fn query_volume() -> String { @@ -36,6 +39,47 @@ fn query_volume() -> String { } } +fn query_cpu_temp() -> String { + let mut max_temp: Option = None; + let hwmon_path = Path::new("/sys/class/hwmon"); + + let hwmon_dirs = match fs::read_dir(hwmon_path) { + Ok(d) => d, + Err(_) => return "temp:?".to_string(), + }; + + for dir_entry in hwmon_dirs.flatten() { + let dir_path = dir_entry.path(); + let entries = match fs::read_dir(&dir_path) { + Ok(e) => e, + Err(_) => continue, + }; + + for entry in entries.flatten() { + let filename = entry.file_name(); + let filename_str = filename.to_string_lossy(); + + if filename_str.starts_with("temp") && filename_str.ends_with("_input") { + let file_path = entry.path(); + if let Ok(content) = fs::read_to_string(&file_path) { + if let Ok(temp_milli) = content.trim().parse::() { + let temp_c = temp_milli / 1000; + max_temp = Some(match max_temp { + Some(current_max) => current_max.max(temp_c), + None => temp_c, + }); + } + } + } + } + } + + match max_temp { + Some(t) => format!("temp:{}°C", t), + None => "temp:?".to_string(), + } +} + fn main() { let (tx, rx) = mpsc::channel::(); @@ -61,10 +105,19 @@ fn main() { }); } + { + let tx = tx.clone(); + std::thread::spawn(move || loop { + tx.send(Update::Temp(query_cpu_temp())).unwrap(); + std::thread::sleep(Duration::from_secs(5)); + }); + } + drop(tx); let mut clock = String::new(); let mut volume = String::new(); + let mut temp = String::new(); let stdout = io::stdout(); let mut handle = stdout.lock(); @@ -72,8 +125,9 @@ fn main() { match update { Update::Clock(s) => clock = s, Update::Volume(s) => volume = s, + Update::Temp(s) => temp = s, } - writeln!(handle, "{volume} | {clock}").expect("failed to write to stdout"); + writeln!(handle, "{volume} | {temp} | {clock}").expect("failed to write to stdout"); handle.flush().expect("failed to flush stdout"); } }