Impl fetching /proc/loadavg

This commit is contained in:
Stephen Seo 2022-07-09 17:04:49 +09:00
parent 0df98bc2a9
commit d7990af228
2 changed files with 31 additions and 5 deletions

View file

@ -10,10 +10,15 @@ fn main() {
println!("["); println!("[");
let mut array = swaybar_object::SwaybarArray::new(); let mut array = swaybar_object::SwaybarArray::new();
array.push_object(swaybar_object::SwaybarObject::default()); array.push_object(swaybar_object::SwaybarObject::default());
array.push_object(swaybar_object::SwaybarObject::new()); {
array.push_object(swaybar_object::SwaybarObject::default()); let meminfo_string = proc::get_meminfo().expect("Should be able to get meminfo");
let meminfo_string = proc::get_meminfo().expect("Should be able to get meminfo"); let meminfo_object = swaybar_object::SwaybarObject::from_string(meminfo_string);
let meminfo_object = swaybar_object::SwaybarObject::from_string(meminfo_string); array.push_object(meminfo_object);
array.push_object(meminfo_object); }
{
let loadavg_string = proc::get_loadavg().expect("Should be able to get loadavg");
let loadavg_object = swaybar_object::SwaybarObject::from_string(loadavg_string);
array.push_object(loadavg_object);
}
println!("{}", array); println!("{}", array);
} }

View file

@ -65,3 +65,24 @@ pub fn get_meminfo() -> io::Result<String> {
Ok(output) Ok(output)
} }
} }
pub fn get_loadavg() -> io::Result<String> {
let mut loadavg_string = String::new();
{
let mut loadavg_file: File = File::open("/proc/loadavg")?;
loadavg_file.read_to_string(&mut loadavg_string)?;
}
let loadavg_parts: Vec<&str> = loadavg_string.split_whitespace().collect();
if loadavg_parts.len() < 3 {
return Err(io::Error::new(
io::ErrorKind::Other,
"loadavg: failed to parse",
));
}
Ok(format!(
"{} {} {}",
loadavg_parts[0], loadavg_parts[1], loadavg_parts[2]
))
}