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