From 628c5e6125e8a318b65accea7954ff8701244d98 Mon Sep 17 00:00:00 2001 From: Atsukoro1 Date: Thu, 1 Dec 2022 10:59:20 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Ram?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 2 +- src/{system.rs => system/host.rs} | 19 --------- src/system/mod.rs | 3 ++ src/system/net.rs | 68 +++++++++++++++++++++++++++++++ src/system/specs.rs | 64 +++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 20 deletions(-) rename src/{system.rs => system/host.rs} (73%) create mode 100644 src/system/mod.rs create mode 100644 src/system/net.rs create mode 100644 src/system/specs.rs diff --git a/src/main.rs b/src/main.rs index e100494..4935410 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ mod system; fn main() { - println!("{}", system::get_ipaddr()) + println!("{}", system::specs::get_ram_used()) } diff --git a/src/system.rs b/src/system/host.rs similarity index 73% rename from src/system.rs rename to src/system/host.rs index 6b0c362..518f52e 100644 --- a/src/system.rs +++ b/src/system/host.rs @@ -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::>()[0].to_string(); - } - }); - - interface } \ No newline at end of file diff --git a/src/system/mod.rs b/src/system/mod.rs new file mode 100644 index 0000000..cf477a9 --- /dev/null +++ b/src/system/mod.rs @@ -0,0 +1,3 @@ +pub mod host; +pub mod net; +pub mod specs; \ No newline at end of file diff --git a/src/system/net.rs b/src/system/net.rs new file mode 100644 index 0000000..d7eb97e --- /dev/null +++ b/src/system/net.rs @@ -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 = 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::>()[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::>()[1].to_string(); + final_str.lock().unwrap().push_str(&ip); + }; + + lines.into_iter().for_each(|line| { + if next { + line.replace("\t", "") + .split(" ") + .collect::>() + .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 +} \ No newline at end of file diff --git a/src/system/specs.rs b/src/system/specs.rs new file mode 100644 index 0000000..fbba0e3 --- /dev/null +++ b/src/system/specs.rs @@ -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 = 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::>()[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 = 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::>()[1].to_string() + .replace("\t", "") + .replace("kB", "") + .replace(" ", "") + .parse::() + .unwrap(); + + (kbs / 1024).to_string() +} \ No newline at end of file