use colored::{ColoredString, Colorize}; use std::env::{args, current_dir, var_os}; mod git; mod ssh; mod user; use crate::git::*; use crate::ssh::*; use crate::user::*; 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" => "󱢇 ", _ => "󱆃 ", }; shell_char.to_string() } fn abrev_path(path: &str) -> String { let mut short_dir = path.to_string(); let slashes = path.matches('/').count(); if slashes > 3 { 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 short_dir = short_dir.replace(part, &ch1); } } } short_dir } fn main() -> std::io::Result<()> { let angle = "❯"; let mut err: String = String::new(); let args: Vec = args().collect(); let shell: String; if args.len() > 1 { shell = args[1].clone(); // Shell symbol if args.len() > 2 { err.clone_from(&args[2]); // Error status } } else { shell = "none".to_string(); } let root_indicator = root_indicator(); let err_indicator = match err.as_str() { "0" => angle.truecolor(0, 255, 180), _ => angle.truecolor(255, 53, 94), }; // SSH status let ssh_char = ssh_char(); //Git status let git_branch = get_git_branch(); 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_char = get_git_char(&git_branch); //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, get_shell_char(&shell).truecolor(75, 75, 75), git_repo_name, git_char, cur_dir.italic().truecolor(82, 82, 82), root_indicator, err_indicator, ); Ok(()) }