path_utils.rs (615B)
1 use std::path::Path; 2 3 /// Abbreviate a path by replacing the given home directory prefix with ~ 4 pub fn abbreviate_with_home(path: &Path, home_dir: &str) -> String { 5 let home = Path::new(home_dir); 6 if let Ok(relative) = path.strip_prefix(home) { 7 return format!("~/{}", relative.display()); 8 } 9 path.display().to_string() 10 } 11 12 /// Abbreviate a path using the local machine's home directory 13 pub fn abbreviate_path(path: &Path) -> String { 14 if let Some(home) = dirs::home_dir() { 15 abbreviate_with_home(path, &home.to_string_lossy()) 16 } else { 17 path.display().to_string() 18 } 19 }