From ae76a0cbe1c8f234c214e33a249961dccff8d576 Mon Sep 17 00:00:00 2001 From: Sora Date: Tue, 13 Aug 2024 22:59:11 +0200 Subject: [PATCH 1/6] refactor: remove some redundant error types (#4) --- src/colors.rs | 6 ++---- src/desktop.rs | 19 ++++++++----------- src/main.rs | 33 +++++++++++++++++---------------- src/release.rs | 2 +- src/system.rs | 14 +++++++------- 5 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/colors.rs b/src/colors.rs index 1aa8096..a8aee60 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -6,8 +6,6 @@ pub const YELLOW: &str = "\x1b[33m"; pub const RED: &str = "\x1b[31m"; pub const MAGENTA: &str = "\x1b[35m"; -pub fn print_dots() -> Result { - let colors = format!("{BLUE} {CYAN} {GREEN} {YELLOW} {RED} {MAGENTA} {RESET}"); - - Ok(colors) +pub fn print_dots() -> String { + format!("{BLUE} {CYAN} {GREEN} {YELLOW} {RED} {MAGENTA} {RESET}") } diff --git a/src/desktop.rs b/src/desktop.rs index 9d72325..bbaa156 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -1,20 +1,17 @@ -use color_eyre::Result; -use std::{env, io}; - -pub fn get_desktop_info() -> Result { - let desktop_env = env::var("XDG_CURRENT_DESKTOP"); - let display_backend = env::var("XDG_SESSION_TYPE"); +pub fn get_desktop_info() -> String { + let desktop_env = std::env::var("XDG_CURRENT_DESKTOP"); + let display_backend = std::env::var("XDG_SESSION_TYPE"); // Trim "none+" from the start of desktop_env if present // XXX: This is a workaround for NixOS modules that set XDG_CURRENT_DESKTOP to "none+foo" // instead of just "foo" // Use "Unknown" if desktop_env or display_backend is empty - let desktop_env = match desktop_env { - Err(_) => String::from("Unknown"), - Ok(s) => s.trim_start_matches("none+").to_owned(), + let desktop_env = match desktop_env.as_ref() { + Err(_) => "Unknown", + Ok(s) => s.trim_start_matches("none+"), }; - let display_backend = display_backend.unwrap_or_else(|_| String::from("Unknown")); + let display_backend = display_backend.unwrap_or(String::from("Unknown")); - Ok(format!("{desktop_env} ({display_backend})")) + format!("{desktop_env} ({display_backend})") } diff --git a/src/main.rs b/src/main.rs index f725d29..ca51782 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,15 +17,15 @@ fn main() -> Result<(), Report> { color_eyre::install()?; let fields = Fields { - user_info: get_username_and_hostname()?, + user_info: get_username_and_hostname(), os_name: get_os_pretty_name()?, kernel_version: get_system_info()?, - shell: get_shell()?, + shell: get_shell(), uptime: get_current()?, - window_manager: get_desktop_info()?, + window_manager: get_desktop_info(), memory_usage: get_memory_usage(sysinfo()?), storage: get_root_disk_usage()?, - colors: print_dots()?, + colors: print_dots(), }; print_system_info(&fields); @@ -50,6 +50,18 @@ struct Fields { } fn print_system_info(fields: &Fields) { + let Fields { + user_info, + os_name, + kernel_version, + shell, + uptime, + window_manager, + memory_usage, + storage, + colors, + } = fields; + println!( " {CYAN} ▟█▖ {BLUE}▝█▙ ▗█▛ {user_info} ~{RESET} @@ -60,16 +72,5 @@ fn print_system_info(fields: &Fields) { {BLUE} ▟█▛{CYAN}▗█▖ {CYAN}▟█▛ {CYAN} {BLUE}WM{RESET}  {window_manager} {BLUE} ▝█▛ {CYAN}██▖{BLUE}▗▄▄▄▄▄▄▄▄▄▄▄ {CYAN}󰍛 {BLUE}Memory{RESET}  {memory_usage} {BLUE} ▝ {CYAN}▟█▜█▖{BLUE}▀▀▀▀▀██▛▀▀▘ {CYAN}󱥎 {BLUE}Storage (/){RESET}  {storage} - {CYAN} ▟█▘ ▜█▖ {BLUE}▝█▛ {CYAN} {BLUE}Colors{RESET}  {colors} -", - user_info = fields.user_info, - os_name = fields.os_name, - kernel_version = fields.kernel_version, - shell = fields.shell, - uptime = fields.uptime, - window_manager = fields.window_manager, - memory_usage = fields.memory_usage, - storage = fields.storage, - colors = fields.colors, - ); + {CYAN} ▟█▘ ▜█▖ {BLUE}▝█▛ {CYAN} {BLUE}Colors{RESET}  {colors}"); } diff --git a/src/release.rs b/src/release.rs index 8a33844..4c6a9b8 100644 --- a/src/release.rs +++ b/src/release.rs @@ -23,5 +23,5 @@ pub fn get_os_pretty_name() -> Result { .to_string() }); - Ok(pretty_name.unwrap_or_else(|| "Unknown".to_string())) + Ok(pretty_name.unwrap_or("Unknown".to_string())) } diff --git a/src/system.rs b/src/system.rs index 551a833..ab10cd5 100644 --- a/src/system.rs +++ b/src/system.rs @@ -7,19 +7,19 @@ use std::io::{self}; use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW}; -pub fn get_username_and_hostname() -> Result { - let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_string()); - let hostname = nix::unistd::gethostname()?; +pub fn get_username_and_hostname() -> String { + let username = env::var("USER").unwrap_or("unknown_user".to_string()); + let hostname = nix::unistd::gethostname().unwrap_or("unknown_host".to_string().into()); let hostname = hostname.to_string_lossy(); - Ok(format!("{YELLOW}{username}{RED}@{GREEN}{hostname}")) + format!("{YELLOW}{username}{RED}@{GREEN}{hostname}") } -pub fn get_shell() -> Result { - let shell_path = env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_string()); +pub fn get_shell() -> String { + let shell_path = env::var("SHELL").unwrap_or("unknown_shell".to_string()); let shell_name = shell_path.rsplit('/').next().unwrap_or("unknown_shell"); - Ok(shell_name.to_string()) + shell_name.to_string() } pub fn get_root_disk_usage() -> Result { From e90de78b612a7b4a082d2f1477e65c3f3be6e96a Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 14 Aug 2024 00:00:11 +0300 Subject: [PATCH 2/6] increment cargo version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6251ee2..db76bd3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,7 +105,7 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "microfetch" -version = "0.3.3" +version = "0.3.4" dependencies = [ "color-eyre", "nix", diff --git a/Cargo.toml b/Cargo.toml index b551c5d..7b0bc8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "microfetch" -version = "0.3.3" +version = "0.3.4" edition = "2021" [dependencies] From 58c6d9b384b589b8df799bdbaa90f086b8e9f20e Mon Sep 17 00:00:00 2001 From: sioodmy Date: Tue, 13 Aug 2024 21:14:38 +0000 Subject: [PATCH 3/6] docs: update readme (#5) --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index decae73..b6e9d8f 100644 --- a/README.md +++ b/README.md @@ -47,12 +47,12 @@ Microfetch's performance is mostly hardware-dependant, however, the overall trend seems to be < 2ms on any modern (2015 and after) CPU. Below are the benchmarks with Hyperfine on my desktop system. -| Command | Mean [ms] | Min [ms] | Max [ms] | Relative | -| :----------- | ----------: | -------: | -------: | -------------: | -| `microfetch` | 1.3 ± 0.0 | 1.3 | 1.4 | 1.00 | -| `pfetch` | 254.2 ± 4.8 | 246.7 | 264.9 | 191.97 ± 7.10 | -| `neofetch` | 735.4 ± 9.5 | 721.1 | 752.8 | 555.48 ± 19.08 | -| `fastfetch` | 31.9 ± 0.8 | 30.8 | 33.8 | 24.08 ± 0.98 | +| Command | Mean [ms] | Min [ms] | Max [ms] | Relative | Written by raf? | +| :----------- | ----------: | -------: | -------: | -------------: | ----------------: +| `microfetch` | 1.3 ± 0.0 | 1.3 | 1.4 | 1.00 | yes | +| `pfetch` | 254.2 ± 4.8 | 246.7 | 264.9 | 191.97 ± 7.10 | no | +| `neofetch` | 735.4 ± 9.5 | 721.1 | 752.8 | 555.48 ± 19.08 | no | +| `fastfetch` | 31.9 ± 0.8 | 30.8 | 33.8 | 24.08 ± 0.98 | no | _As far as I'm concerned, Microfetch is faster than almost every fetch tool there is. The only downside of using Rust is introducing more "bloated" From 6cad15c3304fd082010888462e5170bad1f81f6a Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 14 Aug 2024 00:19:40 +0300 Subject: [PATCH 4/6] include installation instructions --- README.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b6e9d8f..f27384d 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,23 @@ than welcome to use it on your system: it's pretty [fast...](#benchmarks) - Version - Architecture - Current shell (from $SHELL, trimmed if store path) - - WM/Compositor and display backend + - Current Desktop (DE/WM/Compositor and display backend) - Memory Usage/Total Memory - Storage Usage/Total Storage (for `/` only) - Shell Colors - Did I mention fast? +## Installation + +Microfetch is packaged in [nixpkgs](https://github.com/nixos/nixpkgs). You can +get it through the unstable channel for the time being. The Nix flake can also +be used for bleeding-edge builds. + +Non-Nix users will have to build Microfetch with `cargo`. + +Microfetch is _currently_ not available anywhere else. Though, does it _really_ +have to be? + ## Benchmarks Microfetch's performance is mostly hardware-dependant, however, the overall @@ -48,7 +59,7 @@ trend seems to be < 2ms on any modern (2015 and after) CPU. Below are the benchmarks with Hyperfine on my desktop system. | Command | Mean [ms] | Min [ms] | Max [ms] | Relative | Written by raf? | -| :----------- | ----------: | -------: | -------: | -------------: | ----------------: +| :----------- | ----------: | -------: | -------: | -------------: | --------------: | | `microfetch` | 1.3 ± 0.0 | 1.3 | 1.4 | 1.00 | yes | | `pfetch` | 254.2 ± 4.8 | 246.7 | 264.9 | 191.97 ± 7.10 | no | | `neofetch` | 735.4 ± 9.5 | 721.1 | 752.8 | 555.48 ± 19.08 | no | From b792d41fcc2b934538c97964c7917507b1f65222 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 14 Aug 2024 00:20:03 +0300 Subject: [PATCH 5/6] capitalize first letter of session type wayland -> Wayland, x11 -> X11 --- src/desktop.rs | 34 ++++++++++++++++++++++++++-------- src/main.rs | 8 ++++---- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/desktop.rs b/src/desktop.rs index bbaa156..bf0da35 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -1,17 +1,35 @@ pub fn get_desktop_info() -> String { + fn capitalize_first_letter(s: &str) -> String { + if s.is_empty() { + return String::new(); + } + + let mut chars = s.chars(); + let first_char = chars.next().unwrap().to_uppercase().to_string(); + let rest: String = chars.collect(); + first_char + &rest + } + + // Retrieve the environment variables and handle Result types let desktop_env = std::env::var("XDG_CURRENT_DESKTOP"); - let display_backend = std::env::var("XDG_SESSION_TYPE"); + let display_backend_result = std::env::var("XDG_SESSION_TYPE"); + + // Capitalize the first letter of the display backend value + let display_backend = capitalize_first_letter(display_backend_result.as_deref().unwrap_or("")); // Trim "none+" from the start of desktop_env if present - // XXX: This is a workaround for NixOS modules that set XDG_CURRENT_DESKTOP to "none+foo" - // instead of just "foo" - // Use "Unknown" if desktop_env or display_backend is empty - let desktop_env = match desktop_env.as_ref() { - Err(_) => "Unknown", - Ok(s) => s.trim_start_matches("none+"), + // Use "Unknown" if desktop_env is empty or has an error + let desktop_env = match desktop_env { + Err(_) => "Unknown".to_string(), + Ok(s) => s.trim_start_matches("none+").to_string(), }; - let display_backend = display_backend.unwrap_or(String::from("Unknown")); + // Handle the case where display_backend might be empty after capitalization + let display_backend = if display_backend.is_empty() { + "Unknown".to_string() + } else { + display_backend + }; format!("{desktop_env} ({display_backend})") } diff --git a/src/main.rs b/src/main.rs index ca51782..950a766 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ fn main() -> Result<(), Report> { kernel_version: get_system_info()?, shell: get_shell(), uptime: get_current()?, - window_manager: get_desktop_info(), + desktop: get_desktop_info(), memory_usage: get_memory_usage(sysinfo()?), storage: get_root_disk_usage()?, colors: print_dots(), @@ -43,7 +43,7 @@ struct Fields { kernel_version: String, shell: String, uptime: String, - window_manager: String, + desktop: String, memory_usage: String, storage: String, colors: String, @@ -56,7 +56,7 @@ fn print_system_info(fields: &Fields) { kernel_version, shell, uptime, - window_manager, + desktop, memory_usage, storage, colors, @@ -69,7 +69,7 @@ fn print_system_info(fields: &Fields) { {CYAN} ▀▀▀▀▀▀▀▀▀▀▀▘{BLUE}▝██ {CYAN}▟█▖ {CYAN} {BLUE}Kernel{RESET}  {kernel_version} {BLUE} ▟█▛ {BLUE}▝█▘{CYAN}▟█▛ {CYAN} {BLUE}Shell{RESET}  {shell} {BLUE}▟█████▛ {CYAN}▟█████▛ {CYAN} {BLUE}Uptime{RESET}  {uptime} - {BLUE} ▟█▛{CYAN}▗█▖ {CYAN}▟█▛ {CYAN} {BLUE}WM{RESET}  {window_manager} + {BLUE} ▟█▛{CYAN}▗█▖ {CYAN}▟█▛ {CYAN} {BLUE}Desktop{RESET}  {desktop} {BLUE} ▝█▛ {CYAN}██▖{BLUE}▗▄▄▄▄▄▄▄▄▄▄▄ {CYAN}󰍛 {BLUE}Memory{RESET}  {memory_usage} {BLUE} ▝ {CYAN}▟█▜█▖{BLUE}▀▀▀▀▀██▛▀▀▘ {CYAN}󱥎 {BLUE}Storage (/){RESET}  {storage} {CYAN} ▟█▘ ▜█▖ {BLUE}▝█▛ {CYAN} {BLUE}Colors{RESET}  {colors}"); From cd8ddc2177775c722a88c14e853cbe5bbb7f6495 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 14 Aug 2024 00:23:08 +0300 Subject: [PATCH 6/6] increment cargo version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db76bd3..1f39265 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,7 +105,7 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "microfetch" -version = "0.3.4" +version = "0.3.5" dependencies = [ "color-eyre", "nix", diff --git a/Cargo.toml b/Cargo.toml index 7b0bc8b..347b0bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "microfetch" -version = "0.3.4" +version = "0.3.5" edition = "2021" [dependencies]