From 7b27e81c896920545734bee19b592636d282ddad Mon Sep 17 00:00:00 2001 From: candifloss Date: Thu, 21 Nov 2024 14:09:00 +0530 Subject: [PATCH] introduce modularity --- src/git.rs | 55 +++++++++++++++++++++++++++++++++++++ src/main.rs | 79 ++++++----------------------------------------------- src/ssh.rs | 10 +++++++ src/user.rs | 20 ++++++++++++++ 4 files changed, 94 insertions(+), 70 deletions(-) create mode 100644 src/git.rs create mode 100644 src/ssh.rs create mode 100644 src/user.rs diff --git a/src/git.rs b/src/git.rs new file mode 100644 index 0000000..24b006a --- /dev/null +++ b/src/git.rs @@ -0,0 +1,55 @@ +use colored::{ColoredString, Colorize}; +use std::process::Command; + +pub 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() + } else { + String::new() + } +} + +pub 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 = String::new(); + } + git_repo_path +} + +pub fn get_git_repo_name(git_repo_root: &str) -> 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]; + + git_repo_name.to_string() +} + +pub fn get_git_char(git_branch: &str) -> ColoredString { + match git_branch { + "" => "".clear(), + "main" => " 󰊢 ".truecolor(178, 98, 44), + "master" => " 󰊢 ".truecolor(196, 132, 29), + _ => " 󰊢 ".truecolor(82, 82, 82), + } +} diff --git a/src/main.rs b/src/main.rs index 7bd6c8c..b1c24fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,11 @@ use colored::{ColoredString, Colorize}; use std::env::{args, current_dir, var_os}; -use std::process::Command; +mod git; +mod ssh; +mod user; +use crate::git::*; +use crate::ssh::*; +use crate::user::*; fn get_shell_char(shell: &str) -> String { let shell_char = match shell { @@ -15,59 +20,6 @@ fn get_shell_char(shell: &str) -> 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() - } else { - String::new() - } -} - -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 = String::new(); - } - git_repo_path -} - -fn get_git_repo_name(git_repo_root: &str) -> 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]; - - git_repo_name.to_string() -} - -fn get_git_char(git_branch: &str) -> ColoredString { - match git_branch { - "" => "".clear(), - "main" => " 󰊢 ".truecolor(178, 98, 44), - "master" => " 󰊢 ".truecolor(196, 132, 29), - _ => " 󰊢 ".truecolor(82, 82, 82), - } -} - fn abrev_path(path: &str) -> String { let mut short_dir = path.to_string(); @@ -92,13 +44,6 @@ fn abrev_path(path: &str) -> String { fn main() -> std::io::Result<()> { let angle = "❯"; - //Root user indicator - let user = var_os("USER") - .expect("UnknownUser") - .to_str() - .expect("UnknownUser") - .to_string(); - let mut err: String = String::new(); let args: Vec = args().collect(); @@ -112,21 +57,15 @@ fn main() -> std::io::Result<()> { shell = "none".to_string(); } - let root_indicator = match user.as_str() { - "root" => angle.truecolor(255, 53, 94), - _ => angle.truecolor(0, 255, 180), - }; + let root_indicator = root_indicator(); let err_indicator = match err.as_str() { "0" => angle.truecolor(0, 255, 180), _ => angle.truecolor(255, 53, 94), }; - //SSH shell indicator - let ssh_char: ColoredString = match var_os("SSH_TTY") { - Some(_val) => " ".truecolor(0, 150, 180), - None => "".clear(), - }; + // SSH status + let ssh_char = ssh_char(); //Git status let git_branch = get_git_branch(); diff --git a/src/ssh.rs b/src/ssh.rs new file mode 100644 index 0000000..49f86f9 --- /dev/null +++ b/src/ssh.rs @@ -0,0 +1,10 @@ +use colored::{ColoredString, Colorize}; +use std::env::var_os; + +//SSH shell indicator +pub fn ssh_char() -> ColoredString { + match var_os("SSH_TTY") { + Some(_val) => " ".truecolor(0, 150, 180), + None => "".clear(), + } +} diff --git a/src/user.rs b/src/user.rs new file mode 100644 index 0000000..00ed681 --- /dev/null +++ b/src/user.rs @@ -0,0 +1,20 @@ +use colored::{ColoredString, Colorize}; +use std::env::var_os; + +//Root user indicator + +pub fn user() -> String { + var_os("USER") + .expect("UnknownUser") + .to_str() + .expect("UnknownUser") + .to_string() +} + +pub fn root_indicator() -> ColoredString { + let angle = "❯"; + match user().as_str() { + "root" => angle.truecolor(255, 53, 94), + _ => angle.truecolor(0, 255, 180), + } +}