From 11e4103e026e23f8231401097779442cc27aa260 Mon Sep 17 00:00:00 2001 From: charklie Date: Sun, 19 May 2024 14:02:15 +0200 Subject: [PATCH] fixed gpu temp with netbsd, version 0.6.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 1 + src/fns.rs | 60 ++++++++++++++++++++++++++++++++---------------------- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2eaeeff..fa4c430 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -846,7 +846,7 @@ dependencies = [ [[package]] name = "rsftch" -version = "0.5.8" +version = "0.5.9" dependencies = [ "colored", "libmacchina", diff --git a/Cargo.toml b/Cargo.toml index 29f9e26..3d42bd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsftch" -version = "0.5.9" +version = "0.6.0" edition = "2021" authors = ["Charklie charliejohanid@gmail.com"] license = "MIT" diff --git a/README.md b/README.md index b1d3ebc..e0a2f38 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,7 @@ Thank you to: ### Todo - [X] Configuration via JSON - [X] Version command +- [ ] Rewrite memory function - [ ] Optimizations (?) - [ ] Automatic ASCII generation - [ ] Add TOML / JSONC support diff --git a/src/fns.rs b/src/fns.rs index 05c6c52..64b55a5 100644 --- a/src/fns.rs +++ b/src/fns.rs @@ -78,23 +78,25 @@ pub fn get_cpu_temp() -> String { fs::read_to_string("/sys/class/thermal/thermal_zone0/temp") .ok() .and_then(|temp_str| temp_str.trim().parse::().ok()) - .map(|temp| format!("{:.1}°C", temp / 1000.0)) + .map(|temp| format!("({:.1}°C)", temp / 1000.0)) .unwrap_or_default() } #[cfg(target_os = "netbsd")] { Command::new("envstat") - .arg("-d") - .arg("acpitz0") .output() .ok() .and_then(|output| String::from_utf8(output.stdout).ok()) .and_then(|output_str| { - output_str.lines().next() - .and_then(|line| line.split_whitespace().nth(1)) + output_str + .lines() + .skip_while(|line| *line != "[acpitz0]") + .nth(1) + .and_then(|line| line.split(':').nth(1)) + .map(|s| s.split_whitespace().next().unwrap_or("")) .and_then(|temp_str| temp_str.parse::().ok()) - .map(|temp| format!("{:.1}°C", temp)) + .map(|temp| format!("({:.1}°C)", temp)) }) .unwrap_or_default() } @@ -109,19 +111,32 @@ fn get_gpu_temp() -> String { .output() .ok() .and_then(|output| String::from_utf8(output.stdout).ok()) - .and_then(|temp_str| temp_str.lines().next().and_then(|s| s.trim().parse::().ok())) - .map(|temp| format!("{:.1}°C", temp)) + .and_then(|temp_str| { + temp_str + .lines() + .next() + .and_then(|s| s.trim().parse::().ok()) + }) + .map(|temp| format!("({:.1}°C)", temp)) .unwrap_or_else(|| { Command::new("sensors") .output() .ok() .and_then(|output| String::from_utf8(output.stdout).ok()) .and_then(|output_str| { - output_str.lines() - .find(|line| line.contains("temp1:") || line.contains("edge:") || line.contains("junction:") || line.contains("mem:")) + output_str + .lines() + .find(|line| { + line.contains("temp1:") + || line.contains("edge:") + || line.contains("junction:") + || line.contains("mem:") + }) .and_then(|line| line.split_whitespace().nth(1)) - .and_then(|temp_str| temp_str.trim_end_matches("°C").parse::().ok()) - .map(|temp| format!("{:.1}°C", temp)) + .and_then(|temp_str| { + temp_str.trim_end_matches("°C").parse::().ok() + }) + .map(|temp| format!("({:.1}°C)", temp)) }) .unwrap_or_default() }) @@ -130,21 +145,23 @@ fn get_gpu_temp() -> String { #[cfg(target_os = "netbsd")] { Command::new("envstat") - .arg("-d") .output() .ok() .and_then(|output| String::from_utf8(output.stdout).ok()) .and_then(|output_str| { - output_str.lines().find(|line| line.contains("gpu0 temperature")) - .and_then(|line| line.split_whitespace().nth(2)) + output_str + .lines() + .skip_while(|line| *line != "[acpitz2]") + .nth(1) + .and_then(|line| line.split(':').nth(1)) + .map(|s| s.split_whitespace().next().unwrap_or("")) .and_then(|temp_str| temp_str.parse::().ok()) - .map(|temp| format!("{:.1}°C", temp)) + .map(|temp| format!("({:.1}°C)", temp)) }) .unwrap_or_default() } } - pub fn get_disk_usage() -> String { let output_str = match Command::new("df").arg("-h").output() { Ok(output) if output.status.success() => { @@ -188,7 +205,7 @@ pub fn get_cpu_info() -> String { } format!( - "{}({})", + "{}{}", &cpu.split('@').next().unwrap_or_default(), get_cpu_temp() ) @@ -597,12 +614,7 @@ pub fn get_gpu_info() -> Result { "GPU name not found", ))? + start_index; let gpu_name = &line[start_index..end_index]; - return Ok(format!( - "{} {} ({})", - prefix, - gpu_name.trim(), - get_gpu_temp() - )); + return Ok(format!("{} {} {}", prefix, gpu_name.trim(), get_gpu_temp())); } }