mirror of
https://github.com/NotAShelf/microfetch.git
synced 2025-11-26 01:02:50 +00:00
various: reduce allocations where available
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I517d855b14c015569a325deb64948f3b6a6a6964
This commit is contained in:
parent
325ec69024
commit
2ad765ef98
5 changed files with 125 additions and 65 deletions
|
|
@ -37,7 +37,7 @@ impl Colors {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static COLORS: LazyLock<Colors> = LazyLock::new(|| {
|
pub static COLORS: LazyLock<Colors> = LazyLock::new(|| {
|
||||||
// check for NO_COLOR once at startup
|
// Check for NO_COLOR once at startup
|
||||||
let is_no_color = env::var("NO_COLOR").is_ok();
|
let is_no_color = env::var("NO_COLOR").is_ok();
|
||||||
Colors::new(is_no_color)
|
Colors::new(is_no_color)
|
||||||
});
|
});
|
||||||
|
|
@ -45,14 +45,37 @@ pub static COLORS: LazyLock<Colors> = LazyLock::new(|| {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
pub fn print_dots() -> String {
|
pub fn print_dots() -> String {
|
||||||
format!(
|
// Pre-calculate capacity: 6 color codes + " " (glyph + 2 spaces) per color
|
||||||
"{} {} {} {} {} {} {}",
|
const GLYPH: &str = "";
|
||||||
COLORS.blue,
|
let capacity = COLORS.blue.len()
|
||||||
COLORS.cyan,
|
+ COLORS.cyan.len()
|
||||||
COLORS.green,
|
+ COLORS.green.len()
|
||||||
COLORS.yellow,
|
+ COLORS.yellow.len()
|
||||||
COLORS.red,
|
+ COLORS.red.len()
|
||||||
COLORS.magenta,
|
+ COLORS.magenta.len()
|
||||||
COLORS.reset,
|
+ COLORS.reset.len()
|
||||||
)
|
+ (GLYPH.len() + 2) * 6;
|
||||||
|
|
||||||
|
let mut result = String::with_capacity(capacity);
|
||||||
|
result.push_str(COLORS.blue);
|
||||||
|
result.push_str(GLYPH);
|
||||||
|
result.push_str(" ");
|
||||||
|
result.push_str(COLORS.cyan);
|
||||||
|
result.push_str(GLYPH);
|
||||||
|
result.push_str(" ");
|
||||||
|
result.push_str(COLORS.green);
|
||||||
|
result.push_str(GLYPH);
|
||||||
|
result.push_str(" ");
|
||||||
|
result.push_str(COLORS.yellow);
|
||||||
|
result.push_str(GLYPH);
|
||||||
|
result.push_str(" ");
|
||||||
|
result.push_str(COLORS.red);
|
||||||
|
result.push_str(GLYPH);
|
||||||
|
result.push_str(" ");
|
||||||
|
result.push_str(COLORS.magenta);
|
||||||
|
result.push_str(GLYPH);
|
||||||
|
result.push_str(" ");
|
||||||
|
result.push_str(COLORS.reset);
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,37 @@
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
pub fn get_desktop_info() -> String {
|
pub fn get_desktop_info() -> String {
|
||||||
// Retrieve the environment variables and handle Result types
|
// Retrieve the environment variables and handle Result types
|
||||||
let desktop_env = std::env::var("XDG_CURRENT_DESKTOP");
|
let desktop_env = std::env::var("XDG_CURRENT_DESKTOP");
|
||||||
let display_backend_result = std::env::var("XDG_SESSION_TYPE");
|
let display_backend = std::env::var("XDG_SESSION_TYPE");
|
||||||
|
|
||||||
// Capitalize the first letter of the display backend value
|
let desktop_str = match desktop_env {
|
||||||
let mut display_backend = display_backend_result.unwrap_or_default();
|
Err(_) => "Unknown",
|
||||||
if let Some(c) = display_backend.as_mut_str().get_mut(0..1) {
|
Ok(ref s) if s.starts_with("none+") => &s[5..],
|
||||||
c.make_ascii_uppercase();
|
Ok(ref s) => s.as_str(),
|
||||||
}
|
|
||||||
|
|
||||||
// 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 backend_str = match display_backend {
|
||||||
let display_backend = if display_backend.is_empty() {
|
Err(_) => "Unknown",
|
||||||
"Unknown"
|
Ok(ref s) if s.is_empty() => "Unknown",
|
||||||
} else {
|
Ok(ref s) => s.as_str(),
|
||||||
&display_backend
|
};
|
||||||
}
|
|
||||||
.to_owned();
|
|
||||||
|
|
||||||
format!("{desktop_env} ({display_backend})")
|
// Pre-calculate capacity: desktop_len + " (" + backend_len + ")"
|
||||||
|
// Capitalize first char needs temporary allocation only if backend exists
|
||||||
|
let mut result =
|
||||||
|
String::with_capacity(desktop_str.len() + backend_str.len() + 3);
|
||||||
|
result.push_str(desktop_str);
|
||||||
|
result.push_str(" (");
|
||||||
|
|
||||||
|
// Capitalize first character of backend
|
||||||
|
if let Some(first_char) = backend_str.chars().next() {
|
||||||
|
let _ = write!(result, "{}", first_char.to_ascii_uppercase());
|
||||||
|
result.push_str(&backend_str[first_char.len_utf8()..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push(')');
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
|
fmt::Write as _,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, BufRead, BufReader},
|
io::{self, Read},
|
||||||
};
|
};
|
||||||
|
|
||||||
use nix::sys::utsname::UtsName;
|
use nix::sys::utsname::UtsName;
|
||||||
|
|
@ -8,21 +9,26 @@ use nix::sys::utsname::UtsName;
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
pub fn get_system_info(utsname: &UtsName) -> String {
|
pub fn get_system_info(utsname: &UtsName) -> String {
|
||||||
format!(
|
let sysname = utsname.sysname().to_str().unwrap_or("Unknown");
|
||||||
"{} {} ({})",
|
let release = utsname.release().to_str().unwrap_or("Unknown");
|
||||||
utsname.sysname().to_str().unwrap_or("Unknown"),
|
let machine = utsname.machine().to_str().unwrap_or("Unknown");
|
||||||
utsname.release().to_str().unwrap_or("Unknown"),
|
|
||||||
utsname.machine().to_str().unwrap_or("Unknown")
|
// Pre-allocate capacity: sysname + " " + release + " (" + machine + ")"
|
||||||
)
|
let capacity = sysname.len() + 1 + release.len() + 2 + machine.len() + 1;
|
||||||
|
let mut result = String::with_capacity(capacity);
|
||||||
|
|
||||||
|
write!(result, "{sysname} {release} ({machine})").unwrap();
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
pub fn get_os_pretty_name() -> Result<String, io::Error> {
|
pub fn get_os_pretty_name() -> Result<String, io::Error> {
|
||||||
let file = File::open("/etc/os-release")?;
|
// We use a stack-allocated buffer here, which seems to perform MUCH better
|
||||||
let reader = BufReader::new(file);
|
// than `BufReader`. In hindsight, I should've seen this coming.
|
||||||
|
let mut buffer = String::with_capacity(1024);
|
||||||
|
File::open("/etc/os-release")?.read_to_string(&mut buffer)?;
|
||||||
|
|
||||||
for line in reader.lines() {
|
for line in buffer.lines() {
|
||||||
let line = line?;
|
|
||||||
if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") {
|
if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") {
|
||||||
if let Some(trimmed) = pretty_name
|
if let Some(trimmed) = pretty_name
|
||||||
.strip_prefix('"')
|
.strip_prefix('"')
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
env,
|
||||||
|
fmt::Write as _,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, Read},
|
io::{self, Read},
|
||||||
};
|
};
|
||||||
|
|
@ -12,18 +13,26 @@ use crate::colors::COLORS;
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
pub fn get_username_and_hostname(utsname: &UtsName) -> String {
|
pub fn get_username_and_hostname(utsname: &UtsName) -> String {
|
||||||
let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_owned());
|
let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_owned());
|
||||||
let hostname = utsname
|
let hostname = utsname.nodename().to_str().unwrap_or("unknown_host");
|
||||||
.nodename()
|
|
||||||
.to_str()
|
let capacity = COLORS.yellow.len()
|
||||||
.unwrap_or("unknown_host")
|
+ username.len()
|
||||||
.to_owned();
|
+ COLORS.red.len()
|
||||||
format!(
|
+ 1
|
||||||
"{yellow}{username}{red}@{green}{hostname}{reset}",
|
+ COLORS.green.len()
|
||||||
yellow = COLORS.yellow,
|
+ hostname.len()
|
||||||
red = COLORS.red,
|
+ COLORS.reset.len();
|
||||||
green = COLORS.green,
|
let mut result = String::with_capacity(capacity);
|
||||||
reset = COLORS.reset,
|
|
||||||
)
|
result.push_str(COLORS.yellow);
|
||||||
|
result.push_str(&username);
|
||||||
|
result.push_str(COLORS.red);
|
||||||
|
result.push('@');
|
||||||
|
result.push_str(COLORS.green);
|
||||||
|
result.push_str(hostname);
|
||||||
|
result.push_str(COLORS.reset);
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
|
@ -31,8 +40,13 @@ pub fn get_username_and_hostname(utsname: &UtsName) -> String {
|
||||||
pub fn get_shell() -> String {
|
pub fn get_shell() -> String {
|
||||||
let shell_path =
|
let shell_path =
|
||||||
env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_owned());
|
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()
|
// Find last '/' and get the part after it, avoiding allocation
|
||||||
|
shell_path
|
||||||
|
.rsplit('/')
|
||||||
|
.next()
|
||||||
|
.unwrap_or("unknown_shell")
|
||||||
|
.to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
|
|
@ -49,11 +63,16 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
|
||||||
let used_size = used_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 usage = (used_size / total_size) * 100.0;
|
||||||
|
|
||||||
Ok(format!(
|
let mut result = String::with_capacity(64);
|
||||||
|
write!(
|
||||||
|
result,
|
||||||
"{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})",
|
"{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})",
|
||||||
cyan = COLORS.cyan,
|
cyan = COLORS.cyan,
|
||||||
reset = COLORS.reset,
|
reset = COLORS.reset,
|
||||||
))
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
|
|
@ -70,7 +89,7 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
|
||||||
let mut split = line.split_whitespace();
|
let mut split = line.split_whitespace();
|
||||||
match split.next().unwrap_or_default() {
|
match split.next().unwrap_or_default() {
|
||||||
"MemTotal:" => {
|
"MemTotal:" => {
|
||||||
total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0)
|
total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0);
|
||||||
},
|
},
|
||||||
"MemAvailable:" => {
|
"MemAvailable:" => {
|
||||||
available_memory_kb =
|
available_memory_kb =
|
||||||
|
|
@ -92,10 +111,15 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
|
||||||
let (used_memory, total_memory) = parse_memory_info()?;
|
let (used_memory, total_memory) = parse_memory_info()?;
|
||||||
let percentage_used = (used_memory / total_memory * 100.0).round() as u64;
|
let percentage_used = (used_memory / total_memory * 100.0).round() as u64;
|
||||||
|
|
||||||
Ok(format!(
|
let mut result = String::with_capacity(64);
|
||||||
|
write!(
|
||||||
|
result,
|
||||||
"{used_memory:.2} GiB / {total_memory:.2} GiB \
|
"{used_memory:.2} GiB / {total_memory:.2} GiB \
|
||||||
({cyan}{percentage_used}%{reset})",
|
({cyan}{percentage_used}%{reset})",
|
||||||
cyan = COLORS.cyan,
|
cyan = COLORS.cyan,
|
||||||
reset = COLORS.reset,
|
reset = COLORS.reset,
|
||||||
))
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{io, mem::MaybeUninit};
|
use std::{fmt::Write, io, mem::MaybeUninit};
|
||||||
|
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
pub fn get_current() -> Result<String, io::Error> {
|
pub fn get_current() -> Result<String, io::Error> {
|
||||||
|
|
@ -16,21 +16,21 @@ pub fn get_current() -> Result<String, io::Error> {
|
||||||
|
|
||||||
let mut result = String::with_capacity(32);
|
let mut result = String::with_capacity(32);
|
||||||
if days > 0 {
|
if days > 0 {
|
||||||
result.push_str(&days.to_string());
|
let _ = write!(result, "{days}");
|
||||||
result.push_str(if days == 1 { " day" } else { " days" });
|
result.push_str(if days == 1 { " day" } else { " days" });
|
||||||
}
|
}
|
||||||
if hours > 0 {
|
if hours > 0 {
|
||||||
if !result.is_empty() {
|
if !result.is_empty() {
|
||||||
result.push_str(", ");
|
result.push_str(", ");
|
||||||
}
|
}
|
||||||
result.push_str(&hours.to_string());
|
let _ = write!(result, "{hours}");
|
||||||
result.push_str(if hours == 1 { " hour" } else { " hours" });
|
result.push_str(if hours == 1 { " hour" } else { " hours" });
|
||||||
}
|
}
|
||||||
if minutes > 0 {
|
if minutes > 0 {
|
||||||
if !result.is_empty() {
|
if !result.is_empty() {
|
||||||
result.push_str(", ");
|
result.push_str(", ");
|
||||||
}
|
}
|
||||||
result.push_str(&minutes.to_string());
|
let _ = write!(result, "{minutes}");
|
||||||
result.push_str(if minutes == 1 { " minute" } else { " minutes" });
|
result.push_str(if minutes == 1 { " minute" } else { " minutes" });
|
||||||
}
|
}
|
||||||
if result.is_empty() {
|
if result.is_empty() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue