mirror of
https://github.com/NotAShelf/microfetch.git
synced 2025-12-14 08:21:01 +00:00
perf: use libc to fetch env vars
This commit is contained in:
parent
8c32f5f408
commit
8376e9d323
3 changed files with 41 additions and 27 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{env, sync::LazyLock};
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
pub struct Colors {
|
pub struct Colors {
|
||||||
pub reset: &'static str,
|
pub reset: &'static str,
|
||||||
|
|
@ -37,8 +37,8 @@ impl Colors {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static COLORS: LazyLock<Colors> = LazyLock::new(|| {
|
pub static COLORS: LazyLock<Colors> = LazyLock::new(|| {
|
||||||
// Check for NO_COLOR once at startup
|
const NO_COLOR: *const libc::c_char = c"NO_COLOR".as_ptr();
|
||||||
let is_no_color = env::var("NO_COLOR").is_ok();
|
let is_no_color = unsafe { !libc::getenv(NO_COLOR).is_null() };
|
||||||
Colors::new(is_no_color)
|
Colors::new(is_no_color)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,27 @@
|
||||||
use std::fmt::Write;
|
use std::{ffi::CStr, 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_str = unsafe {
|
||||||
let display_backend = std::env::var("XDG_SESSION_TYPE");
|
let ptr = libc::getenv(c"XDG_CURRENT_DESKTOP".as_ptr());
|
||||||
|
if ptr.is_null() {
|
||||||
let desktop_str = match desktop_env {
|
"Unknown"
|
||||||
Err(_) => "Unknown",
|
} else {
|
||||||
Ok(ref s) if s.starts_with("none+") => &s[5..],
|
let s = CStr::from_ptr(ptr).to_str().unwrap_or("Unknown");
|
||||||
Ok(ref s) => s.as_str(),
|
s.strip_prefix("none+").unwrap_or(s)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let backend_str = match display_backend {
|
let backend_str = unsafe {
|
||||||
Err(_) => "Unknown",
|
let ptr = libc::getenv(c"XDG_SESSION_TYPE".as_ptr());
|
||||||
Ok(ref s) if s.is_empty() => "Unknown",
|
if ptr.is_null() {
|
||||||
Ok(ref s) => s.as_str(),
|
"Unknown"
|
||||||
|
} else {
|
||||||
|
let s = CStr::from_ptr(ptr).to_str().unwrap_or("Unknown");
|
||||||
|
if s.is_empty() { "Unknown" } else { s }
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Pre-calculate capacity: desktop_len + " (" + backend_len + ")"
|
// Pre-calculate capacity: desktop_len + " (" + backend_len + ")"
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,18 @@
|
||||||
use std::{env, fmt::Write as _, io, mem::MaybeUninit};
|
use std::{ffi::CStr, fmt::Write as _, io, mem::MaybeUninit};
|
||||||
|
|
||||||
use crate::{UtsName, colors::COLORS, syscall::read_file_fast};
|
use crate::{UtsName, colors::COLORS, syscall::read_file_fast};
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[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 = unsafe {
|
||||||
|
let ptr = libc::getenv(c"USER".as_ptr());
|
||||||
|
if ptr.is_null() {
|
||||||
|
"unknown_user"
|
||||||
|
} else {
|
||||||
|
CStr::from_ptr(ptr).to_str().unwrap_or("unknown_user")
|
||||||
|
}
|
||||||
|
};
|
||||||
let hostname = utsname.nodename().to_str().unwrap_or("unknown_host");
|
let hostname = utsname.nodename().to_str().unwrap_or("unknown_host");
|
||||||
|
|
||||||
let capacity = COLORS.yellow.len()
|
let capacity = COLORS.yellow.len()
|
||||||
|
|
@ -18,7 +25,7 @@ pub fn get_username_and_hostname(utsname: &UtsName) -> String {
|
||||||
let mut result = String::with_capacity(capacity);
|
let mut result = String::with_capacity(capacity);
|
||||||
|
|
||||||
result.push_str(COLORS.yellow);
|
result.push_str(COLORS.yellow);
|
||||||
result.push_str(&username);
|
result.push_str(username);
|
||||||
result.push_str(COLORS.red);
|
result.push_str(COLORS.red);
|
||||||
result.push('@');
|
result.push('@');
|
||||||
result.push_str(COLORS.green);
|
result.push_str(COLORS.green);
|
||||||
|
|
@ -31,15 +38,17 @@ pub fn get_username_and_hostname(utsname: &UtsName) -> String {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
pub fn get_shell() -> String {
|
pub fn get_shell() -> String {
|
||||||
let shell_path =
|
unsafe {
|
||||||
env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_owned());
|
let ptr = libc::getenv(c"SHELL".as_ptr());
|
||||||
|
if ptr.is_null() {
|
||||||
|
return "unknown_shell".into();
|
||||||
|
}
|
||||||
|
|
||||||
// Find last '/' and get the part after it, avoiding allocation
|
let bytes = CStr::from_ptr(ptr).to_bytes();
|
||||||
shell_path
|
let start = bytes.iter().rposition(|&b| b == b'/').map_or(0, |i| i + 1);
|
||||||
.rsplit('/')
|
let name = std::str::from_utf8_unchecked(&bytes[start..]);
|
||||||
.next()
|
name.into()
|
||||||
.unwrap_or("unknown_shell")
|
}
|
||||||
.to_owned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the root disk usage information.
|
/// Gets the root disk usage information.
|
||||||
|
|
@ -106,7 +115,7 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
|
||||||
fn parse_memory_info() -> Result<(f64, f64), io::Error> {
|
fn parse_memory_info() -> Result<(f64, f64), io::Error> {
|
||||||
let mut total_memory_kb = 0u64;
|
let mut total_memory_kb = 0u64;
|
||||||
let mut available_memory_kb = 0u64;
|
let mut available_memory_kb = 0u64;
|
||||||
let mut buffer = [0u8; 2048];
|
let mut buffer = [0u8; 1024];
|
||||||
|
|
||||||
// Use fast syscall-based file reading
|
// Use fast syscall-based file reading
|
||||||
let bytes_read = read_file_fast("/proc/meminfo", &mut buffer)?;
|
let bytes_read = read_file_fast("/proc/meminfo", &mut buffer)?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue