From 19bd00f62005d07fc22ef72558e1102a7bb13b03 Mon Sep 17 00:00:00 2001 From: Yuvi Panda Date: Sun, 9 May 2021 13:03:56 +0530 Subject: Support fulltext search of commands (#75) --- atuin-client/src/database.rs | 26 +++++++++++++++++++++----- atuin-client/src/settings.rs | 11 +++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) (limited to 'atuin-client') diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index e56a8df0..160c6054 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -13,6 +13,7 @@ use sqlx::sqlite::{ use sqlx::Row; use super::history::History; +use super::settings::SearchMode; #[async_trait] pub trait Database { @@ -34,7 +35,12 @@ pub trait Database { async fn last(&self) -> Result; async fn before(&self, timestamp: chrono::DateTime, count: i64) -> Result>; - async fn search(&self, limit: Option, query: &str) -> Result>; + async fn search( + &self, + limit: Option, + search_mode: SearchMode, + query: &str, + ) -> Result>; async fn query_history(&self, query: &str) -> Result>; } @@ -185,7 +191,7 @@ impl Database for Sqlite { // inject the unique check if unique { "where timestamp = ( - select max(timestamp) from history + select max(timestamp) from history where h.command = history.command )" } else { @@ -268,16 +274,26 @@ impl Database for Sqlite { Ok(res.0) } - async fn search(&self, limit: Option, query: &str) -> Result> { + async fn search( + &self, + limit: Option, + search_mode: SearchMode, + query: &str, + ) -> Result> { let query = query.to_string().replace("*", "%"); // allow wildcard char let limit = limit.map_or("".to_owned(), |l| format!("limit {}", l)); + let query = match search_mode { + SearchMode::Prefix => query, + SearchMode::FullText => format!("%{}", query), + }; + let res = sqlx::query( format!( "select * from history h - where command like ?1 || '%' + where command like ?1 || '%' and timestamp = ( - select max(timestamp) from history + select max(timestamp) from history where h.command = history.command ) order by timestamp desc {}", diff --git a/atuin-client/src/settings.rs b/atuin-client/src/settings.rs index 4ea4be84..7ccbaf32 100644 --- a/atuin-client/src/settings.rs +++ b/atuin-client/src/settings.rs @@ -10,6 +10,15 @@ use parse_duration::parse; pub const HISTORY_PAGE_SIZE: i64 = 100; +#[derive(Clone, Debug, Deserialize, Copy)] +pub enum SearchMode { + #[serde(rename = "prefix")] + Prefix, + + #[serde(rename = "fulltext")] + FullText, +} + #[derive(Clone, Debug, Deserialize)] pub struct Settings { pub dialect: String, @@ -19,6 +28,7 @@ pub struct Settings { pub db_path: String, pub key_path: String, pub session_path: String, + pub search_mode: SearchMode, // This is automatically loaded when settings is created. Do not set in // config! Keep secrets and settings apart. @@ -100,6 +110,7 @@ impl Settings { s.set_default("auto_sync", true)?; s.set_default("sync_frequency", "1h")?; s.set_default("sync_address", "https://api.atuin.sh")?; + s.set_default("search_mode", "prefix")?; if config_file.exists() { s.merge(ConfigFile::with_name(config_file.to_str().unwrap()))?; -- cgit v1.2.3