Git repo name & path abrev

This commit is contained in:
Candifloss 2024-08-17 23:57:14 +05:30
parent 27b0a77202
commit e43fd9d7a1

View File

@ -1,7 +1,7 @@
use std::env::{current_dir,var_os,args}; use std::env::{current_dir,var_os,args};
use std::path::PathBuf;
use colored::{Colorize,ColoredString}; use colored::{Colorize,ColoredString};
use std::process::Command; use std::process::Command;
//use std::path::PathBuf;
//use std::ffi::OsString; //use std::ffi::OsString;
fn get_shell_char (shell: String) -> String { fn get_shell_char (shell: String) -> String {
@ -71,15 +71,14 @@ fn get_git_repo_name (git_repo_root: String) -> String {
fn get_git_char (git_branch: String) -> ColoredString { fn get_git_char (git_branch: String) -> ColoredString {
match git_branch.as_str() { match git_branch.as_str() {
"" => "".clear(),
"main" => " 󰊢 ".truecolor(178,98,44), "main" => " 󰊢 ".truecolor(178,98,44),
"master" => " 󰊢 ".truecolor(196,132,29), "master" => " 󰊢 ".truecolor(196,132,29),
_ => " 󰊢 ".truecolor(82,82,82), _ => " 󰊢 ".truecolor(82,82,82),
} }
} }
fn abrev_path (pwd: PathBuf, homedir: String) -> String { fn abrev_path (path: String) -> String {
let mut path = pwd.display().to_string();
path = path.replace(&homedir, "~"); // Abreviate homedir with "~"
let mut short_dir = path.clone(); let mut short_dir = path.clone();
let slashes = path.matches("/").count(); let slashes = path.matches("/").count();
@ -106,13 +105,9 @@ fn main() -> std::io::Result<()> {
let _user = var_os("USER").expect("UnknownUser").to_str().expect("UnknownUser").to_string(); let _user = var_os("USER").expect("UnknownUser").to_str().expect("UnknownUser").to_string();
/* /*
if user == "root" { if user == "root" {
println!("roo_user"); println!("root_user");
}*/ }*/
//pwd
let pwd = current_dir()?;
let homedir = var_os("HOME").expect("UnknownDir").to_str().expect("UnknownDir").to_string();
//Shell symbol //Shell symbol
let args: Vec<String> = args().collect(); let args: Vec<String> = args().collect();
let shell: String; let shell: String;
@ -137,15 +132,24 @@ fn main() -> std::io::Result<()> {
//Git status //Git status
let git_branch = get_git_branch();//.clear(); let git_branch = get_git_branch();//.clear();
let git_repo_root = get_git_root(); let git_repo_root = get_git_root();
let git_repo_name = get_git_repo_name(git_repo_root); let git_repo_name = get_git_repo_name(git_repo_root.clone()).truecolor(200, 150, 80);
let git_char = get_git_char(git_branch); //.to_string()); let git_char = get_git_char(git_branch); //.to_string());
print!("{}{}{}{} {}{} ", //pwd
let homedir = var_os("HOME").expect("UnknownDir").to_str().expect("UnknownDir").to_string();
let pwd = current_dir()?;
let mut cur_dir = pwd.display().to_string();
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, ssh_char,
get_shell_char(shell).truecolor(75,75,75), get_shell_char(shell).truecolor(75,75,75),
git_repo_name, git_repo_name,
git_char, git_char,
abrev_path(pwd,homedir).italic().truecolor(82,82,82), //abrev_path(pwd,homedir).italic().truecolor(82,82,82),
cur_dir.italic().truecolor(82,82,82),
angle.truecolor(0, 255, 180), angle.truecolor(0, 255, 180),
); );