improved code readability & comments

This commit is contained in:
Candifloss 2024-11-30 12:18:47 +05:30
parent 82528f13be
commit 735158a123
2 changed files with 34 additions and 29 deletions

View File

@ -58,7 +58,8 @@ pub fn pwd(
replace_repo: bool, replace_repo: bool,
git_repo: Option<String>, git_repo: Option<String>,
) -> ANSIGenericString<'static, str> { ) -> 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 let mut path = full_path(); // Get the full path of he current directory
// Replace a git repo root path with the repo's name // Replace a git repo root path with the repo's name
@ -67,6 +68,7 @@ pub fn pwd(
path = remove_repo(&path, &repo_path); path = remove_repo(&path, &repo_path);
let repo_name = repo_name(&repo_path); let repo_name = repo_name(&repo_path);
if shorten_path { if shorten_path {
let slash_limit: u8 = 2; // Max number of slashes
path = short(&path, slash_limit); path = short(&path, slash_limit);
} }
return if path.is_empty() { return if path.is_empty() {

View File

@ -6,17 +6,25 @@ mod indicators {
pub mod user; pub mod user;
} }
use crate::indicators::error; use crate::indicators::{error, git, pwd, ssh, user};
use crate::indicators::git;
use crate::indicators::pwd;
use crate::indicators::ssh;
use crate::indicators::user;
use ansi_term::{ANSIGenericString, ANSIGenericStrings}; use ansi_term::{ANSIGenericString, ANSIGenericStrings};
fn main() { // Add a component to the prompt if the condition is true.
let cmd_args: Vec<String> = std::env::args().collect(); // Cmd-line args fn add_component(
components: &mut Vec<ANSIGenericString<'static, str>>, // 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<ANSIGenericString<'static, str>> = Vec::new(); // The components will be concated into one string fn main() {
let cmd_args: Vec<String> = std::env::args().collect(); // Command-line args
// Vector to hold the different parts of the prompt.
let mut components: Vec<ANSIGenericString<'static, str>> = 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 // Hard-coded configuration. This will be replaced by a configuration file in a future version
let indicate_user: bool = true; let indicate_user: bool = true;
@ -28,6 +36,7 @@ fn main() {
let shorten_path: bool = true; let shorten_path: bool = true;
let replace_repo: 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 { let git_info: Option<(String, String)> = if indicate_git_branch || replace_repo {
git::info() git::info()
} else { } else {
@ -35,31 +44,25 @@ fn main() {
}; };
// Conditionally add the parts of the prompt // Conditionally add the parts of the prompt
if indicate_ssh { add_component(&mut components, indicate_ssh, ssh::indicator);
components.push(ssh::indicator()); add_component(&mut components, indicate_git_branch, || {
} git::indicator(git_info.as_ref().map(|info| info.1.clone()))
if indicate_git_branch { });
match git_info { add_component(&mut components, indicate_user, user::indicator);
Some(ref info) => components.push(git::indicator(Some(info.1.clone()))), add_component(&mut components, indicate_err, || {
None => components.push(git::indicator(None)), error::indicator(&cmd_args)
} });
}
if indicate_user { // Insert `pwd` at the beginning of the prompt
components.push(user::indicator());
}
if indicate_err {
components.push(error::indicator(&cmd_args));
}
if show_pwd { if show_pwd {
let repo_path = match git_info { let repo_path = git_info.map(|info| info.0);
Some(info) => Some(info.0),
None => None,
};
components.insert( components.insert(
0, 0,
pwd::pwd(abbrev_home, shorten_path, replace_repo, repo_path), 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[..]); let prompt: ANSIGenericStrings<'_, str> = ANSIGenericStrings(&components[..]);
print!("{prompt} "); print!("{prompt} "); // A trailing space for aesthetic formatting. `print!()` prevents an extra newline, unlike `println!()`
} }