Compare commits

...

2 commits

Author SHA1 Message Date
927f6077b4
store printed fields in a struct
Some checks failed
Rust / build (push) Has been cancelled
Shut up, clippy.
2024-08-05 11:33:42 +03:00
Ryan
13903539b7
perf: use nix::sys::utsname::uname for less syscalls (#2)
Instead of reading multiple files to get the `sysname`, `release`, and
`machine` name, use the `nix::sys::utsname::uname` function which sends a
single uname syscall instead. This increases performance and portability.

From my observations, there are ~10 less syscalls.
2024-08-05 08:08:25 +00:00
3 changed files with 49 additions and 58 deletions

View file

@ -4,7 +4,7 @@ version = "0.3.3"
edition = "2021"
[dependencies]
nix = {version = "0.29", features = ["fs", "hostname"]}
nix = { version = "0.29", features = ["fs", "hostname", "feature"] }
color-eyre = { version = "0.6", default-features = false }
[profile.dev]

View file

@ -16,43 +16,40 @@ use nix::sys::sysinfo::sysinfo;
fn main() -> Result<(), Report> {
color_eyre::install()?;
let user_info = get_username_and_hostname()?;
let os_name = get_os_pretty_name()?;
let kernel_version = get_system_info()?;
let shell = get_shell()?;
let uptime = get_current()?;
let window_manager = get_desktop_info()?;
let sys_info = sysinfo()?;
let memory_usage = get_memory_usage(sys_info);
let storage = get_root_disk_usage()?;
let colors = print_dots()?;
let fields = Fields {
user_info: get_username_and_hostname()?,
os_name: get_os_pretty_name()?,
kernel_version: get_system_info()?,
shell: get_shell()?,
uptime: get_current()?,
window_manager: get_desktop_info()?,
memory_usage: get_memory_usage(sysinfo()?),
storage: get_root_disk_usage()?,
colors: print_dots()?,
};
print_system_info(
&user_info,
&os_name,
&kernel_version,
&shell,
&uptime,
&window_manager,
&memory_usage,
&storage,
&colors,
);
print_system_info(&fields);
Ok(())
}
fn print_system_info(
user_info: &str,
os_name: &str,
kernel_version: &str,
shell: &str,
uptime: &str,
window_manager: &str,
memory_usage: &str,
storage: &str,
colors: &str,
) {
// Struct to hold all the fields we need to print
// helps avoid clippy warnings about argument count
// and makes it easier to pass around, though its
// not like we need to
struct Fields {
user_info: String,
os_name: String,
kernel_version: String,
shell: String,
uptime: String,
window_manager: String,
memory_usage: String,
storage: String,
colors: String,
}
fn print_system_info(fields: &Fields) {
println!(
"
{CYAN} {BLUE} {user_info} ~{RESET}
@ -64,6 +61,15 @@ fn print_system_info(
{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,
);
}

View file

@ -1,30 +1,15 @@
use color_eyre::Result;
use std::fs::{self, read_to_string};
use std::fs::read_to_string;
use std::io;
// Try to detect OS type as accurately as possible and without depending on uname.
// /etc/os-release should generally imply Linux, and /etc/bsd-release would imply BSD system.
fn detect_os() -> Result<&'static str, io::Error> {
if fs::metadata("/etc/os-release").is_ok() || fs::metadata("/usr/lib/os-release").is_ok() {
Ok("Linux")
} else if fs::metadata("/etc/rc.conf").is_ok() || fs::metadata("/etc/bsd-release").is_ok() {
Ok("BSD")
} else {
Ok("Unknown")
}
}
pub fn get_system_info() -> Result<String, io::Error> {
let system = detect_os()?;
let kernel_release = read_to_string("/proc/sys/kernel/osrelease")?;
let kernel_release = kernel_release.trim();
let architecture = read_to_string("/proc/sys/kernel/arch")?;
let architecture = architecture.trim();
let result = format!("{system} {kernel_release} ({architecture})");
Ok(result)
pub fn get_system_info() -> nix::Result<String> {
let utsname = nix::sys::utsname::uname()?;
Ok(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<String, io::Error> {