PrettyPrompt/src/main.rs

136 lines
4.3 KiB
Rust
Raw Normal View History

2024-08-14 21:03:53 +00:00
use std::env::{current_dir,var_os,args};
2024-08-14 14:58:16 +00:00
use std::path::PathBuf;
2024-08-14 21:03:53 +00:00
use colored::{Colorize,ColoredString};
2024-08-15 15:05:40 +00:00
use std::process::Command;
//use std::ffi::OsString;
2024-08-14 14:58:16 +00:00
fn get_shell_char (shell: String) -> String {
let shell_char = match shell.as_str() {
"bash"|"/bin/bash"
2024-08-15 17:45:45 +00:00
=> "",
2024-08-14 20:40:03 +00:00
"zsh"|"/bin/zsh"|"/usr/bin/zsh"|"-zsh"
2024-08-15 17:45:45 +00:00
=> "󰰶 ",
2024-08-14 14:58:16 +00:00
"fish"
2024-08-15 17:45:45 +00:00
=> "󰈺 ",
2024-08-14 14:58:16 +00:00
"nushell"
2024-08-15 17:45:45 +00:00
=> "",
2024-08-14 14:58:16 +00:00
"ion"
2024-08-15 17:45:45 +00:00
=> "",
2024-08-14 14:58:16 +00:00
"oursh"
2024-08-15 17:45:45 +00:00
=> "󱢇 ",
2024-08-14 14:58:16 +00:00
_
2024-08-15 17:45:45 +00:00
=> "󱆃 ",
2024-08-14 14:58:16 +00:00
};
shell_char.to_string()
}
2024-08-15 17:45:45 +00:00
fn get_git_char (git_branch: String) -> ColoredString {
2024-08-17 11:18:19 +00:00
match git_branch.as_str() {
2024-08-15 17:45:45 +00:00
"main" => " 󰊢 ".truecolor(178,98,44),
"master" => " 󰊢 ".truecolor(196,132,29),
_ => " 󰊢 ".truecolor(82,82,82),
}
}
2024-08-14 14:58:16 +00:00
fn abrev_path (pwd: PathBuf, homedir: String) -> String {
let mut path = pwd.display().to_string();
path = path.replace(&homedir, "~"); // Abreviate homedir with "~"
let mut short_dir = path.clone();
let slashes = path.matches("/").count();
if slashes > 3 {
let parts: Vec<&str> = path.split("/").collect();
let len = parts.len() - 1;
2024-08-14 21:03:53 +00:00
let mut ch1: String;
2024-08-14 14:58:16 +00:00
2024-08-14 21:03:53 +00:00
for part in &parts[0..len] {
2024-08-14 20:40:03 +00:00
if part.to_string() != "" { // to avoid the 1st "/"
2024-08-14 21:03:53 +00:00
ch1 = part.chars().next().expect(part).to_string(); // 1st char of each part
short_dir = short_dir.replace(part, &ch1);
}
2024-08-14 14:58:16 +00:00
}
}
short_dir
}
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
let _user = var_os("USER").expect("UnknownUser").to_str().expect("UnknownUser").to_string();
2024-08-15 15:05:40 +00:00
/*
if user == "root" {
println!("roo_user");
}*/
//pwd
let pwd = current_dir()?;
2024-08-14 14:58:16 +00:00
let homedir = var_os("HOME").expect("UnknownDir").to_str().expect("UnknownDir").to_string();
2024-08-14 11:19:47 +00:00
2024-08-15 15:05:40 +00:00
//Shell symbol
let args: Vec<String> = args().collect();
let shell: String;
2024-08-13 21:49:45 +00:00
if args.len() > 1 {
shell = args[1].clone();
}
else {
shell = "none".to_string();
}
2024-08-15 15:05:40 +00:00
//SSH shell indicator
let ssh_char:ColoredString;
2024-08-14 20:40:03 +00:00
match var_os("SSH_TTY") {
Some(_val) => {
2024-08-15 15:05:40 +00:00
ssh_char = "".truecolor(0,150,180);
2024-08-14 20:40:03 +00:00
},
None => {
ssh_char = "".truecolor(34,109,155);
}
}
2024-08-15 15:05:40 +00:00
//Git status
2024-08-15 17:45:45 +00:00
let mut git_char = "".clear();
let mut git_repo_name = "".clear();
let mut git_branch = "".clear();
2024-08-15 15:05:40 +00:00
let git_status_cmd = Command::new("git")
.arg("status")
.output()
.expect("git_status_cmd_fail");
let git_status = String::from_utf8_lossy(&git_status_cmd.stdout);
let git_err = String::from_utf8_lossy(&git_status_cmd.stderr);
if git_err == "" {
git_branch = git_status.split("\n").collect::<Vec<&str>>()[0]
.split(" ").collect::<Vec<&str>>()[2]
.truecolor(82,82,82);
2024-08-17 11:18:19 +00:00
git_char = get_git_char(git_branch.to_string());
2024-08-15 15:05:40 +00:00
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);
let repo_path_split: Vec<&str> = git_repo_path.split("/").collect();
let last_index = repo_path_split.len() - 1;
git_repo_name = repo_path_split[last_index]
.truecolor(82,82,82);
}
}
2024-08-14 11:19:47 +00:00
2024-08-15 15:05:40 +00:00
print!("{}{}{}{} {}{} ",
2024-08-14 20:40:03 +00:00
ssh_char,
2024-08-14 14:58:16 +00:00
get_shell_char(shell).truecolor(75,75,75),
2024-08-15 15:05:40 +00:00
git_repo_name,
git_char,
2024-08-14 20:40:03 +00:00
abrev_path(pwd,homedir).italic().truecolor(82,82,82),
2024-08-13 21:49:45 +00:00
angle.truecolor(0, 255, 180),
2024-08-14 21:03:53 +00:00
);
2024-08-13 21:49:45 +00:00
Ok(())
2024-08-14 14:58:16 +00:00
}