From 27b0a772029ce4a52a2a009e408815b220b42a41 Mon Sep 17 00:00:00 2001 From: candifloss Date: Sat, 17 Aug 2024 21:30:08 +0530 Subject: [PATCH] Comparatively cleaner code --- src/main.rs | 81 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index 519e450..5b7a17c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,51 @@ fn get_shell_char (shell: String) -> 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::>()[0] + .split(" ").collect::>()[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 { match git_branch.as_str() { "main" => " 󰊢 ".truecolor(178,98,44), @@ -90,38 +135,10 @@ fn main() -> std::io::Result<()> { } //Git status - let mut git_char = "".clear(); - let mut git_repo_name = "".clear(); - let mut git_branch = "".clear(); - let git_status_cmd = Command::new("git") - .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::>()[0] - .split(" ").collect::>()[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); - } - } + let git_branch = get_git_branch();//.clear(); + let git_repo_root = get_git_root(); + let git_repo_name = get_git_repo_name(git_repo_root); + let git_char = get_git_char(git_branch); //.to_string()); print!("{}{}{}{} {}{} ", ssh_char,