Comparatively cleaner code

This commit is contained in:
Candifloss 2024-08-17 21:30:08 +05:30
parent 69ea3213a5
commit 27b0a77202

View File

@ -24,6 +24,51 @@ fn get_shell_char (shell: String) -> String {
shell_char.to_string() shell_char.to_string()
} }
fn get_git_branch () -> String {
let git_status_cmd = Command::new("git")
.arg("status")
.output()
.expect("git_status_cmd_fail");
let git_status_output = String::from_utf8_lossy(&git_status_cmd.stdout);
let git_err = String::from_utf8_lossy(&git_status_cmd.stderr);
if git_err == "" {
git_status_output.split("\n").collect::<Vec<&str>>()[0]
.split(" ").collect::<Vec<&str>>()[2].to_string()
//.truecolor(82,82,82);
}
else {
"".to_string()
}
}
fn get_git_root () -> String {
let git_repo_root_cmd = Command::new("git")
.arg("rev-parse")
.arg("--show-toplevel")
.output()
.expect("git_repo_root_cmd_fail");
let mut git_repo_path = String::from_utf8_lossy(&git_repo_root_cmd.stdout).to_string();
let git_repo_err = String::from_utf8_lossy(&git_repo_root_cmd.stderr);
if git_repo_err == "" {
let len = git_repo_path.trim_end_matches(&['\r', '\n'][..]).len();
git_repo_path.truncate(len);
}
else {
git_repo_path = "".to_string();
}
git_repo_path
}
fn get_git_repo_name (git_repo_root: String) -> String {
let repo_path_split: Vec<&str> = git_repo_root.split("/").collect();
let last_index = repo_path_split.len() - 1;
let git_repo_name = repo_path_split[last_index];//.truecolor(82,82,82);
git_repo_name.to_string()
}
fn get_git_char (git_branch: String) -> ColoredString { fn get_git_char (git_branch: String) -> ColoredString {
match git_branch.as_str() { match git_branch.as_str() {
"main" => " 󰊢 ".truecolor(178,98,44), "main" => " 󰊢 ".truecolor(178,98,44),
@ -90,38 +135,10 @@ fn main() -> std::io::Result<()> {
} }
//Git status //Git status
let mut git_char = "".clear(); let git_branch = get_git_branch();//.clear();
let mut git_repo_name = "".clear(); let git_repo_root = get_git_root();
let mut git_branch = "".clear(); let git_repo_name = get_git_repo_name(git_repo_root);
let git_status_cmd = Command::new("git") let git_char = get_git_char(git_branch); //.to_string());
.arg("status")
.output()
.expect("git_status_cmd_fail");
let git_status = String::from_utf8_lossy(&git_status_cmd.stdout);
let git_err = String::from_utf8_lossy(&git_status_cmd.stderr);
if git_err == "" {
git_branch = git_status.split("\n").collect::<Vec<&str>>()[0]
.split(" ").collect::<Vec<&str>>()[2]
.truecolor(82,82,82);
git_char = get_git_char(git_branch.to_string());
let git_repo_root_cmd = Command::new("git")
.arg("rev-parse")
.arg("--show-toplevel")
.output()
.expect("git_repo_root_cmd_fail");
let mut git_repo_path = String::from_utf8_lossy(&git_repo_root_cmd.stdout).to_string();
let git_repo_err = String::from_utf8_lossy(&git_repo_root_cmd.stderr);
if git_repo_err == "" {
let len = git_repo_path.trim_end_matches(&['\r', '\n'][..]).len();
git_repo_path.truncate(len);
let repo_path_split: Vec<&str> = git_repo_path.split("/").collect();
let last_index = repo_path_split.len() - 1;
git_repo_name = repo_path_split[last_index]
.truecolor(82,82,82);
}
}
print!("{}{}{}{} {}{} ", print!("{}{}{}{} {}{} ",
ssh_char, ssh_char,