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;
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::<Vec<&str>>()[0]
.split(' ')
.collect::<Vec<&str>>()[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);
if parts.len() == 2 {
Some((parts[0].to_string(), parts[1].to_string()))
} else {
git_repo_path = String::new();
None
}
}
_ => None,
}
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),
//Git branch indicator
pub fn indicator(branch: Option<String>) -> 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),
}
}

View File

@ -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)
}
}

View File

@ -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_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} ");
}