Print git repo name

This commit is contained in:
Candifloss 2024-08-15 20:35:40 +05:30
parent bf36aaaf6f
commit bbcd54dd92

View File

@ -1,24 +1,25 @@
use std::env::{current_dir,var_os,args}; use std::env::{current_dir,var_os,args};
use std::path::PathBuf; use std::path::PathBuf;
use colored::{Colorize,ColoredString}; use colored::{Colorize,ColoredString};
use std::process::Command;
//use std::ffi::OsString; //use std::ffi::OsString;
fn get_shell_char (shell: String) -> String { fn get_shell_char (shell: String) -> String {
let shell_char = match shell.as_str() { let shell_char = match shell.as_str() {
"bash"|"/bin/bash" "bash"|"/bin/bash"
=> "", => " ",
"zsh"|"/bin/zsh"|"/usr/bin/zsh"|"-zsh" "zsh"|"/bin/zsh"|"/usr/bin/zsh"|"-zsh"
=> "󰰶", => "󰰶 ",
"fish" "fish"
=> "󰈺", => "󰈺 ",
"nushell" "nushell"
=> "", => " ",
"ion" "ion"
=> "", => " ",
"oursh" "oursh"
=> "󱢇", => "󱢇 ",
_ _
=> "󱆃", => "󱆃 ",
}; };
shell_char.to_string() shell_char.to_string()
} }
@ -47,38 +48,87 @@ fn abrev_path (pwd: PathBuf, homedir: String) -> String {
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let angle = ""; let angle = "";
//Root user indicator
let _user = var_os("USER").expect("UnknownUser").to_str().expect("UnknownUser").to_string();
/*
if user == "root" {
println!("roo_user");
}*/
//pwd
let pwd = current_dir()?; let pwd = current_dir()?;
let homedir = var_os("HOME").expect("UnknownDir").to_str().expect("UnknownDir").to_string();
//Shell symbol
let args: Vec<String> = args().collect(); let args: Vec<String> = args().collect();
let shell: String; let shell: String;
let _user = var_os("USER").expect("UnknownUser").to_str().expect("UnknownUser").to_string();
let homedir = var_os("HOME").expect("UnknownDir").to_str().expect("UnknownDir").to_string();
let ssh_char:ColoredString;
let mut ssh_char_space: String = "".to_string();
if args.len() > 1 { if args.len() > 1 {
shell = args[1].clone(); shell = args[1].clone();
} }
else { else {
shell = "none".to_string(); shell = "none".to_string();
} }
/*
if user == "root" { //SSH shell indicator
println!("heh"); let ssh_char:ColoredString;
}*/ //let mut ssh_char_space: String = "".to_string();
match var_os("SSH_TTY") { match var_os("SSH_TTY") {
Some(_val) => { Some(_val) => {
ssh_char = "".on_truecolor(0,150,180).truecolor(20,20,20); ssh_char = " ".truecolor(0,150,180);
ssh_char_space = " ";//.to_string(); //ssh_char_space = " ".to_string();
}, },
None => { None => {
ssh_char = "".truecolor(34,109,155); ssh_char = "".truecolor(34,109,155);
} }
} }
//Git status
let mut git_char = "".truecolor(82,82,82);
let mut git_repo_name = "".truecolor(82,82,82);
let mut git_branch = "".truecolor(82,82,82);
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);
git_char = match git_branch.to_string().as_str() {
"main" => " 󰊢 ".truecolor(178,98,44),
"master" => " 󰊢 ".truecolor(196,132,29),
_ => " 󰊢 ".truecolor(82,82,82),
};
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);
//println!("LastInd:{}",last_index);
}
//println!("git_repo_path:{}\ngit_repo_err:{}",git_repo_path,git_repo_err);
}
//println!("git_repo_name:{}\ngit_branch:{}",git_repo_name,git_branch);
print!("{}{}{} {}{} ", print!("{}{}{}{} {}{} ",
ssh_char, ssh_char,
ssh_char_space, //ssh_char_space,
get_shell_char(shell).truecolor(75,75,75), get_shell_char(shell).truecolor(75,75,75),
git_repo_name,
git_char,
abrev_path(pwd,homedir).italic().truecolor(82,82,82), abrev_path(pwd,homedir).italic().truecolor(82,82,82),
angle.truecolor(0, 255, 180), angle.truecolor(0, 255, 180),
); );