mod user; pub struct Prompt { pub ps1: std::string::String, pub home: std::path::PathBuf, } impl Prompt { pub fn new() -> Result { let username = user::username()?; let hostname = user::hostname()?; let ready = if username == "root" { "#" } else { "$" }.to_owned(); let ps1 = match std::env::var("PROMPT") { Ok(val) => val, Err(_) => String::from(" {USER}@{HOST} [{PWD}]\n{$} "), } .replace("{USER}", &username) .replace("{HOST}", &hostname) .replace("{$}", &ready); let home = match std::env::var("HOME") { Ok(val) => std::path::Path::new(&val).to_path_buf(), Err(_) => std::path::Path::new("/").to_path_buf(), }; Ok(Prompt { ps1, home }) } pub fn print(&self) -> std::string::String { let pwd = std::env::current_dir().unwrap(); let path = if pwd.starts_with(&self.home) { "~".to_owned() + &pwd.strip_prefix(&self.home).unwrap().display().to_string() } else { pwd.display().to_string() }; self.ps1.replace("{PWD}", &path) } }