Skip to content

Commit

Permalink
Merge pull request #83 from grouzen/fuzzy-search-for-the-rest
Browse files Browse the repository at this point in the history
Add fuzzy search for Cmd, Ports, and Args
  • Loading branch information
jacek-kurlit authored Oct 2, 2024
2 parents 159d0b0 + 958a458 commit 4db8c29
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/processes/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ impl QueryFilter {

pub(super) fn accept(&self, prc: &impl ProcessInfo, ports: Option<&str>) -> bool {
match self.search_by {
SearchBy::Cmd => self.query_matches_str(prc.cmd()),
SearchBy::Path => self.query_matches_fuzzy_opt(prc.cmd_path()),
SearchBy::Cmd => self.query_match_str(prc.cmd()),
SearchBy::Path => self.query_matches_opt(prc.cmd_path()),
SearchBy::Args => self.query_matches_vec(get_process_args(prc)),
SearchBy::Port => self.query_matches_opt(ports),
SearchBy::Pid => self.query_eq_u32(prc.pid()),
SearchBy::ProcessFamily => self.query_matches_process_family(prc),
SearchBy::Everywhere => {
self.query_matches_str(prc.cmd())
self.query_match_str(prc.cmd())
|| self.query_matches_opt(prc.cmd_path())
|| self.query_matches_opt(ports)
|| self.query_matches_vec(get_process_args(prc))
Expand All @@ -59,26 +59,18 @@ impl QueryFilter {
}
}

fn query_matches_str(&self, s: &str) -> bool {
s.to_lowercase().contains(&self.query)
}

fn query_match_fuzzy_str(&self, s: &str) -> bool {
fn query_match_str(&self, s: &str) -> bool {
let score = self.matcher.fuzzy_match(s, self.query.as_str());
// TODO: fine-tune the score threshold or make it configurable?
score.map(|s| s >= 0).unwrap_or(false)
}

fn query_matches_fuzzy_opt(&self, s: Option<&str>) -> bool {
s.map(|s| self.query_match_fuzzy_str(s)).unwrap_or(false)
}

fn query_matches_opt(&self, s: Option<&str>) -> bool {
s.map(|v| self.query_matches_str(v)).unwrap_or(false)
s.map(|s| self.query_match_str(s)).unwrap_or(false)
}

fn query_matches_vec(&self, s: Vec<&str>) -> bool {
s.iter().any(|a| self.query_matches_str(a))
s.iter().any(|a| self.query_match_str(a))
}

fn query_eq_u32(&self, s: u32) -> bool {
Expand Down
3 changes: 3 additions & 0 deletions tests/processes_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{thread, time::Duration};
use pik::processes::{FilterOptions, ProcessManager};

#[test]
#[cfg(not(target_os = "macos"))]
fn should_find_cargo_process_by_cmd_name() {
let mut process_manager = ProcessManager::new().unwrap();
let results = process_manager.find_processes("cargo", FilterOptions::default());
Expand All @@ -22,6 +23,7 @@ fn should_find_cargo_process_by_cmd_path() {
}

#[test]
#[cfg(not(target_os = "macos"))]
fn should_find_cargo_process_by_name_path_or_args() {
let mut process_manager = ProcessManager::new().unwrap();
let results = process_manager.find_processes("~cargo", FilterOptions::default());
Expand All @@ -34,6 +36,7 @@ fn should_find_cargo_process_by_name_path_or_args() {
}

#[test]
#[cfg(not(target_os = "macos"))]
fn should_find_cargo_process_by_args() {
let mut process_manager = ProcessManager::new().unwrap();
let results = process_manager.find_processes("-test", FilterOptions::default());
Expand Down

0 comments on commit 4db8c29

Please sign in to comment.