mirror of
				https://github.com/NotAShelf/microfetch.git
				synced 2025-11-03 23:32:19 +00:00 
			
		
		
		
	refactor: remove some redundant error types
This commit is contained in:
		
					parent
					
						
							
								927f6077b4
							
						
					
				
			
			
				commit
				
					
						65b282153d
					
				
			
		
					 5 changed files with 35 additions and 39 deletions
				
			
		| 
						 | 
					@ -6,8 +6,6 @@ pub const YELLOW: &str = "\x1b[33m";
 | 
				
			||||||
pub const RED: &str = "\x1b[31m";
 | 
					pub const RED: &str = "\x1b[31m";
 | 
				
			||||||
pub const MAGENTA: &str = "\x1b[35m";
 | 
					pub const MAGENTA: &str = "\x1b[35m";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn print_dots() -> Result<String, std::io::Error> {
 | 
					pub fn print_dots() -> String {
 | 
				
			||||||
    let colors = format!("{BLUE}  {CYAN}  {GREEN}  {YELLOW}  {RED}  {MAGENTA}  {RESET}");
 | 
					    format!("{BLUE}  {CYAN}  {GREEN}  {YELLOW}  {RED}  {MAGENTA}  {RESET}")
 | 
				
			||||||
 | 
					 | 
				
			||||||
    Ok(colors)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,20 +1,17 @@
 | 
				
			||||||
use color_eyre::Result;
 | 
					pub fn get_desktop_info() -> String {
 | 
				
			||||||
use std::{env, io};
 | 
					    let desktop_env = std::env::var("XDG_CURRENT_DESKTOP");
 | 
				
			||||||
 | 
					    let display_backend = std::env::var("XDG_SESSION_TYPE");
 | 
				
			||||||
pub fn get_desktop_info() -> Result<String, io::Error> {
 | 
					 | 
				
			||||||
    let desktop_env = env::var("XDG_CURRENT_DESKTOP");
 | 
					 | 
				
			||||||
    let display_backend = env::var("XDG_SESSION_TYPE");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Trim "none+" from the start of desktop_env if present
 | 
					    // Trim "none+" from the start of desktop_env if present
 | 
				
			||||||
    // XXX: This is a workaround for NixOS modules that set XDG_CURRENT_DESKTOP to "none+foo"
 | 
					    // XXX: This is a workaround for NixOS modules that set XDG_CURRENT_DESKTOP to "none+foo"
 | 
				
			||||||
    // instead of just "foo"
 | 
					    // instead of just "foo"
 | 
				
			||||||
    // Use "Unknown" if desktop_env or display_backend is empty
 | 
					    // Use "Unknown" if desktop_env or display_backend is empty
 | 
				
			||||||
    let desktop_env = match desktop_env {
 | 
					    let desktop_env = match desktop_env.as_ref() {
 | 
				
			||||||
        Err(_) => String::from("Unknown"),
 | 
					        Err(_) => "Unknown",
 | 
				
			||||||
        Ok(s) => s.trim_start_matches("none+").to_owned(),
 | 
					        Ok(s) => s.trim_start_matches("none+"),
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let display_backend = display_backend.unwrap_or_else(|_| String::from("Unknown"));
 | 
					    let display_backend = display_backend.unwrap_or(String::from("Unknown"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Ok(format!("{desktop_env} ({display_backend})"))
 | 
					    format!("{desktop_env} ({display_backend})")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										33
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										33
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
					@ -17,15 +17,15 @@ fn main() -> Result<(), Report> {
 | 
				
			||||||
    color_eyre::install()?;
 | 
					    color_eyre::install()?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let fields = Fields {
 | 
					    let fields = Fields {
 | 
				
			||||||
        user_info: get_username_and_hostname()?,
 | 
					        user_info: get_username_and_hostname(),
 | 
				
			||||||
        os_name: get_os_pretty_name()?,
 | 
					        os_name: get_os_pretty_name()?,
 | 
				
			||||||
        kernel_version: get_system_info()?,
 | 
					        kernel_version: get_system_info()?,
 | 
				
			||||||
        shell: get_shell()?,
 | 
					        shell: get_shell(),
 | 
				
			||||||
        uptime: get_current()?,
 | 
					        uptime: get_current()?,
 | 
				
			||||||
        window_manager: get_desktop_info()?,
 | 
					        window_manager: get_desktop_info(),
 | 
				
			||||||
        memory_usage: get_memory_usage(sysinfo()?),
 | 
					        memory_usage: get_memory_usage(sysinfo()?),
 | 
				
			||||||
        storage: get_root_disk_usage()?,
 | 
					        storage: get_root_disk_usage()?,
 | 
				
			||||||
        colors: print_dots()?,
 | 
					        colors: print_dots(),
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print_system_info(&fields);
 | 
					    print_system_info(&fields);
 | 
				
			||||||
| 
						 | 
					@ -50,6 +50,18 @@ struct Fields {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn print_system_info(fields: &Fields) {
 | 
					fn print_system_info(fields: &Fields) {
 | 
				
			||||||
 | 
					    let Fields {
 | 
				
			||||||
 | 
					        user_info,
 | 
				
			||||||
 | 
					        os_name,
 | 
				
			||||||
 | 
					        kernel_version,
 | 
				
			||||||
 | 
					        shell,
 | 
				
			||||||
 | 
					        uptime,
 | 
				
			||||||
 | 
					        window_manager,
 | 
				
			||||||
 | 
					        memory_usage,
 | 
				
			||||||
 | 
					        storage,
 | 
				
			||||||
 | 
					        colors,
 | 
				
			||||||
 | 
					    } = fields;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    println!(
 | 
					    println!(
 | 
				
			||||||
        "
 | 
					        "
 | 
				
			||||||
 {CYAN}     ▟█▖    {BLUE}▝█▙ ▗█▛          {user_info} ~{RESET}
 | 
					 {CYAN}     ▟█▖    {BLUE}▝█▙ ▗█▛          {user_info} ~{RESET}
 | 
				
			||||||
| 
						 | 
					@ -60,16 +72,5 @@ fn print_system_info(fields: &Fields) {
 | 
				
			||||||
 {BLUE}   ▟█▛{CYAN}▗█▖       {CYAN}▟█▛          {CYAN}  {BLUE}WM{RESET}            {window_manager}
 | 
					 {BLUE}   ▟█▛{CYAN}▗█▖       {CYAN}▟█▛          {CYAN}  {BLUE}WM{RESET}            {window_manager}
 | 
				
			||||||
 {BLUE}  ▝█▛  {CYAN}██▖{BLUE}▗▄▄▄▄▄▄▄▄▄▄▄       {CYAN}  {BLUE}Memory{RESET}        {memory_usage}
 | 
					 {BLUE}  ▝█▛  {CYAN}██▖{BLUE}▗▄▄▄▄▄▄▄▄▄▄▄       {CYAN}  {BLUE}Memory{RESET}        {memory_usage}
 | 
				
			||||||
 {BLUE}   ▝  {CYAN}▟█▜█▖{BLUE}▀▀▀▀▀██▛▀▀▘       {CYAN}  {BLUE}Storage (/){RESET}   {storage}
 | 
					 {BLUE}   ▝  {CYAN}▟█▜█▖{BLUE}▀▀▀▀▀██▛▀▀▘       {CYAN}  {BLUE}Storage (/){RESET}   {storage}
 | 
				
			||||||
 {CYAN}     ▟█▘ ▜█▖    {BLUE}▝█▛          {CYAN}  {BLUE}Colors{RESET}        {colors}
 | 
					 {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,
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,5 +23,5 @@ pub fn get_os_pretty_name() -> Result<String, io::Error> {
 | 
				
			||||||
                .to_string()
 | 
					                .to_string()
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Ok(pretty_name.unwrap_or_else(|| "Unknown".to_string()))
 | 
					    Ok(pretty_name.unwrap_or("Unknown".to_string()))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,19 +7,19 @@ use std::io::{self};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW};
 | 
					use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn get_username_and_hostname() -> Result<String, io::Error> {
 | 
					pub fn get_username_and_hostname() -> String {
 | 
				
			||||||
    let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_string());
 | 
					    let username = env::var("USER").unwrap_or("unknown_user".to_string());
 | 
				
			||||||
    let hostname = nix::unistd::gethostname()?;
 | 
					    let hostname = nix::unistd::gethostname().unwrap_or("unknown_host".to_string().into());
 | 
				
			||||||
    let hostname = hostname.to_string_lossy();
 | 
					    let hostname = hostname.to_string_lossy();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Ok(format!("{YELLOW}{username}{RED}@{GREEN}{hostname}"))
 | 
					    format!("{YELLOW}{username}{RED}@{GREEN}{hostname}")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn get_shell() -> Result<String, io::Error> {
 | 
					pub fn get_shell() -> String {
 | 
				
			||||||
    let shell_path = env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_string());
 | 
					    let shell_path = env::var("SHELL").unwrap_or("unknown_shell".to_string());
 | 
				
			||||||
    let shell_name = shell_path.rsplit('/').next().unwrap_or("unknown_shell");
 | 
					    let shell_name = shell_path.rsplit('/').next().unwrap_or("unknown_shell");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Ok(shell_name.to_string())
 | 
					    shell_name.to_string()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn get_root_disk_usage() -> Result<String, io::Error> {
 | 
					pub fn get_root_disk_usage() -> Result<String, io::Error> {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue