Skip to content

Commit f0c59be

Browse files
committed
fix
1 parent 630214d commit f0c59be

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pathmarks"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2024"
55
description = "Simple path bookmarks for your shell"
66
license = "MIT"

src/main.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ fn app(cli: Cli, bookmarks_file: PathBuf) -> AppResult<Option<String>> {
103103

104104
let bookmarks = read_bookmarks(&bookmarks_file)?;
105105

106-
if let Some((best, _score)) =
107-
best_bookmark_match(&path, bookmarks.iter().map(|s| s.as_str()), 100)
108-
{
106+
if let Some(best) = best_bookmark_match(&path, bookmarks.iter().map(|s| s.as_str())) {
109107
return Ok(Some(best.into()));
110108
}
111109

@@ -143,17 +141,22 @@ fn app(cli: Cli, bookmarks_file: PathBuf) -> AppResult<Option<String>> {
143141
fn best_bookmark_match<'a>(
144142
query: &str,
145143
bookmarks: impl IntoIterator<Item = &'a str>,
146-
min_score: u32,
147-
) -> Option<(&'a str, u32)> {
144+
) -> Option<&'a str> {
145+
let min_score = 100;
148146
let mut matcher = Matcher::new(Config::DEFAULT.match_paths());
149147

150-
let results = Pattern::parse(query, CaseMatching::Ignore, Normalization::Smart)
148+
let results = Pattern::parse(query, CaseMatching::Smart, Normalization::Smart)
151149
.match_list(bookmarks, &mut matcher);
152150

153151
results
154152
.into_iter()
155-
.max_by_key(|(_, score)| *score)
156153
.filter(|(_, score)| *score >= min_score)
154+
.max_by(|(a_str, a_score), (b_str, b_score)| {
155+
a_score
156+
.cmp(b_score)
157+
.then_with(|| b_str.len().cmp(&a_str.len()))
158+
})
159+
.map(|(s, _)| s)
157160
}
158161

159162
fn find_case_insensitive(name: &str) -> Option<PathBuf> {
@@ -219,3 +222,21 @@ fn pick_one(bookmarks: &[String]) -> AppResult<Option<String>> {
219222
}
220223
Ok(picker.pick()?.map(|bookmark| bookmark.to_string()))
221224
}
225+
226+
#[cfg(test)]
227+
mod tests {
228+
use super::*;
229+
230+
#[test]
231+
fn best_with_same_score() {
232+
let paths = [
233+
"/path/with/many/sub/directories",
234+
"/path/with/",
235+
"/path/with/many/sub/",
236+
];
237+
238+
let best = best_bookmark_match("pathwith", paths).unwrap();
239+
240+
assert_eq!(best, paths[1]);
241+
}
242+
}

0 commit comments

Comments
 (0)