From 119ad436599c54c0c6bfc593866bf8ed9fa91e73 Mon Sep 17 00:00:00 2001 From: Luis Alberto Santos Date: Sun, 14 May 2023 19:10:32 +0200 Subject: [PATCH] hashtag support on descriptions --- README.md | 2 +- src/lib.rs | 2 +- src/storage.rs | 29 +++++++++++++++++++++++------ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b90f303..f3ba283 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ It currently works on Bash, Zsh and Fish and should be compatible with most Linu - Standalone binaries - Autocomplete currently typed command - - Full Text Search in both command and description + - Full Text Search in both command and description with hashtag support - Find & replace labels of currently typed command - Non-intrusive (inline) and full-screen interfaces - Fetch command to parse and store [tldr](https://github.com/tldr-pages/tldr) pages (Thanks to them!) diff --git a/src/lib.rs b/src/lib.rs index 6d423fc..5a1bd5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ //! //! - Standalone binaries //! - Autocomplete currently typed command -//! - Full Text Search in both command and description +//! - Full Text Search in both command and description with hashtag support //! - Find & replace labels of currently typed command //! - Non-intrusive (inline) and full-screen interfaces //! - Fetch command to parse and store [tldr](https://github.com/tldr-pages/tldr) pages (Thanks to them!) diff --git a/src/storage.rs b/src/storage.rs index 4a7dc92..f4b7a13 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -295,9 +295,14 @@ impl SqliteStorage { return Ok(vec![cmd]); } - let flat_search = ALLOWED_FTS_REGEX.replace_all(&flat_search, ""); - let flat_search = flat_search.trim(); - if flat_search.is_empty() || flat_search == " " { + let hashtags = flat_search + .split_whitespace() + .filter(|t| t.starts_with('#')) + .collect_vec(); + + let flat_fts_search = ALLOWED_FTS_REGEX.replace_all(&flat_search, ""); + let flat_fts_search = flat_fts_search.trim(); + if flat_fts_search.is_empty() || flat_fts_search == " " { drop(conn); return self.get_commands(USER_CATEGORY); } @@ -323,7 +328,7 @@ impl SqliteStorage { SELECT c.rowid, c.category, c.alias, c.cmd, c.description, c.usage, 0 as ord FROM command_fts s JOIN command c ON s.rowid = c.rowid - WHERE s.flat_cmd GLOB :glob + WHERE s.flat_cmd GLOB :glob OR s.flat_description GLOB :glob ) ORDER BY ord DESC, usage DESC, (CASE WHEN category = 'user' THEN 1 ELSE 0 END) DESC "#, @@ -331,12 +336,12 @@ impl SqliteStorage { let match_cmd_ordered = format!( "\"flat_cmd\" : ^{}", - flat_search + flat_fts_search .split_whitespace() .map(|token| format!("{token}*")) .join(" + ") ); - let match_simple = flat_search + let match_simple = flat_fts_search .split_whitespace() .map(|token| format!("{token}*")) .join(" "); @@ -352,6 +357,18 @@ impl SqliteStorage { (":glob", &glob), ])? .mapped(command_from_row) + .filter(|r| { + if !hashtags.is_empty() { + if let Ok(command) = r { + for tag in &hashtags { + if !command.description.contains(tag) { + return false; + } + } + } + } + true + }) .finish_vec() .context("Error querying fts command")?;