feat: 🎸 Ram

This commit is contained in:
Atsukoro1 2022-12-01 10:59:20 +01:00
parent d94d023576
commit 628c5e6125
5 changed files with 136 additions and 20 deletions

View file

@ -1,5 +1,5 @@
mod system;
fn main() {
println!("{}", system::get_ipaddr())
println!("{}", system::specs::get_ram_used())
}

View file

@ -57,23 +57,4 @@ pub fn get_distro() -> String {
});
distro.to_string()
}
#[cfg(target_os = "linux")]
pub fn get_ipaddr() -> String {
// Get current using network interface
let mut f = File::open("/proc/net/route").unwrap();
let mut intr = String::new();
f.read_to_string(&mut intr).unwrap();
let lines: &Vec<&str> = &intr.lines().collect();
let mut interface = String::new();
lines.into_iter().for_each(|line| {
if line.contains("00000000") {
interface = line.split("\t").collect::<Vec<&str>>()[0].to_string();
}
});
interface
}

3
src/system/mod.rs Normal file
View file

@ -0,0 +1,3 @@
pub mod host;
pub mod net;
pub mod specs;

68
src/system/net.rs Normal file
View file

@ -0,0 +1,68 @@
use std::{fs::File, io::Read};
use std::process::Command;
use std::sync::Mutex;
#[cfg(target_os = "linux")]
pub fn get_ipaddr() -> String {
use std::ops::Add;
let mut final_str: Mutex<String> = Mutex::new(String::new());
let mut f = File::open("/proc/net/route").unwrap();
let mut intr = String::new();
f.read_to_string(&mut intr).unwrap();
let lines: &Vec<&str> = &intr.lines().collect();
let mut interface = String::new();
lines.into_iter().for_each(|line| {
if line.contains("00000000") {
interface = line.split("\t").collect::<Vec<&str>>()[0].to_string();
}
});
let output = Command::new("ifconfig")
.arg(interface.clone())
.output()
.expect("Failed to execute ifconfig");
let output = String::from_utf8(output.stdout).unwrap();
let lines: &Vec<&str> = &output.lines().clone().collect();
let mut next: bool = false;
let process_ip = |line: &str| {
let ip = line.split(" ").collect::<Vec<&str>>()[1].to_string();
final_str.lock().unwrap().push_str(&ip);
};
lines.into_iter().for_each(|line| {
if next {
line.replace("\t", "")
.split(" ")
.collect::<Vec<&str>>()
.into_iter()
.for_each(|item| {
if item.contains("inet") {
process_ip(item);
}
});
next = false;
}
if line.contains(&interface) {
next = !next;
}
});
let x = final_str
.lock()
.unwrap()
.to_string()
.add(format!(" ({})", interface).as_str());
x
}

64
src/system/specs.rs Normal file
View file

@ -0,0 +1,64 @@
use std::sync::Mutex;
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.to_string()
}
#[cfg(target_os = "linux")]
pub fn get_ram_used() -> String {
use std::ops::Add;
let mut ram: Mutex<String> = Mutex::new(String::new());
let mut temp_buf: String = String::new();
let mut file = File::open("/proc/meminfo").unwrap();
file.read_to_string(&mut temp_buf).unwrap();
let lines: &Vec<&str> = &temp_buf.lines().collect();
lines.into_iter().for_each(|line| {
ram.get_mut().unwrap().add("fdlkj");
if line.contains("MemTotal") {
} else if line.contains("MemAvailable") {
}
});
let x = ram.lock().unwrap().to_string(); x
}
#[cfg(target_os = "linux")]
fn eval_ram(line: String) -> String {
let kbs: u128 = line.split(":")
.collect::<Vec<&str>>()[1].to_string()
.replace("\t", "")
.replace("kB", "")
.replace(" ", "")
.parse::<u128>()
.unwrap();
(kbs / 1024).to_string()
}