mirror of
https://github.com/Atsukoro1/ponyfetch.git
synced 2024-11-30 15:48:00 +01:00
feat: 🎸 Ram
This commit is contained in:
parent
d94d023576
commit
628c5e6125
5 changed files with 136 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
|||
mod system;
|
||||
|
||||
fn main() {
|
||||
println!("{}", system::get_ipaddr())
|
||||
println!("{}", system::specs::get_ram_used())
|
||||
}
|
||||
|
|
|
@ -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
3
src/system/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod host;
|
||||
pub mod net;
|
||||
pub mod specs;
|
68
src/system/net.rs
Normal file
68
src/system/net.rs
Normal 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
64
src/system/specs.rs
Normal 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()
|
||||
}
|
Loading…
Reference in a new issue