81 lines
2.0 KiB
Rust
81 lines
2.0 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();
|
|
Os::from_str(lsb_release.trim())
|
|
}
|
|
|
|
pub fn from_str(os: &str) -> Option<Os> {
|
|
match os {
|
|
"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"]);
|
|
|