From 6d4c84b49a24d5c65d4eba82bff57d542d161ac0 Mon Sep 17 00:00:00 2001 From: vars1ty Date: Wed, 11 Jan 2023 07:51:45 +0100 Subject: [PATCH] 0.1.1: Optimization --- Cargo.lock | 2 +- Cargo.toml | 3 ++- src/info.rs | 58 ++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7759c2b..a739f20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,7 +28,7 @@ checksum = "aaa3a8d9a1ca92e282c96a32d6511b695d7d994d1d102ba85d279f9b2756947f" [[package]] name = "just-fetch" -version = "0.1.0" +version = "0.1.1" dependencies = [ "ansi_rgb", "arguments", diff --git a/Cargo.toml b/Cargo.toml index a4c40b8..d8f73c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "just-fetch" -version = "0.1.0" +version = "0.1.1" authors = [ "varsity " ] description = "A simple minimal neofetch alternative" edition = "2021" @@ -12,6 +12,7 @@ libc = "*" rgb = "*" [profile.release] +overflow-checks = false panic = "abort" opt-level = 3 strip = true diff --git a/src/info.rs b/src/info.rs index 63dfac1..5a3cfe4 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,5 +1,12 @@ +use libc::{c_char, sysconf, _SC_HOST_NAME_MAX}; + use crate::utils; -use std::{ffi::CStr, fs::read_to_string}; +use std::{ + ffi::{CStr, OsString}, + fs::read_to_string, + mem::MaybeUninit, + os::unix::prelude::OsStringExt, +}; /// Fetched system information. #[derive(Debug)] @@ -42,6 +49,37 @@ fn get_username() -> String { username.to_owned() } +/// Returns the active hostname. +fn get_hostname() -> String { + // Thanks to swsnr/gethostname.rs + let hostname_max = unsafe { sysconf(_SC_HOST_NAME_MAX) }; + let mut buffer = vec![0; (hostname_max as usize) + 1]; + unsafe { libc::gethostname(buffer.as_mut_ptr() as *mut _, buffer.len()) }; + let end = buffer + .iter() + .position(|&byte| byte == 0) + .unwrap_or(buffer.len()); + buffer.resize(end, 0); + OsString::from_vec(buffer) + .to_str() + .expect("[ERROR] Failed getting hostname as str!") + .to_owned() +} + +/// Returns the active kernel version. +fn get_kernel_version() -> String { + let mut info = unsafe { MaybeUninit::::zeroed().assume_init() }; + let mut result = vec![0; info.release.len()]; + unsafe { libc::uname(&mut info as *mut _) }; + + // Push content into `result` as `u8`. + for i in info.release { + result.push(i as u8); + } + + String::from_utf8(result).unwrap() +} + /// Fetches system information. pub fn get_system_information() -> Option { let os_release = @@ -50,20 +88,12 @@ pub fn get_system_information() -> Option { let distro_id = parse_key(&os_release, "ID")?; let distro_build_id = parse_key(&os_release, "BUILD_ID")?; - // Bundle commands into the same execute call, reducing the time needed to process the output. - // tl;dr: Ugly-ish trick for extra performance. - let bundled_command = utils::execute_batched("uname -n, echo $SHELL, uname -r, uptime -p"); - // Ensure the bundled command consists of 4 entries. - if bundled_command.len() != 4 { - panic!("[ERROR] bundled_command isn't consisting of 4 entries. Output: {bundled_command:?}") - } - let username = get_username(); - let hostname = bundled_command[0].to_owned(); - let shell = bundled_command[1].split('/').last()?.to_owned(); - let kernel = bundled_command[2].to_owned(); - let mut uptime = bundled_command[3].to_owned(); - let uptime_start = bundled_command[3] + let hostname = get_hostname(); + let shell = env!("SHELL").split('/').last()?.to_owned(); + let kernel = get_kernel_version(); + let mut uptime = utils::execute("uptime -p"); + let uptime_start = uptime .to_owned() .split_whitespace() .next()