migrated user, git, & ssh indicators to ansi_term

This commit is contained in:
Candifloss 2024-11-24 19:53:15 +05:30
parent b823586d03
commit 24f3f6772e
7 changed files with 76 additions and 61 deletions

View File

@ -1,55 +1,45 @@
use colored::{ColoredString, Colorize}; use ansi_term::ANSIGenericString;
use ansi_term::Colour::RGB;
use std::process::Command; use std::process::Command;
pub fn get_git_branch() -> String { // SSH indicator symbol
let git_status_cmd = Command::new("git") pub const GIT_SYMBOL: &str = "\u{276F}"; // ""
.arg("status") pub const MAIN_COL: ansi_term::Colour = RGB(178, 98, 44); //
.output() pub const DEV_COL: ansi_term::Colour = RGB(54, 159, 150); //
.expect("git_status_cmd_fail"); pub const OTHER_COL: ansi_term::Colour = RGB(82, 82, 82); //
let git_status_output = String::from_utf8_lossy(&git_status_cmd.stdout); pub const NORMIE_COL: ansi_term::Colour = RGB(255, 255, 255); // White
let git_err = String::from_utf8_lossy(&git_status_cmd.stderr);
if git_err == "" { pub fn info() -> Option<(String, String)> {
git_status_output.split('\n').collect::<Vec<&str>>()[0] let output = Command::new("git")
.split(' ')
.collect::<Vec<&str>>()[2]
.to_string()
} else {
String::new()
}
}
pub fn get_git_root() -> String {
let git_repo_root_cmd = Command::new("git")
.arg("rev-parse") .arg("rev-parse")
.arg("--show-toplevel") .arg("--show-toplevel") // Repo root
.output() .arg("--abbrev-ref") // Git branch
.expect("git_repo_root_cmd_fail"); .arg("HEAD")
let mut git_repo_path = String::from_utf8_lossy(&git_repo_root_cmd.stdout).to_string(); .output();
let git_repo_err = String::from_utf8_lossy(&git_repo_root_cmd.stderr); 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 == "" { if parts.len() == 2 {
let len = git_repo_path.trim_end_matches(&['\r', '\n'][..]).len(); Some((parts[0].to_string(), parts[1].to_string()))
git_repo_path.truncate(len); } else {
} else { None
git_repo_path = String::new(); }
} }
git_repo_path _ => None,
} }
}
pub fn get_git_repo_name(git_repo_root: &str) -> String {
let repo_path_split: Vec<&str> = git_repo_root.split('/').collect(); //Git branch indicator
let last_index = repo_path_split.len() - 1; pub fn indicator(branch: Option<String>) -> ANSIGenericString<'static, str> {
let git_repo_name = repo_path_split[last_index]; match branch {
Some(b) => match b.as_str() {
git_repo_name.to_string() "main" => MAIN_COL.paint(GIT_SYMBOL),
} "dev" => DEV_COL.paint(GIT_SYMBOL),
_ => OTHER_COL.paint(GIT_SYMBOL),
pub fn get_git_char(git_branch: &str) -> ColoredString { },
match git_branch { None => NORMIE_COL.paint(GIT_SYMBOL),
"" => "".clear(),
"main" => " 󰊢 ".truecolor(178, 98, 44),
"master" => " 󰊢 ".truecolor(196, 132, 29),
_ => " 󰊢 ".truecolor(82, 82, 82),
} }
} }

View File

@ -1,10 +1,21 @@
use colored::{ColoredString, Colorize}; use ansi_term::ANSIGenericString;
use std::env::var_os; 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 //SSH shell indicator
pub fn ssh_char() -> ColoredString { pub fn indicator() -> ANSIGenericString<'static, str> {
match var_os("SSH_TTY") { if is_ssh_session() {
Some(_val) => "".truecolor(0, 150, 180), SSH_COL.paint(SSH_SYMBOL)
None => "".clear(), } else {
NORMIE_COL.paint(SSH_SYMBOL)
} }
} }

View File

@ -1,18 +1,32 @@
mod indicators { mod indicators {
pub mod git;
pub mod ssh;
pub mod user; pub mod user;
} }
use indicators::git;
use crate::indicators::ssh;
use crate::indicators::user; use crate::indicators::user;
fn main() -> std::io::Result<()> { fn main() {
//let mut prompt: ANSIGenericString<'static, str> = "".into();
let mut prompt: String = String::new(); 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 { if indicate_user {
prompt += &user::indicator().to_string(); prompt += &user::indicator().to_string();
} }
print!("{}", prompt); print!("{prompt} ");
Ok(())
} }