2024-11-21 10:58:26 +00:00
mod indicators {
2024-11-25 07:53:37 +00:00
pub mod error ;
2024-11-24 14:23:15 +00:00
pub mod git ;
2024-11-26 08:13:39 +00:00
pub mod pwd ;
2024-11-24 14:23:15 +00:00
pub mod ssh ;
2024-11-21 10:58:26 +00:00
pub mod user ;
2024-08-14 14:58:16 +00:00
}
2024-11-26 07:04:31 +00:00
2024-11-30 06:48:47 +00:00
use crate ::indicators ::{ error , git , pwd , ssh , user } ;
2024-11-28 10:51:38 +00:00
use ansi_term ::{ ANSIGenericString , ANSIGenericStrings } ;
2024-08-14 14:58:16 +00:00
2024-11-30 06:48:47 +00:00
// Add a component to the prompt if the condition is true.
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.
}
}
2024-11-24 14:23:15 +00:00
fn main ( ) {
2024-11-30 06:48:47 +00:00
let cmd_args : Vec < String > = std ::env ::args ( ) . collect ( ) ; // Command-line args
2024-11-27 11:11:55 +00:00
2024-11-30 06:48:47 +00:00
// 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.
2024-11-23 11:20:15 +00:00
2024-11-30 06:01:50 +00:00
// Hard-coded configuration. This will be replaced by a configuration file in a future version
2024-11-24 14:23:15 +00:00
let indicate_user : bool = true ;
let indicate_ssh : bool = true ;
2024-11-25 07:53:37 +00:00
let indicate_err : bool = true ;
2024-11-24 14:23:15 +00:00
let indicate_git_branch : bool = true ;
2024-11-27 11:11:55 +00:00
let show_pwd : bool = true ;
let abbrev_home : bool = true ;
let shorten_path : bool = true ;
let replace_repo : bool = true ;
2024-11-28 10:51:38 +00:00
2024-11-30 06:48:47 +00:00
// Conditionally fetch Git-related info if required, or set to None
2024-11-28 10:51:38 +00:00
let git_info : Option < ( String , String ) > = if indicate_git_branch | | replace_repo {
git ::info ( )
} else {
None
} ;
2024-11-24 14:23:15 +00:00
2024-11-30 06:01:50 +00:00
// Conditionally add the parts of the prompt
2024-11-30 06:48:47 +00:00
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
2024-11-27 11:11:55 +00:00
if show_pwd {
2024-11-30 06:48:47 +00:00
let repo_path = git_info . map ( | info | info . 0 ) ;
2024-11-27 11:11:55 +00:00
components . insert (
0 ,
2024-11-28 10:51:38 +00:00
pwd ::pwd ( abbrev_home , shorten_path , replace_repo , repo_path ) ,
2024-11-27 11:11:55 +00:00
) ;
2024-11-25 07:53:37 +00:00
}
2024-11-30 06:48:47 +00:00
// Finally, combine the parts into a single prompts string
2024-11-28 10:51:38 +00:00
let prompt : ANSIGenericStrings < '_ , str > = ANSIGenericStrings ( & components [ .. ] ) ;
2024-11-30 08:46:14 +00:00
// `print!()` prevents an extra newline, unlike `println!()`
print! ( " {prompt} " ) ; // A trailing space for aesthetic formatting.
2024-08-19 13:24:12 +00:00
}