Skip to content

Commit

Permalink
chore: add sorting technique (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthJadhav authored Dec 26, 2022
1 parent a0261a7 commit 4200127
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ regex = "1"
ignore = "0.4"
num_cpus = "1.0"
dirs = "4.0.0"
strsim = "0.10.0"
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rust_search = "2.0.0"

## Examples

General use
- General use

```rust
use rust_search::SearchBuilder;
Expand All @@ -48,7 +48,34 @@ fn main(){
}
```

To get all the files with a specific extension in a directory, use:
- Sort the output by similarity with the input

```rust
use rust_search::{SearchBuilder, similarity_sort};
fn main() {
let search_input = "fly";
let mut search: Vec<String> = SearchBuilder::default()
.location("~/Desktop/")
.search_input(search_input)
.depth(1)
.ignore_case()
.build()
.collect();

similarity_sort(&mut search, &search_input);
for path in search {
println!("{:?}", path);
}
}

```
> search **without** similarity sort
`["afly.txt", "bfly.txt", "flyer.txt", "fly.txt"]`

> search **with** similarity sort
`["fly.txt", "flyer.txt", "afly.txt", "bfly.txt",]`

- To get all the files with a specific extension in a directory, use:

```rust
use rust_search::SearchBuilder;
Expand All @@ -60,7 +87,7 @@ let files: Vec<String> = SearchBuilder::default()
.collect();
```

To get all the files in a directory, use:
- To get all the files in a directory, use:

```rust
use rust_search::SearchBuilder;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pub use filter::{FileSize, FilterExt, FilterFn};
// export this in order to use it with custom filter functions
pub use ignore::DirEntry;
pub use search::Search;
pub use utils::similarity_sort;
50 changes: 48 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};

use regex::Regex;
use std::path::{Path, PathBuf};
use strsim::jaro_winkler;

pub(crate) fn build_regex_search_input(
search_input: Option<&str>,
Expand Down Expand Up @@ -40,3 +40,49 @@ pub(crate) fn replace_tilde_with_home_dir(path: impl AsRef<Path>) -> PathBuf {
}
path.to_path_buf()
}

fn file_name_from_path(path: &str) -> String {
let path = Path::new(path);
let file_name = path.file_name().unwrap().to_str().unwrap();
return file_name.to_string();
}

/// This function can be used to sort the given vector on basis of similarity between the input & the vector
///
/// ### Arguments
/// * `&mut vector` - it needs a mutable reference to the vector
/// ### Examples
/// ```rust
/// use rust_search::{SearchBuilder, similarity_sort};
/// fn main() {
/// let search_input = "fly";
/// let mut search: Vec<String> = SearchBuilder::default()
/// .location("~/Desktop/")
/// .search_input(search_input)
/// .depth(1)
/// .ignore_case()
/// .build()
/// .collect();

/// similarity_sort(&mut search, &search_input);
/// for path in search {
/// println!("{:?}", path);
/// }
/// }
/// ```
///
/// search **without** similarity sort
/// `["afly.txt", "bfly.txt", "flyer.txt", "fly.txt"]`
///
/// search **with** similarity sort
/// `["fly.txt", "flyer.txt", "afly.txt", "bfly.txt",]`
pub fn similarity_sort(vector: &mut Vec<String>, input: &str) {
vector.sort_by(|a, b| {
let input = input.to_lowercase();
let a = file_name_from_path(a).to_lowercase();
let b = file_name_from_path(b).to_lowercase();
let a = jaro_winkler(a.as_str(), input.as_str());
let b = jaro_winkler(b.as_str(), input.as_str());
b.partial_cmp(&a).unwrap()
});
}

0 comments on commit 4200127

Please sign in to comment.