@@ -76,22 +76,29 @@ pub fn run(terms: Vec<String>, show_score: bool, show_all: bool, cwd_mode: bool) |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /// 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. |
| 79 | 81 | fn run_cwd_mode(matcher: &mut Matcher, terms: &[String], show_all: bool) -> Result<()> { |
| 80 | 82 | let cwd = std::env::current_dir()?; |
| 81 | 83 | |
| 82 | 84 | // 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)? |
| 84 | 87 | .filter_map(|entry| entry.ok()) |
| 85 | 88 | .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 | + }) |
| 87 | 94 | .collect(); |
| 88 | 95 | |
| 89 | 96 | if dirs.is_empty() { |
| 90 | 97 | std::process::exit(1); |
| 91 | 98 | } |
| 92 | 99 | |
| 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)); |
| 95 | 102 | |
| 96 | 103 | let matches = matcher.rank(paths, terms); |
| 97 | 104 | |
@@ -99,12 +106,20 @@ fn run_cwd_mode(matcher: &mut Matcher, terms: &[String], show_all: bool) -> Resu |
| 99 | 106 | std::process::exit(1); |
| 100 | 107 | } |
| 101 | 108 | |
| 109 | + // Find the full path for each matched name |
| 102 | 110 | if show_all { |
| 103 | 111 | 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 | + } |
| 105 | 116 | } |
| 106 | 117 | } 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 | + } |
| 108 | 123 | } |
| 109 | 124 | |
| 110 | 125 | Ok(()) |