From 99ad6be5f22b77da3f32a6be48b08e06c1885418 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Fri, 30 Jan 2026 23:10:20 -0500 Subject: [PATCH] Fix TUI bug when passed a relative path `../` --- Cargo.lock | 1 + Cargo.toml | 1 + src/tui/explorer.rs | 25 ++++++++++--------------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 063f626..c2b50d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -303,6 +303,7 @@ dependencies = [ "syntect", "tui-logger", "tui-tree-widget", + "uuid", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c9b44f5..fe5a0fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ tui-tree-widget = "0.24.0" tui-logger = "0.18.1" edtui = "0.11.1" strum = "0.27.2" +uuid = { version = "1.19.0", features = ["v4"] } [build-dependencies] # The link_qt_object_files feature is required for statically linking Qt 6. diff --git a/src/tui/explorer.rs b/src/tui/explorer.rs index a8b4c6c..fbd2063 100644 --- a/src/tui/explorer.rs +++ b/src/tui/explorer.rs @@ -39,23 +39,24 @@ impl<'a> Explorer<'a> { fn build_tree_from_path(path: PathBuf) -> Result> { let mut children = vec![]; - if let Ok(entries) = fs::read_dir(&path) { + let clean_path = fs::canonicalize(path)?; + if let Ok(entries) = fs::read_dir(&clean_path) { let mut paths = entries .map(|res| res.map(|e| e.path())) .collect::, std::io::Error>>() .context(format!( "Failed to build vector of paths under directory: {:?}", - path + clean_path ))?; paths.sort(); for path in paths { if path.is_dir() { children.push(Self::build_tree_from_path(path)?); } else { - if let Ok(path) = std::path::absolute(&path) { + if let Ok(path) = fs::canonicalize(&path) { let path_str = path.to_string_lossy().to_string(); children.push(TreeItem::new_leaf( - path_str, + path_str + uuid::Uuid::new_v4().to_string().as_str(), path.file_name() .context("Failed to get file name from path.")? .to_string_lossy() @@ -66,22 +67,16 @@ impl<'a> Explorer<'a> { } } - let abs = std::path::absolute(&path) - .context(format!( - "Failed to find absolute path for TreeItem: {:?}", - path - ))? - .to_string_lossy() - .to_string(); TreeItem::new( - abs, - path.file_name() - .expect("Failed to get file name from path.") + clean_path.to_string_lossy().to_string() + uuid::Uuid::new_v4().to_string().as_str(), + clean_path + .file_name() + .context(format!("Failed to get file name from path: {clean_path:?}"))? .to_string_lossy() .to_string(), children, ) - .context("Failed to build tree from path.") + .context(format!("Failed to build tree from path: {clean_path:?}")) } pub fn selected(&self) -> Result {