tenseleyflow/gump / 0e6435d

Browse files

fix: match CWD directory names only, not full paths

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
0e6435db696c18e3ec53b82f4b604a826cc0c9e8
Parents
c7ffa6a
Tree
238a2f0

1 changed file

StatusFile+-
M src/cmd/query.rs 21 6
src/cmd/query.rsmodified
@@ -76,22 +76,29 @@ pub fn run(terms: Vec<String>, show_score: bool, show_all: bool, cwd_mode: bool)
7676
 }
7777
 
7878
 /// Match against directories in the current working directory.
79
+/// Matches against directory NAMES only (not full paths) to avoid false positives
80
+/// from parent directory names appearing in the path.
7981
 fn run_cwd_mode(matcher: &mut Matcher, terms: &[String], show_all: bool) -> Result<()> {
8082
     let cwd = std::env::current_dir()?;
8183
 
8284
     // Read directory entries, filter to directories only
83
-    let dirs: Vec<PathBuf> = std::fs::read_dir(&cwd)?
85
+    // Store both the full path (for result) and just the name (for matching)
86
+    let dirs: Vec<(PathBuf, PathBuf)> = std::fs::read_dir(&cwd)?
8487
         .filter_map(|entry| entry.ok())
8588
         .filter(|entry| entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false))
86
-        .map(|entry| entry.path())
89
+        .map(|entry| {
90
+            let full_path = entry.path();
91
+            let name_only = PathBuf::from(entry.file_name());
92
+            (full_path, name_only)
93
+        })
8794
         .collect();
8895
 
8996
     if dirs.is_empty() {
9097
         std::process::exit(1);
9198
     }
9299
 
93
-    // Use equal frecency (1.0) for all CWD entries - fuzzy score determines ranking
94
-    let paths = dirs.iter().map(|p| (p.as_path(), 1.0));
100
+    // Match against directory NAMES only, not full paths
101
+    let paths = dirs.iter().map(|(_, name)| (name.as_path(), 1.0));
95102
 
96103
     let matches = matcher.rank(paths, terms);
97104
 
@@ -99,12 +106,20 @@ fn run_cwd_mode(matcher: &mut Matcher, terms: &[String], show_all: bool) -> Resu
99106
         std::process::exit(1);
100107
     }
101108
 
109
+    // Find the full path for each matched name
102110
     if show_all {
103111
         for m in &matches {
104
-            println!("{}", m.path.display());
112
+            // Find the full path that corresponds to this matched name
113
+            if let Some((full_path, _)) = dirs.iter().find(|(_, name)| name.as_path() == m.path) {
114
+                println!("{}", full_path.display());
115
+            }
105116
         }
106117
     } else {
107
-        println!("{}", matches[0].path.display());
118
+        // Find the full path for the best match
119
+        let matched_name = matches[0].path;
120
+        if let Some((full_path, _)) = dirs.iter().find(|(_, name)| name.as_path() == matched_name) {
121
+            println!("{}", full_path.display());
122
+        }
108123
     }
109124
 
110125
     Ok(())