tfetch/src/main.rs

96 lines
2.6 KiB
Rust

use std::env::{args, var};
use std::process::exit;
use std::fs::File;
use std::io::Read;
use tfetch::{kernel, uptime, Os};
pub fn main() {
let mut hostname = String::new();
let mut hostname_file = File::open("/etc/hostname").unwrap();
hostname_file.read_to_string(&mut hostname).unwrap();
let mut os = Os::detect().unwrap();
let mut iterator = args().skip(1);
loop {
let arg = match iterator.next() {
Some(arg) => arg,
None => break,
};
if arg == "-d" {
os = match iterator.next().map(|x| Os::from_name(&x)) {
Some(Some(os)) => os,
Some(None) => {
eprintln!("\x1B[31;1merror:\x1B[0m distribution not recognized");
exit(1);
},
None => {
eprintln!("\x1B[31;1merror:\x1B[0m argument -d expect distribution as parameter");
exit(1);
},
};
continue;
};
}
let style = os.style();
let unstyle = os.unstyle();
let username = format!("{style}{}{unstyle}@{style}{}{unstyle}", var("USER").unwrap(), hostname, style = style, unstyle = unstyle);
let osname = format!("{style}OS:{unstyle} {}\n", Os::ArchLinux.name(), style = style, unstyle = unstyle);
let kernel = format!("{style}Kernel:{unstyle} {}", kernel().unwrap(), style = style, unstyle = unstyle);
let uptime = format!("{style}Uptime:{unstyle} {}", &uptime().unwrap()[3..], style = style, unstyle = unstyle);
let mut info = vec![
String::from("\n"),
username,
osname,
kernel,
uptime,
];
let mut iterator = args().skip(1);
loop {
let arg = match iterator.next() {
Some(arg) => arg,
None => break,
};
if arg == "-d" {
iterator.next();
continue;
};
let split = arg.split(":").collect::<Vec<_>>();
if split.len() != 2 {
eprintln!("\x1B[33;1mwarning:\x1B[0m argument must be formatted like \"key:value\"");
continue;
}
info.push(format!("{style}{}:{unstyle} {}\n", split[0], split[1], style = style, unstyle = unstyle));
}
let mut logo_iterator = os.ascii_logo().lines();
let mut info_iterator = info.iter();
loop {
match (logo_iterator.next(), info_iterator.next()) {
(Some(logo), Some(info)) => print!("{} {}", logo, info),
(Some(logo), None) => println!("{}", logo),
_ => break,
}
}
}