Skip to content

Commit

Permalink
feat: add fuzzy matching
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael DEMACON <[email protected]>
  • Loading branch information
quantumsheep committed Feb 16, 2024
1 parent 3632041 commit 83f484a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
20 changes: 20 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 @@ -10,6 +10,7 @@ path = "src/main.rs"
[dependencies]
clap = { version = "4.5.0", features = ["derive"] }
crossterm = "0.27.0"
fuzzy-matcher = "0.3.7"
itertools = "0.12.1"
ratatui = "0.26.1"
shellexpand = "3.1.0"
Expand Down
6 changes: 5 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
#[allow(clippy::wildcard_imports)]
use ratatui::{prelude::*, widgets::*};
use std::{cell::RefCell, error::Error, io, rc::Rc};
Expand Down Expand Up @@ -221,12 +222,15 @@ fn render_table(f: &mut Frame, app: &mut App, area: Rect) {

let search_value = app.search.value();

let matcher = SkimMatcherV2::default();

let rows = app
.hosts
.iter()
.filter(|host| {
search_value.is_empty()
|| (host.hostname.contains(search_value) || host.aliases.contains(search_value))
|| matcher.fuzzy_match(&host.hostname, search_value).is_some()
|| matcher.fuzzy_match(&host.aliases, search_value).is_some()
})
.map(|host| {
[
Expand Down

0 comments on commit 83f484a

Please sign in to comment.