From 24f3f6772ea8ea14adfa2c4e37ccf975d7c24dc3 Mon Sep 17 00:00:00 2001 From: candifloss Date: Sun, 24 Nov 2024 19:53:15 +0530 Subject: [PATCH] migrated user, git, & ssh indicators to ansi_term --- src/indicators/git.rs | 86 ++++++++++++++------------------ src/indicators/ssh.rs | 23 ++++++--- src/main.rs | 28 ++++++++--- src/{indicators => old}/error.rs | 0 src/{ => old}/main.bkp.rs | 0 src/{indicators => old}/path.rs | 0 src/{indicators => old}/shell.rs | 0 7 files changed, 76 insertions(+), 61 deletions(-) rename src/{indicators => old}/error.rs (100%) rename src/{ => old}/main.bkp.rs (100%) rename src/{indicators => old}/path.rs (100%) rename src/{indicators => old}/shell.rs (100%) diff --git a/src/indicators/git.rs b/src/indicators/git.rs index 24b006a..f14fd85 100644 --- a/src/indicators/git.rs +++ b/src/indicators/git.rs @@ -1,55 +1,45 @@ -use colored::{ColoredString, Colorize}; +use ansi_term::ANSIGenericString; +use ansi_term::Colour::RGB; 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); +// SSH indicator symbol +pub const GIT_SYMBOL: &str = "\u{276F}"; // "❯" +pub const MAIN_COL: ansi_term::Colour = RGB(178, 98, 44); // +pub const DEV_COL: ansi_term::Colour = RGB(54, 159, 150); // +pub const OTHER_COL: ansi_term::Colour = RGB(82, 82, 82); // +pub const NORMIE_COL: ansi_term::Colour = RGB(255, 255, 255); // White - 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") +pub fn info() -> Option<(String, String)> { + let output = 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); + .arg("--show-toplevel") // Repo root + .arg("--abbrev-ref") // Git branch + .arg("HEAD") + .output(); + match output { + Ok(output) if output.status.success() => { + let stdout = String::from_utf8_lossy(&output.stdout); // Get cmd output + let output_string = stdout.trim(); + let parts: Vec<&str> = output_string.split('\n').collect(); - 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), + if parts.len() == 2 { + Some((parts[0].to_string(), parts[1].to_string())) + } else { + None + } + } + _ => None, + } +} + +//Git branch indicator +pub fn indicator(branch: Option) -> ANSIGenericString<'static, str> { + match branch { + Some(b) => match b.as_str() { + "main" => MAIN_COL.paint(GIT_SYMBOL), + "dev" => DEV_COL.paint(GIT_SYMBOL), + _ => OTHER_COL.paint(GIT_SYMBOL), + }, + None => NORMIE_COL.paint(GIT_SYMBOL), } } diff --git a/src/indicators/ssh.rs b/src/indicators/ssh.rs index 49f86f9..ef4aae7 100644 --- a/src/indicators/ssh.rs +++ b/src/indicators/ssh.rs @@ -1,10 +1,21 @@ -use colored::{ColoredString, Colorize}; -use std::env::var_os; +use ansi_term::ANSIGenericString; +use ansi_term::Colour::RGB; +use std::env::var; + +// SSH indicator symbol +pub const SSH_SYMBOL: &str = "\u{276F}"; // "❯" +pub const SSH_COL: ansi_term::Colour = RGB(255, 149, 0); // A ind of orange +pub const NORMIE_COL: ansi_term::Colour = RGB(255, 255, 255); // White + +fn is_ssh_session() -> bool { + var("SSH_TTY").is_ok() || var("SSH_CONNECTION").is_ok() +} //SSH shell indicator -pub fn ssh_char() -> ColoredString { - match var_os("SSH_TTY") { - Some(_val) => " ".truecolor(0, 150, 180), - None => "".clear(), +pub fn indicator() -> ANSIGenericString<'static, str> { + if is_ssh_session() { + SSH_COL.paint(SSH_SYMBOL) + } else { + NORMIE_COL.paint(SSH_SYMBOL) } } diff --git a/src/main.rs b/src/main.rs index c425200..c4d3caf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,32 @@ mod indicators { + pub mod git; + pub mod ssh; pub mod user; } +use indicators::git; + +use crate::indicators::ssh; use crate::indicators::user; -fn main() -> std::io::Result<()> { - //let mut prompt: ANSIGenericString<'static, str> = "".into(); +fn main() { let mut prompt: String = String::new(); - - let indicate_user:bool = true; + let indicate_user: bool = true; + let indicate_ssh: bool = true; + let indicate_git_branch: bool = true; + + if indicate_git_branch { + let git_info = git::info(); + match git_info { + Some((_repo, branch)) => prompt += &git::indicator(Some(branch)).to_string(), + None => prompt += &git::indicator(None).to_string(), + }; + } + if indicate_ssh { + prompt += &ssh::indicator().to_string(); + } if indicate_user { prompt += &user::indicator().to_string(); } - print!("{}", prompt); - - Ok(()) + print!("{prompt} "); } diff --git a/src/indicators/error.rs b/src/old/error.rs similarity index 100% rename from src/indicators/error.rs rename to src/old/error.rs diff --git a/src/main.bkp.rs b/src/old/main.bkp.rs similarity index 100% rename from src/main.bkp.rs rename to src/old/main.bkp.rs diff --git a/src/indicators/path.rs b/src/old/path.rs similarity index 100% rename from src/indicators/path.rs rename to src/old/path.rs diff --git a/src/indicators/shell.rs b/src/old/shell.rs similarity index 100% rename from src/indicators/shell.rs rename to src/old/shell.rs