From d94d023576cb09be650f884a73af99827e8ae069 Mon Sep 17 00:00:00 2001 From: Atsukoro1 Date: Thu, 1 Dec 2022 09:06:00 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Init?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++++ Cargo.toml | 6 ++++ src/main.rs | 5 ++++ src/system.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/system.rs diff --git a/.gitignore b/.gitignore index 088ba6b..22d3516 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,8 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..4a4ab0b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ponyfetch" +version = "0.1.0" +edition = "2021" + +[dependencies] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e100494 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +mod system; + +fn main() { + println!("{}", system::get_ipaddr()) +} diff --git a/src/system.rs b/src/system.rs new file mode 100644 index 0000000..6b0c362 --- /dev/null +++ b/src/system.rs @@ -0,0 +1,79 @@ +use std::{fs::File, io::Read}; +use std::process::Command; + +#[cfg(target_os = "linux")] +pub fn get_hostname() -> String { + let mut hostname = String::new(); + + let mut f = File::open("/etc/hostname").unwrap(); + f.read_to_string(&mut hostname).unwrap(); + + hostname.pop(); + + hostname +} + +#[cfg(target_os = "linux")] +pub fn get_user() -> String { + Command::new("whoami") + .output() + .expect("Failed to execute whoami") + .stdout + .iter() + .map(|&c| c as char) + .collect() +} + +#[cfg(target_os = "linux")] +pub fn get_distro() -> String { + use std::rc::Rc; + + let mut distro: Rc = Rc::new(String::new()); + let mut temp_buf: String = String::new(); + + let mut file = File::open("/etc/os-release").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("PRETTY_NAME") { + distro = Rc::new( + line.split("=") + .collect::>()[1].to_string() + .replace("\"", "") + ); + } + + if line.contains("BUILD_ID") { + distro = Rc::new( + format!("{} ({})", distro, + line.split("=") + .collect::>()[1].to_string() + .replace("\"", "") + ) + ); + } + }); + + 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