cargo fmt & corrections

This commit is contained in:
Candifloss 2024-08-19 18:54:12 +05:30
parent 22774c4e13
commit 89d909b512

View File

@ -1,23 +1,16 @@
use std::env::{current_dir,var_os,args}; use colored::{ColoredString, Colorize};
use colored::{Colorize,ColoredString}; use std::env::{args, current_dir, var_os};
use std::process::Command; use std::process::Command;
fn get_shell_char (shell: String) -> String { fn get_shell_char(shell: &str) -> String {
let shell_char = match shell.as_str() { let shell_char = match shell {
"bash" | "/bin/bash" | "/usr/bin/bash" | "-bash" "bash" | "/bin/bash" | "/usr/bin/bash" | "-bash" => "",
=> "", "zsh" | "/bin/zsh" | "/usr/bin/zsh" | "-zsh" => "󰰶 ",
"zsh" | "/bin/zsh" | "/usr/bin/zsh" | "-zsh" "fish" => "󰈺 ",
=> "󰰶 ", "nushell" => "",
"fish" "ion" => "",
=> "󰈺 ", "oursh" => "󱢇 ",
"nushell" _ => "󱆃 ",
=> "",
"ion"
=> "",
"oursh"
=> "󱢇 ",
_
=> "󱆃 ",
}; };
shell_char.to_string() shell_char.to_string()
} }
@ -31,11 +24,12 @@ fn get_git_branch () -> String {
let git_err = String::from_utf8_lossy(&git_status_cmd.stderr); let git_err = String::from_utf8_lossy(&git_status_cmd.stderr);
if git_err == "" { if git_err == "" {
git_status_output.split("\n").collect::<Vec<&str>>()[0] git_status_output.split('\n').collect::<Vec<&str>>()[0]
.split(" ").collect::<Vec<&str>>()[2].to_string() .split(' ')
} .collect::<Vec<&str>>()[2]
else { .to_string()
"".to_string() } else {
String::new()
} }
} }
@ -51,23 +45,22 @@ fn get_git_root () -> String {
if git_repo_err == "" { if git_repo_err == "" {
let len = git_repo_path.trim_end_matches(&['\r', '\n'][..]).len(); let len = git_repo_path.trim_end_matches(&['\r', '\n'][..]).len();
git_repo_path.truncate(len); git_repo_path.truncate(len);
} } else {
else { git_repo_path = String::new();
git_repo_path = "".to_string();
} }
git_repo_path git_repo_path
} }
fn get_git_repo_name (git_repo_root: String) -> String { fn get_git_repo_name(git_repo_root: &str) -> String {
let repo_path_split: Vec<&str> = git_repo_root.split("/").collect(); let repo_path_split: Vec<&str> = git_repo_root.split('/').collect();
let last_index = repo_path_split.len() - 1; let last_index = repo_path_split.len() - 1;
let git_repo_name = repo_path_split[last_index]; let git_repo_name = repo_path_split[last_index];
git_repo_name.to_string() git_repo_name.to_string()
} }
fn get_git_char (git_branch: String) -> ColoredString { fn get_git_char(git_branch: &str) -> ColoredString {
match git_branch.as_str() { match git_branch {
"" => "".clear(), "" => "".clear(),
"main" => " 󰊢 ".truecolor(178, 98, 44), "main" => " 󰊢 ".truecolor(178, 98, 44),
"master" => " 󰊢 ".truecolor(196, 132, 29), "master" => " 󰊢 ".truecolor(196, 132, 29),
@ -75,18 +68,19 @@ fn get_git_char (git_branch: String) -> ColoredString {
} }
} }
fn abrev_path (path: String) -> String { fn abrev_path(path: &str) -> String {
let mut short_dir = path.clone(); let mut short_dir = path.to_string();
let slashes = path.matches("/").count(); let slashes = path.matches('/').count();
if slashes > 3 { if slashes > 3 {
let parts: Vec<&str> = path.split("/").collect(); let parts: Vec<&str> = path.split('/').collect();
let len = parts.len() - 1; let len = parts.len() - 1;
let mut ch1: String; let mut ch1: String;
for part in &parts[0..len] { for part in &parts[0..len] {
if part.to_string() != "" { // to avoid the 1st "/" if part.to_string() != "" {
// to avoid the 1st "/"
ch1 = part.chars().next().expect(part).to_string(); // 1st char of each part ch1 = part.chars().next().expect(part).to_string(); // 1st char of each part
short_dir = short_dir.replace(part, &ch1); short_dir = short_dir.replace(part, &ch1);
} }
@ -99,19 +93,22 @@ fn main() -> std::io::Result<()> {
let angle = ""; let angle = "";
//Root user indicator //Root user indicator
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();
let mut err: String = "".to_string(); let mut err: String = String::new();
let args: Vec<String> = args().collect(); let args: Vec<String> = args().collect();
let shell: String; let shell: String;
if args.len() > 1 { if args.len() > 1 {
shell = args[1].clone(); // Shell symbol shell = args[1].clone(); // Shell symbol
if args.len() > 2 { if args.len() > 2 {
err = args[2].clone(); // Error status err.clone_from(&args[2]); // Error status
} }
} } else {
else {
shell = "none".to_string(); shell = "none".to_string();
} }
@ -126,33 +123,33 @@ fn main() -> std::io::Result<()> {
}; };
//SSH shell indicator //SSH shell indicator
let ssh_char:ColoredString; let ssh_char: ColoredString = match var_os("SSH_TTY") {
match var_os("SSH_TTY") { Some(_val) => "".truecolor(0, 150, 180),
Some(_val) => { None => "".clear(),
ssh_char = "".truecolor(0,150,180); };
},
None => {
ssh_char = "".clear();
}
}
//Git status //Git status
let git_branch = get_git_branch(); let git_branch = get_git_branch();
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.clone()).truecolor(122, 68, 24); let git_repo_name = get_git_repo_name(&git_repo_root.clone()).truecolor(122, 68, 24);
let git_char = get_git_char(git_branch); let git_char = get_git_char(&git_branch);
//pwd //pwd
let homedir = var_os("HOME").expect("UnknownDir").to_str().expect("UnknownDir").to_string(); let homedir = var_os("HOME")
.expect("UnknownDir")
.to_str()
.expect("UnknownDir")
.to_string();
let pwd = current_dir()?; let pwd = current_dir()?;
let mut cur_dir = pwd.display().to_string(); 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(&git_repo_root, ""); // Remove git repo root
cur_dir = cur_dir.replace(&homedir, "~"); // Abreviate homedir with "~" cur_dir = cur_dir.replace(&homedir, "~"); // Abreviate homedir with "~"
cur_dir = abrev_path(cur_dir); cur_dir = abrev_path(&cur_dir);
print!("{}{}{}{}{}{}{} ", 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,
cur_dir.italic().truecolor(82, 82, 82), cur_dir.italic().truecolor(82, 82, 82),