Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ keywords = ["cli", "tool", "utility"]
clap = { version = "4.6.0", features = ["derive"] }
ctrlc = { version = "3.5.2", features = ["termination"] }
dialoguer = { version = "0.12.0", default-features = false }
fuzzy_dir = "0.2.0"
upon = { version = "0.10.0", default-features = false, features = ["serde"] }

[dev-dependencies]
Expand Down
10 changes: 4 additions & 6 deletions src/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::{
path::{Path, PathBuf},
};

use crate::fuzzy::fuzzy_match_score;

pub fn get_current_directory() -> PathBuf {
env::current_dir().unwrap_or(PathBuf::from("/"))
}
Expand Down Expand Up @@ -52,19 +50,19 @@ pub fn sub_directories(cwd: &Path, depth: u32) -> Vec<Directory> {
#[derive(Debug)]
pub struct ScoredDirectory {
directory: Directory,
score: i32,
score: u32,
}

impl ScoredDirectory {
pub fn new(directory: Directory, score: i32) -> Self {
pub fn new(directory: Directory, score: u32) -> Self {
Self { directory, score }
}

pub fn directory(&self) -> &Directory {
&self.directory
}

pub fn score(&self) -> i32 {
pub fn score(&self) -> u32 {
self.score
}
}
Expand All @@ -73,7 +71,7 @@ pub fn scored_directories(directories: &[Directory], query: &str) -> Vec<ScoredD
directories
.iter()
.map(|directory| {
let score = fuzzy_match_score(directory.name(), query);
let score = fuzzy_dir::score_dir(directory.name(), query);
ScoredDirectory::new(directory.clone(), score)
})
.collect()
Expand Down
71 changes: 0 additions & 71 deletions src/fuzzy.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod directory;
pub mod fuzzy;
pub mod query;
pub mod query_part;
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::cmd::Run;

mod cmd;
mod directory;
mod fuzzy;
mod query;
mod query_part;
mod ui;
Expand Down
50 changes: 22 additions & 28 deletions src/query_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,41 +69,35 @@ impl QueryPart {
vec![dir]
}
QueryPart::Text(text) => {
let mut scored_dirs = scored_directories(
&dirs
.iter()
.flat_map(|dir| sub_directories(dir.location().as_path(), 0))
.collect::<Vec<_>>(),
text.as_str(),
);

let average_score: f64 = scored_dirs
.iter()
.map(|scored_dir| f64::from(scored_dir.score()))
.sum::<f64>()
/ scored_dirs.len() as f64;
let scored_dirs = {
let mut dirs = scored_directories(
&dirs
.iter()
.flat_map(|dir| sub_directories(dir.location(), 0))
.collect::<Vec<_>>(),
text.as_str(),
);
// sort by score, if scores are equal by alphabetical order
dirs.sort_unstable_by(|a, b| {
a.score()
.cmp(&b.score())
.then(a.directory().location().cmp(b.directory().location()))
});
dirs
};

let half_of_highest_score = scored_dirs
.iter()
let min_score = (scored_dirs
.last() // since it's sorted by score the last one has the most score
.map(ScoredDirectory::score)
.max()
.unwrap_or(0_i32)
/ 2;

// sort by score, if scores are equal by alphabetical order
scored_dirs.sort_unstable_by(|a, b| {
a.score()
.cmp(&b.score())
.then(a.directory().location().cmp(b.directory().location()))
});
.unwrap_or_default() as f64)
/ 2.;

scored_dirs
.iter()
// remove dirs with low score
.filter(|scored_dir| {
f64::from(scored_dir.score()) > 0.0
&& f64::from(scored_dir.score()) >= average_score
&& scored_dir.score() >= half_of_highest_score
let score = scored_dir.score() as f64;
score > 0. && score >= min_score
})
.map(|scored_dir| scored_dir.directory().clone())
.collect()
Expand Down
12 changes: 8 additions & 4 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ fn test_absolute() {
#[test]
fn test_nonexisting() {
let env = TempEnv::new();
assert!(env.resolve_query("test zzzzzzzzz zzzzzzzzz").is_empty());
assert_eq!(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, is there any difference here? Or just readability?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected empty vec, found this instead assert_eq error message is more informative than expected vec to be empty, it was not

env.resolve_query("test zzzzzzzzz zzzzzzzzz"),
Vec::<PathBuf>::new()
);
}

#[test]
Expand Down Expand Up @@ -183,10 +186,11 @@ fn test_dir_skip() {
let env = TempEnv::new();

assert_eq!(
env.resolve_query("test gamma - u"),
env.resolve_query("test alpha - eps"),
vec!(
env.abs_path("test/gamma/sigm#a/upsilon3"),
env.abs_path("test/gamma/sigma9/t@u0"),
env.abs_path("test/alpha/beta/epsil@on8"),
env.abs_path("test/alpha/betabeta/epsil#on"),
env.abs_path("test/alpha/betabeta/epsil0n"),
)
);
assert_eq!(
Expand Down
Loading