From 735158a1235e4df9ccc1f7a4e52f185f03313367 Mon Sep 17 00:00:00 2001 From: candifloss Date: Sat, 30 Nov 2024 12:18:47 +0530 Subject: [PATCH] improved code readability & comments --- src/indicators/pwd.rs | 4 ++- src/main.rs | 59 +++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/indicators/pwd.rs b/src/indicators/pwd.rs index 691681a..328acf1 100644 --- a/src/indicators/pwd.rs +++ b/src/indicators/pwd.rs @@ -58,7 +58,8 @@ pub fn pwd( replace_repo: bool, git_repo: Option, ) -> ANSIGenericString<'static, str> { - let slash_limit: u8 = if replace_repo { 2 } else { 3 }; // Max number of slashes + //let slash_limit: u8 = if replace_repo { 2 } else { 3 }; // Max number of slashes + let slash_limit: u8 = 3; // Max number of slashes let mut path = full_path(); // Get the full path of he current directory // Replace a git repo root path with the repo's name @@ -67,6 +68,7 @@ pub fn pwd( path = remove_repo(&path, &repo_path); let repo_name = repo_name(&repo_path); if shorten_path { + let slash_limit: u8 = 2; // Max number of slashes path = short(&path, slash_limit); } return if path.is_empty() { diff --git a/src/main.rs b/src/main.rs index 3f1bcc7..c90dcca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,17 +6,25 @@ mod indicators { pub mod user; } -use crate::indicators::error; -use crate::indicators::git; -use crate::indicators::pwd; -use crate::indicators::ssh; -use crate::indicators::user; +use crate::indicators::{error, git, pwd, ssh, user}; use ansi_term::{ANSIGenericString, ANSIGenericStrings}; -fn main() { - let cmd_args: Vec = std::env::args().collect(); // Cmd-line args +// Add a component to the prompt if the condition is true. +fn add_component( + components: &mut Vec>, // Vector to hold components of the prompt. + condition: bool, // Condition to add the component + component_fn: impl FnOnce() -> ANSIGenericString<'static, str>, // Function to create the component(takes no arguments, returns `ANSIGenericString`) is passed here. +) { + if condition { + components.push(component_fn()); // Push the generated component to the vector. + } +} - let mut components: Vec> = Vec::new(); // The components will be concated into one string +fn main() { + let cmd_args: Vec = std::env::args().collect(); // Command-line args + + // Vector to hold the different parts of the prompt. + let mut components: Vec> = Vec::new(); // The components will be concatenated into a single string in the end. // Hard-coded configuration. This will be replaced by a configuration file in a future version let indicate_user: bool = true; @@ -28,6 +36,7 @@ fn main() { let shorten_path: bool = true; let replace_repo: bool = true; + // Conditionally fetch Git-related info if required, or set to None let git_info: Option<(String, String)> = if indicate_git_branch || replace_repo { git::info() } else { @@ -35,31 +44,25 @@ fn main() { }; // Conditionally add the parts of the prompt - if indicate_ssh { - components.push(ssh::indicator()); - } - if indicate_git_branch { - match git_info { - Some(ref info) => components.push(git::indicator(Some(info.1.clone()))), - None => components.push(git::indicator(None)), - } - } - if indicate_user { - components.push(user::indicator()); - } - if indicate_err { - components.push(error::indicator(&cmd_args)); - } + add_component(&mut components, indicate_ssh, ssh::indicator); + add_component(&mut components, indicate_git_branch, || { + git::indicator(git_info.as_ref().map(|info| info.1.clone())) + }); + add_component(&mut components, indicate_user, user::indicator); + add_component(&mut components, indicate_err, || { + error::indicator(&cmd_args) + }); + + // Insert `pwd` at the beginning of the prompt if show_pwd { - let repo_path = match git_info { - Some(info) => Some(info.0), - None => None, - }; + let repo_path = git_info.map(|info| info.0); components.insert( 0, pwd::pwd(abbrev_home, shorten_path, replace_repo, repo_path), ); } + + // Finally, combine the parts into a single prompts string let prompt: ANSIGenericStrings<'_, str> = ANSIGenericStrings(&components[..]); - print!("{prompt} "); + print!("{prompt} "); // A trailing space for aesthetic formatting. `print!()` prevents an extra newline, unlike `println!()` }