mirror of
				https://github.com/NotAShelf/microfetch.git
				synced 2025-10-31 14:22:38 +00:00 
			
		
		
		
	micro optimizations (#8)
* perf: break early after parsing required meminfo Also a match statement for compiler magic. * perf: pre-allocate strings when reading files * refactor: remove duplicate .to_string() * perf: try to print everything in one syscall println! sends a syscall for each line. * perf: get rid of duplicate uname syscall * perf: simplify first letter capitalization * refactor: directly use key in match statement
This commit is contained in:
		
					parent
					
						
							
								cb9703f820
							
						
					
				
			
			
				commit
				
					
						907112f2d1
					
				
			
		
					 4 changed files with 52 additions and 42 deletions
				
			
		|  | @ -1,21 +1,13 @@ | |||
| pub fn get_desktop_info() -> String { | ||||
|     fn capitalize_first_letter(s: &str) -> String { | ||||
|         if s.is_empty() { | ||||
|             return String::new(); | ||||
|         } | ||||
| 
 | ||||
|         let mut chars = s.chars(); | ||||
|         let first_char = chars.next().unwrap().to_uppercase().to_string(); | ||||
|         let rest: String = chars.collect(); | ||||
|         first_char + &rest | ||||
|     } | ||||
| 
 | ||||
|     // 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 display_backend = capitalize_first_letter(display_backend_result.as_deref().unwrap_or("")); | ||||
|     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
 | ||||
|  | @ -26,10 +18,11 @@ pub fn get_desktop_info() -> String { | |||
| 
 | ||||
|     // Handle the case where display_backend might be empty after capitalization
 | ||||
|     let display_backend = if display_backend.is_empty() { | ||||
|         "Unknown".to_string() | ||||
|         "Unknown" | ||||
|     } else { | ||||
|         display_backend | ||||
|     }; | ||||
|         &display_backend | ||||
|     } | ||||
|     .to_string(); | ||||
| 
 | ||||
|     format!("{desktop_env} ({display_backend})") | ||||
| } | ||||
|  |  | |||
							
								
								
									
										12
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								src/main.rs
									
										
									
									
									
								
							|  | @ -4,6 +4,8 @@ mod release; | |||
| mod system; | ||||
| mod uptime; | ||||
| 
 | ||||
| use std::io::Write; | ||||
| 
 | ||||
| use crate::colors::{print_dots, BLUE, CYAN, RESET}; | ||||
| use crate::desktop::get_desktop_info; | ||||
| use crate::release::{get_os_pretty_name, get_system_info}; | ||||
|  | @ -15,10 +17,11 @@ use color_eyre::Report; | |||
| fn main() -> Result<(), Report> { | ||||
|     color_eyre::install()?; | ||||
| 
 | ||||
|     let utsname = nix::sys::utsname::uname()?; | ||||
|     let fields = Fields { | ||||
|         user_info: get_username_and_hostname(), | ||||
|         user_info: get_username_and_hostname(&utsname), | ||||
|         os_name: get_os_pretty_name()?, | ||||
|         kernel_version: get_system_info()?, | ||||
|         kernel_version: get_system_info(&utsname)?, | ||||
|         shell: get_shell(), | ||||
|         desktop: get_desktop_info(), | ||||
|         uptime: get_current()?, | ||||
|  | @ -61,7 +64,7 @@ fn print_system_info(fields: &Fields) { | |||
|         colors, | ||||
|     } = fields; | ||||
| 
 | ||||
|     println!( | ||||
|     let _ = std::io::stdout().write_all(format!( | ||||
|         " | ||||
|  {CYAN}     ▟█▖    {BLUE}▝█▙ ▗█▛          {user_info} ~{RESET} | ||||
|  {CYAN}  ▗▄▄▟██▄▄▄▄▄{BLUE}▝█▙█▛  {CYAN}▖        {CYAN}  {BLUE}System{RESET}        {os_name} | ||||
|  | @ -71,5 +74,6 @@ fn print_system_info(fields: &Fields) { | |||
|  {BLUE}   ▟█▛{CYAN}▗█▖       {CYAN}▟█▛          {CYAN}  {BLUE}Desktop{RESET}       {desktop} | ||||
|  {BLUE}  ▝█▛  {CYAN}██▖{BLUE}▗▄▄▄▄▄▄▄▄▄▄▄       {CYAN}  {BLUE}Memory{RESET}        {memory_usage} | ||||
|  {BLUE}   ▝  {CYAN}▟█▜█▖{BLUE}▀▀▀▀▀██▛▀▀▘       {CYAN}  {BLUE}Storage (/){RESET}   {storage} | ||||
|  {CYAN}     ▟█▘ ▜█▖    {BLUE}▝█▛          {CYAN}  {BLUE}Colors{RESET}        {colors}");
 | ||||
|  {CYAN}     ▟█▘ ▜█▖    {BLUE}▝█▛          {CYAN}  {BLUE}Colors{RESET}        {colors} | ||||
| ").as_bytes());
 | ||||
| } | ||||
|  |  | |||
|  | @ -1,9 +1,11 @@ | |||
| use color_eyre::Result; | ||||
| use std::fs::read_to_string; | ||||
| use std::io; | ||||
| use nix::sys::utsname::UtsName; | ||||
| use std::{ | ||||
|     fs::File, | ||||
|     io::{self, Read}, | ||||
| }; | ||||
| 
 | ||||
| pub fn get_system_info() -> nix::Result<String> { | ||||
|     let utsname = nix::sys::utsname::uname()?; | ||||
| pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> { | ||||
|     Ok(format!( | ||||
|         "{} {} ({})", | ||||
|         utsname.sysname().to_str().unwrap_or("Unknown"), | ||||
|  | @ -13,15 +15,13 @@ pub fn get_system_info() -> nix::Result<String> { | |||
| } | ||||
| 
 | ||||
| pub fn get_os_pretty_name() -> Result<String, io::Error> { | ||||
|     let os_release_content = read_to_string("/etc/os-release")?; | ||||
|     let mut os_release_content = String::with_capacity(1024); | ||||
|     File::open("/etc/os-release")?.read_to_string(&mut os_release_content)?; | ||||
| 
 | ||||
|     let pretty_name = os_release_content | ||||
|         .lines() | ||||
|         .find(|line| line.starts_with("PRETTY_NAME=")) | ||||
|         .map(|line| { | ||||
|             line.trim_start_matches("PRETTY_NAME=") | ||||
|                 .trim_matches('"') | ||||
|                 .to_string() | ||||
|         }); | ||||
|         .map(|line| line.trim_start_matches("PRETTY_NAME=").trim_matches('"')); | ||||
| 
 | ||||
|     Ok(pretty_name.unwrap_or("Unknown".to_string())) | ||||
|     Ok(pretty_name.unwrap_or("Unknown").to_string()) | ||||
| } | ||||
|  |  | |||
|  | @ -1,15 +1,21 @@ | |||
| use color_eyre::Result; | ||||
| use nix::sys::statvfs::statvfs; | ||||
| use nix::sys::{statvfs::statvfs, utsname::UtsName}; | ||||
| 
 | ||||
| use std::env; | ||||
| use std::io::{self}; | ||||
| use std::{ | ||||
|     env, | ||||
|     fs::File, | ||||
|     io::{self, Read}, | ||||
| }; | ||||
| 
 | ||||
| use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW}; | ||||
| 
 | ||||
| pub fn get_username_and_hostname() -> String { | ||||
| pub fn get_username_and_hostname(utsname: &UtsName) -> String { | ||||
|     let username = env::var("USER").unwrap_or("unknown_user".to_string()); | ||||
|     let hostname = nix::unistd::gethostname().unwrap_or("unknown_host".to_string().into()); | ||||
|     let hostname = hostname.to_string_lossy(); | ||||
|     let hostname = utsname | ||||
|         .nodename() | ||||
|         .to_str() | ||||
|         .unwrap_or("unknown_host") | ||||
|         .to_string(); | ||||
| 
 | ||||
|     format!("{YELLOW}{username}{RED}@{GREEN}{hostname}") | ||||
| } | ||||
|  | @ -45,13 +51,20 @@ pub fn get_memory_usage() -> Result<String, io::Error> { | |||
|         let mut total_memory_kb = 0.0; | ||||
|         let mut available_memory_kb = 0.0; | ||||
| 
 | ||||
|         for line in std::fs::read_to_string("/proc/meminfo")?.lines() { | ||||
|         let mut meminfo = String::with_capacity(2048); | ||||
|         File::open("/proc/meminfo")?.read_to_string(&mut meminfo)?; | ||||
| 
 | ||||
|         for line in meminfo.lines() { | ||||
|             let mut split = line.split_whitespace(); | ||||
|             let key = split.next().unwrap_or(""); | ||||
|             if key == "MemTotal:" { | ||||
|                 total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0); | ||||
|             } else if key == "MemAvailable:" { | ||||
|                 available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0); | ||||
|             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; | ||||
|                 } | ||||
|                 _ => (), | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Ryan
				Ryan