mod indicators { pub mod error; pub mod git; pub mod pwd; pub mod ssh; pub mod user; } use crate::indicators::{error, git, pwd, ssh, user}; use ansi_term::{ANSIGenericString, ANSIGenericStrings}; // 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. } } 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; let indicate_ssh: bool = true; let indicate_err: bool = true; let indicate_git_branch: bool = true; let show_pwd: bool = true; let abbrev_home: bool = true; 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 { None }; // Conditionally add the parts of the prompt 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 = 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!()` prevents an extra newline, unlike `println!()` print!("{prompt} "); // A trailing space for aesthetic formatting. }