From bf3114d1e0a93abc9cbcc39706bb24e30e5ddb64 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 17 Jul 2019 10:42:40 +0200 Subject: [PATCH] More stuff --- src/lib.rs | 33 +++++++++++++++++++++++++++++++++ src/main.rs | 15 ++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e642991..60c8ec7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,18 @@ +use std::io; +use std::process::Command; + pub mod logos; +#[derive(Copy, Clone, Debug)] pub enum Os { ArchLinux, } impl Os { + pub fn detect() -> Option { + Some(Os::ArchLinux) + } + pub fn ascii_logo(self) -> &'static str { match self { Os::ArchLinux => logos::archlinux::ASCII_LOGO, @@ -16,4 +24,29 @@ impl Os { Os::ArchLinux => "Arch Linux", } } + + pub fn style(self) -> &'static str { + match self { + Os::ArchLinux => "\x1B[36;1m", + } + } + + pub fn unstyle(self) -> &'static str { + "\x1B[0m" + } } + +macro_rules! define_command_function { + ($name: ident, $command: expr, $args: expr) => { + pub fn $name() -> io::Result { + 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"]); + diff --git a/src/main.rs b/src/main.rs index 2bb9eb8..59c8e5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::env::var; use std::fs::File; use std::io::Read; -use tfetch::Os; +use tfetch::{kernel, uptime, Os}; pub fn main() { @@ -10,13 +10,22 @@ pub fn main() { let mut hostname_file = File::open("/etc/hostname").unwrap(); hostname_file.read_to_string(&mut hostname).unwrap(); - let username = format!("\x1B[36;1m{}\x1B[0m@\x1B[36;1m{}\x1B[0m", var("USER").unwrap(), hostname); - let osname = format!("\x1B[36;1mOS:\x1B[0m {}\n", Os::ArchLinux.name()); + let os = Os::detect().unwrap(); + 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 info = [ String::from("\n"), username, osname, + kernel, + uptime, ]; let mut logo_iterator = Os::ArchLinux.ascii_logo().lines();