tfetch/src/lib.rs

79 lines
1.9 KiB
Rust

use std::io;
use std::process::Command;
pub mod logos;
#[derive(Copy, Clone, Debug)]
pub enum Os {
ArchLinux,
Debian,
Manjaro,
Mint,
Suse,
Ubuntu,
}
impl Os {
pub fn detect() -> Option<Os> {
let lsb_release = lsb_release().unwrap();
match lsb_release.trim() {
"archlinux" | "Arch Linux" | "arch" | "Arch" | "archarm" => Some(Os::ArchLinux),
"Debian" => Some(Os::Debian),
"ManjaroLinux" => Some(Os::Manjaro),
"LinuxMint" => Some(Os::Mint),
"openSUSE" | "openSUSE project" | "SUSE LINUX" | "SUSE" => Some(Os::Suse),
"Ubuntu" => Some(Os::Ubuntu),
_ => None,
}
}
pub fn ascii_logo(self) -> &'static str {
match self {
Os::ArchLinux => logos::archlinux::ASCII_LOGO,
Os::Ubuntu => logos::ubuntu::ASCII_LOGO,
Os::Debian => logos::debian::ASCII_LOGO,
_ => unimplemented!(),
}
}
pub fn name(self) -> &'static str {
match self {
Os::ArchLinux => "Arch Linux",
Os::Ubuntu => "Ubuntu",
_ => unimplemented!(),
}
}
pub fn style(self) -> &'static str {
match self {
Os::ArchLinux => "\x1B[36;1m",
Os::Ubuntu => "\x1B[31;1m",
Os::Debian => "\x1B[31;1m",
_ => unimplemented!(),
}
}
pub fn unstyle(self) -> &'static str {
"\x1B[0m"
}
}
macro_rules! define_command_function {
($name: ident, $command: expr, $args: expr) => {
pub fn $name() -> io::Result<String> {
Ok(String::from(std::str::from_utf8(&Command::new($command)
.args(&$args)
.output()?
.stdout).unwrap()))
}
}
}
define_command_function!(kernel, "uname", ["-msr"]);
define_command_function!(uptime, "uptime", ["-p"]);
define_command_function!(lsb_release, "lsb_release", ["-si"]);