use colored::Colorize; use std::env; use whoami::{hostname, username}; #[derive(Clone)] pub enum PromptStyle { Default, SimpleDirectory, BashLike, } impl PromptStyle { pub fn fmt_style(&self) -> String { match self { PromptStyle::Default => self.fmt_default(), PromptStyle::SimpleDirectory => self.fmt_simple_directory(), PromptStyle::BashLike => self.fmt_bash_like(), } } fn fmt_default(&self) -> String { let current = env::current_dir().unwrap_or_default(); let home = dirs::home_dir().unwrap_or_default(); let dir = if current.starts_with(home.clone()) { let mut path = "~/".to_string(); path.push_str( current .as_path() .strip_prefix(home) .unwrap() .to_str() .unwrap_or_default(), ); path } else { current.into_os_string().into_string().unwrap_or_default() }; format!( "\n{}\n{} ", dir.bright_cyan().bold(), "»".bright_green().bold() ) } fn fmt_simple_directory(&self) -> String { let path = env::current_dir().unwrap_or_default(); format!("{} » ", path.to_string_lossy()) } fn fmt_bash_like(&self) -> String { let path = env::current_dir().unwrap_or_default(); let home = dirs::home_dir().unwrap_or_default(); let dir_as_string = if path == home { "~" } else { path.file_name() .unwrap_or_default() .to_str() .unwrap_or_default() }; format!("[{}@{} {}]$ ", username(), hostname(), dir_as_string) } } pub struct Prompt { pub style: PromptStyle, } impl Prompt { pub fn new() -> Self { Self { style: PromptStyle::Default, } } pub fn get_prompt(&mut self) -> String { self.style.fmt_style() } }