Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

fix(gui): shorten home paths only at a path component

RoversX0fb60ec3830f
diff --git a/wezterm-gui/src/termwindow/ui/command_palette.rs b/wezterm-gui/src/termwindow/ui/command_palette.rs
index 29d2a84e..c0ed6514 100644
--- a/wezterm-gui/src/termwindow/ui/command_palette.rs
+++ b/wezterm-gui/src/termwindow/ui/command_palette.rs
@@ -828,16 +828,6 @@ fn thread_icon_name(entry: &ThreadSearchEntry) -> String {
format!("tt_thread:{work}:{tone}")
}
-/// `path` with the home directory folded back to `~`, for the header's
-/// right-hand detail. Three different projects are called "Home".
-fn abbreviate_home(path: &str) -> String {
- let home = config::HOME_DIR.to_string_lossy().to_string();
- match path.strip_prefix(&home) {
- Some(rest) if !home.is_empty() => format!("~{rest}"),
- _ => path.to_string(),
- }
-}
-
/// The searchable part of a project path. Matching the whole thing would
/// let "users" or "documents" hit every local project; the tail names it,
/// and for a remote root the user and host do.
@@ -1440,7 +1430,9 @@ impl crate::TermWindow {
if open_project.as_ref() != Some(&project) {
items.push(PaletteItem::Header(PaletteHeader {
label: entry.project_name.clone(),
- detail: abbreviate_home(&entry.project_path),
+ // The path with home folded back to `~`: three
+ // different projects are called "Home".
+ detail: crate::ui::home_relative(std::path::Path::new(&entry.project_path)),
}));
haystacks.push(String::new());
open_project = Some(project);
diff --git a/wezterm-gui/src/ui/mod.rs b/wezterm-gui/src/ui/mod.rs
index 8c1747dd..03d6fb5a 100644
--- a/wezterm-gui/src/ui/mod.rs
+++ b/wezterm-gui/src/ui/mod.rs
@@ -22,3 +22,41 @@ pub(crate) use primitives::*;
pub(crate) use state::*;
pub(crate) use tokens::*;
pub(crate) use widgets::*;
+
+use std::path::Path;
+
+/// `path` with the home directory written `~`, as people say it.
+pub(crate) fn home_relative(path: &Path) -> String {
+ home_relative_to(path, &config::HOME_DIR)
+}
+
+/// Compared a component at a time, so `/home/user` never shortens
+/// `/home/username`.
+fn home_relative_to(path: &Path, home: &Path) -> String {
+ match path.strip_prefix(home) {
+ Ok(rest) if rest.as_os_str().is_empty() => "~".to_string(),
+ Ok(rest) => format!("~{}{}", std::path::MAIN_SEPARATOR, rest.display()),
+ Err(_) => path.display().to_string(),
+ }
+}
+
+#[cfg(all(test, unix))]
+mod tests {
+ use super::home_relative_to;
+ use std::path::Path;
+
+ #[test]
+ fn home_is_shortened_a_whole_component_at_a_time() {
+ let home = Path::new("/home/user");
+ assert_eq!(home_relative_to(Path::new("/home/user"), home), "~");
+ assert_eq!(
+ home_relative_to(Path::new("/home/user/projects"), home),
+ "~/projects"
+ );
+ assert_eq!(
+ home_relative_to(Path::new("/home/username/projects"), home),
+ "/home/username/projects"
+ );
+ assert_eq!(home_relative_to(Path::new("/srv/data"), home), "/srv/data");
+ }
+}