feat: 🎸 Init

This commit is contained in:
Atsukoro1 2022-12-01 09:06:00 +01:00
parent 0959e88cb4
commit d94d023576
4 changed files with 95 additions and 0 deletions

5
.gitignore vendored
View file

@ -8,3 +8,8 @@ Cargo.lock
# These are backup files generated by rustfmt # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk
# Added by cargo
/target

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "ponyfetch"
version = "0.1.0"
edition = "2021"
[dependencies]

5
src/main.rs Normal file
View file

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

79
src/system.rs Normal file
View file

@ -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<String> = 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::<Vec<&str>>()[1].to_string()
.replace("\"", "")
);
}
if line.contains("BUILD_ID") {
distro = Rc::new(
format!("{} ({})", distro,
line.split("=")
.collect::<Vec<&str>>()[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::<Vec<&str>>()[0].to_string();
}
});
interface
}