diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2023-11-10 17:24:08 -0500 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2023-11-10 17:24:08 -0500 |
commit | 52e581e6b1bd912032a5b2210ef3006a5b75e5e5 (patch) | |
tree | dc34f11cb33c0050019f0a402a373263373032d2 | |
parent | 9a75f05a825e68e5295abf46e76d1810ee6e4deb (diff) |
only replace home dir with tilde if it starts from the root
-rw-r--r-- | src/ui/tab_list_builder.rs | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/ui/tab_list_builder.rs b/src/ui/tab_list_builder.rs index 3ce1d00..d32684f 100644 --- a/src/ui/tab_list_builder.rs +++ b/src/ui/tab_list_builder.rs @@ -35,10 +35,15 @@ pub struct TabLabel { impl TabLabel { fn from_path(path: &Path) -> TabLabel { - let mut full_path_str = path.as_os_str().to_str().unwrap().to_string(); + let mut full_path_str = path.as_os_str() + .to_str() + .unwrap_or_default() + .to_string(); if let Some(home_dir) = HOME_DIR.as_ref() { let home_dir_str = home_dir.to_string_lossy().into_owned(); - full_path_str = full_path_str.replace(&home_dir_str, "~"); + if full_path_str.starts_with(&home_dir_str) { + full_path_str = full_path_str.replacen(&home_dir_str, "~", 1); + } } // eprintln!("full_path_str: {:?}", full_path_str); let last = Path::new(&full_path_str) |