Skip to content

Commit

Permalink
feat: change fulltext to exact search mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lilydjwg committed Feb 1, 2024
1 parent f6b541d commit 49f5d2c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion atuin-client/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# sync_frequency = "10m"

## which search mode to use
## possible values: prefix, fulltext, fuzzy, skim
## possible values: prefix, exact, fuzzy, skim
# search_mode = "fuzzy"

## which filter mode to use
Expand Down
15 changes: 10 additions & 5 deletions atuin-client/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ impl Database for Sqlite {

match search_mode {
SearchMode::Prefix => sql.and_where_like_left("command", query),
SearchMode::FullText => sql.and_where_like_any("command", query),
_ => {
// don't recompile the regex on successive calls!
lazy_static! {
Expand All @@ -448,6 +447,7 @@ impl Database for Sqlite {
None => (false, query_part),
};

#[allow(clippy::if_same_then_else)]
let param = if query_part == "|" {
if !is_or {
is_or = true;
Expand All @@ -463,6 +463,8 @@ impl Database for Sqlite {
format!("{glob}{term}{glob}")
} else if is_inverse {
format!("{glob}{query_part}{glob}")
} else if search_mode == SearchMode::Exact {
format!("{glob}{query_part}{glob}")
} else {
query_part.split("").join(glob)
};
Expand Down Expand Up @@ -802,17 +804,20 @@ mod test {
}

#[tokio::test(flavor = "multi_thread")]
async fn test_search_fulltext() {
async fn test_search_exact() {
let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap();
new_history_item(&mut db, "ls /home/ellie").await.unwrap();

assert_search_eq(&db, SearchMode::FullText, FilterMode::Global, "ls", 1)
assert_search_eq(&db, SearchMode::Exact, FilterMode::Global, "ls", 1)
.await
.unwrap();
assert_search_eq(&db, SearchMode::Exact, FilterMode::Global, "/home", 1)
.await
.unwrap();
assert_search_eq(&db, SearchMode::FullText, FilterMode::Global, "/home", 1)
assert_search_eq(&db, SearchMode::Exact, FilterMode::Global, "ls ho", 1)
.await
.unwrap();
assert_search_eq(&db, SearchMode::FullText, FilterMode::Global, "ls ", 0)
assert_search_eq(&db, SearchMode::Exact, FilterMode::Global, "hm", 0)
.await
.unwrap();
}
Expand Down
13 changes: 6 additions & 7 deletions atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ pub enum SearchMode {
#[serde(rename = "prefix")]
Prefix,

#[serde(rename = "fulltext")]
#[clap(aliases = &["fulltext"])]
FullText,
#[serde(rename = "exact")]
Exact,

#[serde(rename = "fuzzy")]
Fuzzy,
Expand All @@ -47,18 +46,18 @@ impl SearchMode {
pub fn as_str(&self) -> &'static str {
match self {
SearchMode::Prefix => "PREFIX",
SearchMode::FullText => "FULLTXT",
SearchMode::Exact => "EXACT",
SearchMode::Fuzzy => "FUZZY",
SearchMode::Skim => "SKIM",
}
}
pub fn next(&self, settings: &Settings) -> Self {
match self {
SearchMode::Prefix => SearchMode::FullText,
SearchMode::Prefix => SearchMode::Exact,
// if the user is using skim, we go to skim
SearchMode::FullText if settings.search_mode == SearchMode::Skim => SearchMode::Skim,
SearchMode::Exact if settings.search_mode == SearchMode::Skim => SearchMode::Skim,
// otherwise fuzzy.
SearchMode::FullText => SearchMode::Fuzzy,
SearchMode::Exact => SearchMode::Fuzzy,
SearchMode::Fuzzy | SearchMode::Skim => SearchMode::Prefix,
}
}
Expand Down

0 comments on commit 49f5d2c

Please sign in to comment.