ponyfetch/src/system/specs.rs
2022-12-01 14:00:06 +01:00

74 lines
No EOL
1.8 KiB
Rust

use crate::helpers::file::file_open;
use std::fs::File;
use std::io::Read;
use std::rc::Rc;
#[cfg(target_os = "linux")]
pub fn get_cpu() -> String {
let mut cpu: Rc<String> = Rc::new(String::new());
let mut temp_buf: String = String::new();
let mut file = File::open("/proc/cpuinfo").unwrap();
file.read_to_string(&mut temp_buf).unwrap();
let lines: &Vec<&str> = &temp_buf.lines().collect();
lines.into_iter().for_each(|line| {
if line.contains("model name") {
cpu = Rc::new(
line.split(":")
.collect::<Vec<&str>>()[1].to_string()
.replace("\t", "")
);
cpu = Rc::new(cpu.replacen(" ", "", 1));
}
});
cpu.to_string()
}
#[cfg(target_os = "linux")]
pub fn get_ram_used() -> String {
let temp_buf: String = file_open("/proc/meminfo");
let lines: &Vec<&str> = &temp_buf.lines().collect();
let mut total: u128 = 0;
let mut available: u128 = 0;
lines.into_iter().for_each(|line| {
if line.contains("MemTotal") {
total = eval_ram(line.to_string());
} else if line.contains("MemAvailable") {
available = eval_ram(line.to_string());
}
});
format!(
"{}M / {}M",
total - available,
total
)
}
#[cfg(target_os = "linux")]
fn eval_ram(line: String) -> u128 {
let kbs: u128 = line.split(":")
.collect::<Vec<&str>>()[1].to_string()
.replace("\t", "")
.replace("kB", "")
.replace(" ", "")
.parse::<u128>()
.unwrap();
(kbs / 1000)
}
#[cfg(target_os = "linux")]
pub fn get_kernel() -> String {
let temp_buf: String = file_open("/proc/version");
temp_buf.split(" ")
.collect::<Vec<&str>>()[2]
.to_string()
}