From 9bd4c9a70aeb1865ab92da897c6303398816f761 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 17 Nov 2025 16:10:31 +0300 Subject: [PATCH] treewide: format with nightly rustfmt rules Signed-off-by: NotAShelf Change-Id: Ib8502372dafe2e970024f606b44825af6a6a6964 --- benches/benchmark.rs | 41 +++++++------ src/colors.rs | 88 +++++++++++++-------------- src/desktop.rs | 45 +++++++------- src/release.rs | 48 ++++++++------- src/system.rs | 139 +++++++++++++++++++++++-------------------- src/uptime.rs | 62 +++++++++---------- 6 files changed, 220 insertions(+), 203 deletions(-) diff --git a/benches/benchmark.rs b/benches/benchmark.rs index 03e49f4..33dc1b9 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -1,26 +1,31 @@ use criterion::{Criterion, criterion_group, criterion_main}; -use microfetch_lib::colors::print_dots; -use microfetch_lib::desktop::get_desktop_info; -use microfetch_lib::release::{get_os_pretty_name, get_system_info}; -use microfetch_lib::system::{ - get_memory_usage, get_root_disk_usage, get_shell, get_username_and_hostname, +use microfetch_lib::{ + colors::print_dots, + desktop::get_desktop_info, + release::{get_os_pretty_name, get_system_info}, + system::{ + get_memory_usage, + get_root_disk_usage, + get_shell, + get_username_and_hostname, + }, + uptime::get_current, }; -use microfetch_lib::uptime::get_current; fn main_benchmark(c: &mut Criterion) { - let utsname = nix::sys::utsname::uname().expect("lol"); - c.bench_function("user_info", |b| { - b.iter(|| get_username_and_hostname(&utsname)); - }); - c.bench_function("os_name", |b| b.iter(get_os_pretty_name)); - c.bench_function("kernel_version", |b| b.iter(|| get_system_info(&utsname))); - c.bench_function("shell", |b| b.iter(get_shell)); + let utsname = nix::sys::utsname::uname().expect("lol"); + c.bench_function("user_info", |b| { + b.iter(|| get_username_and_hostname(&utsname)); + }); + c.bench_function("os_name", |b| b.iter(get_os_pretty_name)); + c.bench_function("kernel_version", |b| b.iter(|| get_system_info(&utsname))); + c.bench_function("shell", |b| b.iter(get_shell)); - c.bench_function("desktop", |b| b.iter(get_desktop_info)); - c.bench_function("uptime", |b| b.iter(get_current)); - c.bench_function("memory_usage", |b| b.iter(get_memory_usage)); - c.bench_function("storage", |b| b.iter(get_root_disk_usage)); - c.bench_function("colors", |b| b.iter(print_dots)); + c.bench_function("desktop", |b| b.iter(get_desktop_info)); + c.bench_function("uptime", |b| b.iter(get_current)); + c.bench_function("memory_usage", |b| b.iter(get_memory_usage)); + c.bench_function("storage", |b| b.iter(get_root_disk_usage)); + c.bench_function("colors", |b| b.iter(print_dots)); } criterion_group!(benches, main_benchmark); diff --git a/src/colors.rs b/src/colors.rs index da613a5..733ec53 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -1,57 +1,57 @@ -use std::env; -use std::sync::LazyLock; +use std::{env, sync::LazyLock}; pub struct Colors { - pub reset: &'static str, - pub blue: &'static str, - pub cyan: &'static str, - pub green: &'static str, - pub yellow: &'static str, - pub red: &'static str, - pub magenta: &'static str, + pub reset: &'static str, + pub blue: &'static str, + pub cyan: &'static str, + pub green: &'static str, + pub yellow: &'static str, + pub red: &'static str, + pub magenta: &'static str, } impl Colors { - const fn new(is_no_color: bool) -> Self { - if is_no_color { - Self { - reset: "", - blue: "", - cyan: "", - green: "", - yellow: "", - red: "", - magenta: "", - } - } else { - Self { - reset: "\x1b[0m", - blue: "\x1b[34m", - cyan: "\x1b[36m", - green: "\x1b[32m", - yellow: "\x1b[33m", - red: "\x1b[31m", - magenta: "\x1b[35m", - } - } + const fn new(is_no_color: bool) -> Self { + if is_no_color { + Self { + reset: "", + blue: "", + cyan: "", + green: "", + yellow: "", + red: "", + magenta: "", + } + } else { + Self { + reset: "\x1b[0m", + blue: "\x1b[34m", + cyan: "\x1b[36m", + green: "\x1b[32m", + yellow: "\x1b[33m", + red: "\x1b[31m", + magenta: "\x1b[35m", + } } + } } pub static COLORS: LazyLock = LazyLock::new(|| { - // check for NO_COLOR once at startup - let is_no_color = env::var("NO_COLOR").is_ok(); - Colors::new(is_no_color) + // check for NO_COLOR once at startup + let is_no_color = env::var("NO_COLOR").is_ok(); + Colors::new(is_no_color) }); +#[must_use] pub fn print_dots() -> String { - format!( - "{} {} {} {} {} {} {}", - COLORS.blue, - COLORS.cyan, - COLORS.green, - COLORS.yellow, - COLORS.red, - COLORS.magenta, - COLORS.reset, - ) + format!( + "{} {} {} {} {} {} {}", + COLORS.blue, + COLORS.cyan, + COLORS.green, + COLORS.yellow, + COLORS.red, + COLORS.magenta, + COLORS.reset, + ) } diff --git a/src/desktop.rs b/src/desktop.rs index f35cd2a..e5e6039 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -1,28 +1,29 @@ +#[must_use] pub fn get_desktop_info() -> String { - // Retrieve the environment variables and handle Result types - let desktop_env = std::env::var("XDG_CURRENT_DESKTOP"); - let display_backend_result = std::env::var("XDG_SESSION_TYPE"); + // Retrieve the environment variables and handle Result types + let desktop_env = std::env::var("XDG_CURRENT_DESKTOP"); + let display_backend_result = std::env::var("XDG_SESSION_TYPE"); - // Capitalize the first letter of the display backend value - let mut display_backend = display_backend_result.unwrap_or_default(); - if let Some(c) = display_backend.as_mut_str().get_mut(0..1) { - c.make_ascii_uppercase(); - } + // Capitalize the first letter of the display backend value + let mut display_backend = display_backend_result.unwrap_or_default(); + if let Some(c) = display_backend.as_mut_str().get_mut(0..1) { + c.make_ascii_uppercase(); + } - // Trim "none+" from the start of desktop_env if present - // 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(), - }; + // Trim "none+" from the start of desktop_env if present + // Use "Unknown" if desktop_env is empty or has an error + let desktop_env = match desktop_env { + Err(_) => "Unknown".to_owned(), + Ok(s) => s.trim_start_matches("none+").to_owned(), + }; - // Handle the case where display_backend might be empty after capitalization - let display_backend = if display_backend.is_empty() { - "Unknown" - } else { - &display_backend - } - .to_string(); + // Handle the case where display_backend might be empty after capitalization + let display_backend = if display_backend.is_empty() { + "Unknown" + } else { + &display_backend + } + .to_owned(); - format!("{desktop_env} ({display_backend})") + format!("{desktop_env} ({display_backend})") } diff --git a/src/release.rs b/src/release.rs index 6d1b20f..20a9b81 100644 --- a/src/release.rs +++ b/src/release.rs @@ -1,33 +1,35 @@ -use nix::sys::utsname::UtsName; use std::{ - fs::File, - io::{self, BufRead, BufReader}, + fs::File, + io::{self, BufRead, BufReader}, }; +use nix::sys::utsname::UtsName; + +#[must_use] pub fn get_system_info(utsname: &UtsName) -> String { - format!( - "{} {} ({})", - utsname.sysname().to_str().unwrap_or("Unknown"), - utsname.release().to_str().unwrap_or("Unknown"), - utsname.machine().to_str().unwrap_or("Unknown") - ) + format!( + "{} {} ({})", + utsname.sysname().to_str().unwrap_or("Unknown"), + utsname.release().to_str().unwrap_or("Unknown"), + utsname.machine().to_str().unwrap_or("Unknown") + ) } pub fn get_os_pretty_name() -> Result { - let file = File::open("/etc/os-release")?; - let reader = BufReader::new(file); + let file = File::open("/etc/os-release")?; + let reader = BufReader::new(file); - for line in reader.lines() { - let line = line?; - if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") { - if let Some(trimmed) = pretty_name - .strip_prefix('"') - .and_then(|s| s.strip_suffix('"')) - { - return Ok(trimmed.to_string()); - } - return Ok(pretty_name.to_string()); - } + for line in reader.lines() { + let line = line?; + if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") { + if let Some(trimmed) = pretty_name + .strip_prefix('"') + .and_then(|s| s.strip_suffix('"')) + { + return Ok(trimmed.to_owned()); + } + return Ok(pretty_name.to_owned()); } - Ok("Unknown".to_string()) + } + Ok("Unknown".to_owned()) } diff --git a/src/system.rs b/src/system.rs index aed8752..1cfd12c 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1,87 +1,96 @@ -use crate::colors::COLORS; -use nix::sys::{statvfs::statvfs, utsname::UtsName}; use std::{ - env, - fs::File, - io::{self, Read}, + env, + fs::File, + io::{self, Read}, }; +use nix::sys::{statvfs::statvfs, utsname::UtsName}; + +use crate::colors::COLORS; + +#[must_use] pub fn get_username_and_hostname(utsname: &UtsName) -> String { - let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_string()); - let hostname = utsname - .nodename() - .to_str() - .unwrap_or("unknown_host") - .to_string(); - format!( - "{yellow}{username}{red}@{green}{hostname}{reset}", - yellow = COLORS.yellow, - red = COLORS.red, - green = COLORS.green, - reset = COLORS.reset, - ) + let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_owned()); + let hostname = utsname + .nodename() + .to_str() + .unwrap_or("unknown_host") + .to_owned(); + format!( + "{yellow}{username}{red}@{green}{hostname}{reset}", + yellow = COLORS.yellow, + red = COLORS.red, + green = COLORS.green, + reset = COLORS.reset, + ) } +#[must_use] pub fn get_shell() -> String { - let shell_path = env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_string()); - let shell_name = shell_path.rsplit('/').next().unwrap_or("unknown_shell"); - shell_name.to_string() + let shell_path = + env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_owned()); + let shell_name = shell_path.rsplit('/').next().unwrap_or("unknown_shell"); + shell_name.to_owned() } pub fn get_root_disk_usage() -> Result { - let vfs = statvfs("/")?; - let block_size = vfs.block_size() as u64; - let total_blocks = vfs.blocks(); - let available_blocks = vfs.blocks_available(); + let vfs = statvfs("/")?; + let block_size = vfs.block_size() as u64; + let total_blocks = vfs.blocks(); + let available_blocks = vfs.blocks_available(); - let total_size = block_size * total_blocks; - let used_size = total_size - (block_size * available_blocks); + let total_size = block_size * total_blocks; + let used_size = total_size - (block_size * available_blocks); - let total_size = total_size as f64 / (1024.0 * 1024.0 * 1024.0); - let used_size = used_size as f64 / (1024.0 * 1024.0 * 1024.0); - let usage = (used_size / total_size) * 100.0; + let total_size = total_size as f64 / (1024.0 * 1024.0 * 1024.0); + let used_size = used_size as f64 / (1024.0 * 1024.0 * 1024.0); + let usage = (used_size / total_size) * 100.0; - Ok(format!( - "{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})", - cyan = COLORS.cyan, - reset = COLORS.reset, - )) + Ok(format!( + "{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})", + cyan = COLORS.cyan, + reset = COLORS.reset, + )) } pub fn get_memory_usage() -> Result { - fn parse_memory_info() -> Result<(f64, f64), io::Error> { - let mut total_memory_kb = 0.0; - let mut available_memory_kb = 0.0; - let mut meminfo = String::with_capacity(2048); + fn parse_memory_info() -> Result<(f64, f64), io::Error> { + let mut total_memory_kb = 0.0; + let mut available_memory_kb = 0.0; + let mut meminfo = String::with_capacity(2048); - File::open("/proc/meminfo")?.read_to_string(&mut meminfo)?; + File::open("/proc/meminfo")?.read_to_string(&mut meminfo)?; - for line in meminfo.lines() { - let mut split = line.split_whitespace(); - match split.next().unwrap_or_default() { - "MemTotal:" => total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0), - "MemAvailable:" => { - available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0); - // MemTotal comes before MemAvailable, stop parsing - break; - } - _ => (), - } - } - - let total_memory_gb = total_memory_kb / 1024.0 / 1024.0; - let available_memory_gb = available_memory_kb / 1024.0 / 1024.0; - let used_memory_gb = total_memory_gb - available_memory_gb; - - Ok((used_memory_gb, total_memory_gb)) + for line in meminfo.lines() { + let mut split = line.split_whitespace(); + match split.next().unwrap_or_default() { + "MemTotal:" => { + total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0) + }, + "MemAvailable:" => { + available_memory_kb = + split.next().unwrap_or("0").parse().unwrap_or(0.0); + // MemTotal comes before MemAvailable, stop parsing + break; + }, + _ => (), + } } - let (used_memory, total_memory) = parse_memory_info()?; - let percentage_used = (used_memory / total_memory * 100.0).round() as u64; + let total_memory_gb = total_memory_kb / 1024.0 / 1024.0; + let available_memory_gb = available_memory_kb / 1024.0 / 1024.0; + let used_memory_gb = total_memory_gb - available_memory_gb; - Ok(format!( - "{used_memory:.2} GiB / {total_memory:.2} GiB ({cyan}{percentage_used}%{reset})", - cyan = COLORS.cyan, - reset = COLORS.reset, - )) + Ok((used_memory_gb, total_memory_gb)) + } + + let (used_memory, total_memory) = parse_memory_info()?; + let percentage_used = (used_memory / total_memory * 100.0).round() as u64; + + Ok(format!( + "{used_memory:.2} GiB / {total_memory:.2} GiB \ + ({cyan}{percentage_used}%{reset})", + cyan = COLORS.cyan, + reset = COLORS.reset, + )) } diff --git a/src/uptime.rs b/src/uptime.rs index 496052a..87b95cf 100644 --- a/src/uptime.rs +++ b/src/uptime.rs @@ -1,40 +1,40 @@ use std::{io, mem::MaybeUninit}; pub fn get_current() -> Result { - let uptime_seconds = { - let mut info = MaybeUninit::uninit(); - if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 { - return Err(io::Error::last_os_error()); - } - unsafe { info.assume_init().uptime as u64 } - }; + let uptime_seconds = { + let mut info = MaybeUninit::uninit(); + if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 { + return Err(io::Error::last_os_error()); + } + unsafe { info.assume_init().uptime as u64 } + }; - let days = uptime_seconds / 86400; - let hours = (uptime_seconds / 3600) % 24; - let minutes = (uptime_seconds / 60) % 60; + let days = uptime_seconds / 86400; + let hours = (uptime_seconds / 3600) % 24; + let minutes = (uptime_seconds / 60) % 60; - let mut result = String::with_capacity(32); - if days > 0 { - result.push_str(&days.to_string()); - result.push_str(if days == 1 { " day" } else { " days" }); + let mut result = String::with_capacity(32); + if days > 0 { + result.push_str(&days.to_string()); + result.push_str(if days == 1 { " day" } else { " days" }); + } + if hours > 0 { + if !result.is_empty() { + result.push_str(", "); } - if hours > 0 { - if !result.is_empty() { - result.push_str(", "); - } - result.push_str(&hours.to_string()); - result.push_str(if hours == 1 { " hour" } else { " hours" }); - } - if minutes > 0 { - if !result.is_empty() { - result.push_str(", "); - } - result.push_str(&minutes.to_string()); - result.push_str(if minutes == 1 { " minute" } else { " minutes" }); - } - if result.is_empty() { - result.push_str("less than a minute"); + result.push_str(&hours.to_string()); + result.push_str(if hours == 1 { " hour" } else { " hours" }); + } + if minutes > 0 { + if !result.is_empty() { + result.push_str(", "); } + result.push_str(&minutes.to_string()); + result.push_str(if minutes == 1 { " minute" } else { " minutes" }); + } + if result.is_empty() { + result.push_str("less than a minute"); + } - Ok(result) + Ok(result) }