2024-08-19 13:24:12 +00:00
|
|
|
|
use colored::{ColoredString, Colorize};
|
|
|
|
|
use std::env::{args, current_dir, var_os};
|
2024-08-15 15:05:40 +00:00
|
|
|
|
use std::process::Command;
|
2024-08-14 14:58:16 +00:00
|
|
|
|
|
2024-08-19 13:24:12 +00:00
|
|
|
|
fn get_shell_char(shell: &str) -> String {
|
|
|
|
|
let shell_char = match shell {
|
|
|
|
|
"bash" | "/bin/bash" | "/usr/bin/bash" | "-bash" => " ",
|
|
|
|
|
"zsh" | "/bin/zsh" | "/usr/bin/zsh" | "-zsh" => " ",
|
|
|
|
|
"fish" => " ",
|
|
|
|
|
"nushell" => " ",
|
|
|
|
|
"ion" => " ",
|
|
|
|
|
"oursh" => " ",
|
|
|
|
|
_ => " ",
|
2024-08-14 14:58:16 +00:00
|
|
|
|
};
|
|
|
|
|
shell_char.to_string()
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 13:24:12 +00:00
|
|
|
|
fn get_git_branch() -> String {
|
|
|
|
|
let git_status_cmd = Command::new("git")
|
|
|
|
|
.arg("status")
|
|
|
|
|
.output()
|
|
|
|
|
.expect("git_status_cmd_fail");
|
2024-08-17 16:00:08 +00:00
|
|
|
|
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 == "" {
|
2024-08-19 13:24:12 +00:00
|
|
|
|
git_status_output.split('\n').collect::<Vec<&str>>()[0]
|
|
|
|
|
.split(' ')
|
|
|
|
|
.collect::<Vec<&str>>()[2]
|
|
|
|
|
.to_string()
|
|
|
|
|
} else {
|
|
|
|
|
String::new()
|
2024-08-17 16:00:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 13:24:12 +00:00
|
|
|
|
fn get_git_root() -> String {
|
2024-08-17 16:00:08 +00:00
|
|
|
|
let git_repo_root_cmd = Command::new("git")
|
2024-08-19 13:24:12 +00:00
|
|
|
|
.arg("rev-parse")
|
|
|
|
|
.arg("--show-toplevel")
|
|
|
|
|
.output()
|
|
|
|
|
.expect("git_repo_root_cmd_fail");
|
2024-08-17 16:00:08 +00:00
|
|
|
|
let mut git_repo_path = String::from_utf8_lossy(&git_repo_root_cmd.stdout).to_string();
|
2024-08-19 13:24:12 +00:00
|
|
|
|
let git_repo_err = String::from_utf8_lossy(&git_repo_root_cmd.stderr);
|
2024-08-17 16:00:08 +00:00
|
|
|
|
|
|
|
|
|
if git_repo_err == "" {
|
|
|
|
|
let len = git_repo_path.trim_end_matches(&['\r', '\n'][..]).len();
|
|
|
|
|
git_repo_path.truncate(len);
|
2024-08-19 13:24:12 +00:00
|
|
|
|
} else {
|
|
|
|
|
git_repo_path = String::new();
|
2024-08-17 16:00:08 +00:00
|
|
|
|
}
|
|
|
|
|
git_repo_path
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 13:24:12 +00:00
|
|
|
|
fn get_git_repo_name(git_repo_root: &str) -> String {
|
|
|
|
|
let repo_path_split: Vec<&str> = git_repo_root.split('/').collect();
|
2024-08-17 16:00:08 +00:00
|
|
|
|
let last_index = repo_path_split.len() - 1;
|
2024-08-18 03:57:36 +00:00
|
|
|
|
let git_repo_name = repo_path_split[last_index];
|
2024-08-17 16:00:08 +00:00
|
|
|
|
|
|
|
|
|
git_repo_name.to_string()
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 13:24:12 +00:00
|
|
|
|
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),
|
2024-08-15 17:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 13:24:12 +00:00
|
|
|
|
fn abrev_path(path: &str) -> String {
|
|
|
|
|
let mut short_dir = path.to_string();
|
2024-08-14 14:58:16 +00:00
|
|
|
|
|
2024-08-19 13:24:12 +00:00
|
|
|
|
let slashes = path.matches('/').count();
|
2024-08-14 14:58:16 +00:00
|
|
|
|
|
|
|
|
|
if slashes > 3 {
|
2024-08-19 13:24:12 +00:00
|
|
|
|
let parts: Vec<&str> = path.split('/').collect();
|
|
|
|
|
let len = parts.len() - 1;
|
|
|
|
|
let mut ch1: String;
|
|
|
|
|
|
|
|
|
|
for part in &parts[0..len] {
|
|
|
|
|
if part.to_string() != "" {
|
|
|
|
|
// to avoid the 1st "/"
|
|
|
|
|
ch1 = part.chars().next().expect(part).to_string(); // 1st char of each part
|
2024-08-14 21:03:53 +00:00
|
|
|
|
short_dir = short_dir.replace(part, &ch1);
|
2024-08-19 13:24:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
short_dir
|
2024-08-14 14:58:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 21:49:45 +00:00
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
|
|
|
let angle = "❯";
|
2024-08-15 15:05:40 +00:00
|
|
|
|
|
|
|
|
|
//Root user indicator
|
2024-08-19 13:24:12 +00:00
|
|
|
|
let user = var_os("USER")
|
|
|
|
|
.expect("UnknownUser")
|
|
|
|
|
.to_str()
|
|
|
|
|
.expect("UnknownUser")
|
|
|
|
|
.to_string();
|
2024-08-18 03:05:00 +00:00
|
|
|
|
|
2024-08-19 13:24:12 +00:00
|
|
|
|
let mut err: String = String::new();
|
2024-08-15 15:05:40 +00:00
|
|
|
|
|
|
|
|
|
let args: Vec<String> = args().collect();
|
|
|
|
|
let shell: String;
|
2024-08-13 21:49:45 +00:00
|
|
|
|
if args.len() > 1 {
|
2024-08-19 13:24:12 +00:00
|
|
|
|
shell = args[1].clone(); // Shell symbol
|
2024-08-18 03:05:00 +00:00
|
|
|
|
if args.len() > 2 {
|
2024-08-19 13:24:12 +00:00
|
|
|
|
err.clone_from(&args[2]); // Error status
|
2024-08-18 03:05:00 +00:00
|
|
|
|
}
|
2024-08-19 13:24:12 +00:00
|
|
|
|
} else {
|
2024-08-13 21:49:45 +00:00
|
|
|
|
shell = "none".to_string();
|
|
|
|
|
}
|
2024-08-15 15:05:40 +00:00
|
|
|
|
|
2024-08-18 03:05:00 +00:00
|
|
|
|
let root_indicator = match user.as_str() {
|
|
|
|
|
"root" => angle.truecolor(255, 53, 94),
|
|
|
|
|
_ => angle.truecolor(0, 255, 180),
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-18 03:57:36 +00:00
|
|
|
|
let err_indicator = match err.as_str() {
|
2024-08-18 03:05:00 +00:00
|
|
|
|
"0" => angle.truecolor(0, 255, 180),
|
|
|
|
|
_ => angle.truecolor(255, 53, 94),
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-15 15:05:40 +00:00
|
|
|
|
//SSH shell indicator
|
2024-08-19 13:24:12 +00:00
|
|
|
|
let ssh_char: ColoredString = match var_os("SSH_TTY") {
|
|
|
|
|
Some(_val) => " ".truecolor(0, 150, 180),
|
|
|
|
|
None => "".clear(),
|
|
|
|
|
};
|
2024-08-15 15:05:40 +00:00
|
|
|
|
|
|
|
|
|
//Git status
|
2024-08-19 13:24:12 +00:00
|
|
|
|
let git_branch = get_git_branch();
|
2024-08-17 16:00:08 +00:00
|
|
|
|
let git_repo_root = get_git_root();
|
2024-08-19 13:24:12 +00:00
|
|
|
|
let git_repo_name = get_git_repo_name(&git_repo_root.clone()).truecolor(122, 68, 24);
|
|
|
|
|
let git_char = get_git_char(&git_branch);
|
2024-08-17 18:27:14 +00:00
|
|
|
|
|
|
|
|
|
//pwd
|
2024-08-19 13:24:12 +00:00
|
|
|
|
let homedir = var_os("HOME")
|
|
|
|
|
.expect("UnknownDir")
|
|
|
|
|
.to_str()
|
|
|
|
|
.expect("UnknownDir")
|
|
|
|
|
.to_string();
|
2024-08-17 18:27:14 +00:00
|
|
|
|
let pwd = current_dir()?;
|
|
|
|
|
let mut cur_dir = pwd.display().to_string();
|
2024-08-19 13:24:12 +00:00
|
|
|
|
cur_dir = cur_dir.replace(&git_repo_root, ""); // Remove git repo root
|
|
|
|
|
cur_dir = cur_dir.replace(&homedir, "~"); // Abreviate homedir with "~"
|
|
|
|
|
cur_dir = abrev_path(&cur_dir);
|
|
|
|
|
|
|
|
|
|
print!(
|
|
|
|
|
"{}{}{}{}{}{}{} ",
|
|
|
|
|
ssh_char,
|
|
|
|
|
get_shell_char(&shell).truecolor(75, 75, 75),
|
|
|
|
|
git_repo_name,
|
|
|
|
|
git_char,
|
|
|
|
|
cur_dir.italic().truecolor(82, 82, 82),
|
|
|
|
|
root_indicator,
|
|
|
|
|
err_indicator,
|
2024-08-14 21:03:53 +00:00
|
|
|
|
);
|
|
|
|
|
|
2024-08-13 21:49:45 +00:00
|
|
|
|
Ok(())
|
2024-08-19 13:24:12 +00:00
|
|
|
|
}
|