up request

This commit is contained in:
Romulus21
2022-03-14 22:13:01 +01:00
parent 76185d4327
commit 664e57cead

View File

@@ -1,19 +1,14 @@
use std::{ use std::{env, process::Command};
env,
process::Command,
};
pub fn get_temperature() -> f32 { pub fn get_temperature() -> f32 {
let mut temperature = 0.0; let mut temperature = 0.0;
let system = env::var("SYSTEM").unwrap().to_string(); let system = env::var("SYSTEM").unwrap().to_string();
if system == "ubuntu" { if system == "ubuntu" {
let temperature_command = Command::new("cat") let temperature_command = Command::new("cat")
.arg("/sys/class/thermal/thermal_zone1/temp") .arg("/sys/class/thermal/thermal_zone1/temp")
.output().unwrap_or_else(|e| { .output()
panic!("failed to execute process: {}", e) .unwrap_or_else(|e| panic!("failed to execute process: {}", e));
});
let temperature_string = &String::from_utf8_lossy(&temperature_command.stdout)[..5]; let temperature_string = &String::from_utf8_lossy(&temperature_command.stdout)[..5];
temperature = temperature_string.parse::<f32>().unwrap(); temperature = temperature_string.parse::<f32>().unwrap();
@@ -22,9 +17,8 @@ pub fn get_temperature() -> f32 {
let temperature_command = Command::new("sudo") let temperature_command = Command::new("sudo")
.arg("/opt/vc/bin/vcgencmd") .arg("/opt/vc/bin/vcgencmd")
.arg("measure_temp") .arg("measure_temp")
.output().unwrap_or_else(|e| { .output()
panic!("failed to execute process: {}", e) .unwrap_or_else(|e| panic!("failed to execute process: {}", e));
});
let mut temperature_string = &String::from_utf8_lossy(&temperature_command.stdout)[5..]; let mut temperature_string = &String::from_utf8_lossy(&temperature_command.stdout)[5..];
temperature_string = &temperature_string[..temperature_string.len() - 3]; temperature_string = &temperature_string[..temperature_string.len() - 3];
@@ -38,28 +32,28 @@ pub fn get_storage() -> i8 {
let storage_command = Command::new("df") let storage_command = Command::new("df")
.arg("-h") .arg("-h")
.arg("/") .arg("/")
.output().unwrap_or_else(|e| { .output()
panic!("failed to execute process: {}", e) .unwrap_or_else(|e| panic!("failed to execute process: {}", e));
});
let storage_string = &String::from_utf8_lossy(&storage_command.stdout); let storage_string = &String::from_utf8_lossy(&storage_command.stdout);
let vec_storage : Vec<&str> = storage_string.split_whitespace().collect(); let vec_storage: Vec<&str> = storage_string.split_whitespace().collect();
let storage = (vec_storage[vec_storage.len() - 2][..2]).parse::<i8>().unwrap(); let mut value = vec_storage[vec_storage.len() - 2].to_string();
value.pop();
let storage = value.parse::<i8>().unwrap();
return storage; return storage;
} }
pub fn get_mem() -> i8 { pub fn get_mem() -> i8 {
let mem_command = Command::new("free") let mem_command = Command::new("free")
.arg("-m") .arg("-m")
.output().unwrap_or_else(|e| { .output()
panic!("failed to execute process: {}", e) .unwrap_or_else(|e| panic!("failed to execute process: {}", e));
});
let mem_string = &String::from_utf8_lossy(&mem_command.stdout); let mem_string = &String::from_utf8_lossy(&mem_command.stdout);
let vec_mem : Vec<&str> = mem_string.split_whitespace().collect(); let vec_mem: Vec<&str> = mem_string.split_whitespace().collect();
let total = vec_mem[7].parse::<f32>().unwrap(); let total = vec_mem[7].parse::<f32>().unwrap();
let mem_use = vec_mem[8].parse::<f32>().unwrap(); let mem_use = vec_mem[8].parse::<f32>().unwrap();
let result = (mem_use / total * 100.0) as i8; let result = (mem_use / total * 100.0) as i8;
return result; return result;
@@ -71,13 +65,14 @@ pub fn get_battery() -> i8 {
if system == "ubuntu" { if system == "ubuntu" {
let battery_command = Command::new("cat") let battery_command = Command::new("cat")
.arg("/sys/class/power_supply/BAT0/capacity") .arg("/sys/class/power_supply/BAT0/capacity")
.output().unwrap_or_else(|e| { .output()
panic!("failed to execute process: {}", e) .unwrap_or_else(|e| panic!("failed to execute process: {}", e));
});
let battery_string = &String::from_utf8_lossy(&battery_command.stdout); let battery_string = &String::from_utf8_lossy(&battery_command.stdout);
battery = (&battery_string[..battery_string.len() - 1]).parse::<i8>().unwrap(); battery = (&battery_string[..battery_string.len() - 1])
.parse::<i8>()
.unwrap();
} }
return battery; return battery;