src/prompt.rs (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
use crate::colours::{blue, green, yellow}; use std::env; use std::path::Path; use std::process::Command; pub fn generate_prompt() -> String { let current_dir = env::current_dir().unwrap_or_else(|_| Path::new("").to_path_buf()); let current_dir_str = current_dir.to_str().unwrap_or(""); let home_dir = env::var("HOME").unwrap_or_else(|_| String::from("/")); let display_dir = if current_dir_str.starts_with(&home_dir) { current_dir_str.replacen(&home_dir, "~", 1) } else { current_dir_str.to_string() }; let branch_name = if current_dir.join(".git").exists() { Command::new("git") .args(&["rev-parse", "--abbrev-ref", "HEAD"]) .output() .ok() .and_then(|output| { if output.status.success() { String::from_utf8(output.stdout) .ok() .map(|s| s.trim().to_string()) } else { None } }) .unwrap_or_else(|| "".to_string()) } else { "".to_string() }; format!( "{} {} {} ", blue(&display_dir), green(&branch_name), yellow("❯") ) } |