dev #1

Merged
candifloss merged 23 commits from dev into main 2024-12-02 04:26:43 +00:00
2 changed files with 34 additions and 29 deletions
Showing only changes of commit 735158a123 - Show all commits

View File

@ -58,7 +58,8 @@ pub fn pwd(
replace_repo: bool,
git_repo: Option<String>,
) -> 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() {

View File

@ -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<String> = std::env::args().collect(); // Cmd-line args
// 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.
}
}
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
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!()`
}