Skip to content

Commit

Permalink
hashtag support on descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lasantosr committed May 15, 2023
1 parent fc75983 commit 119ad43
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
Expand Down
29 changes: 23 additions & 6 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -323,20 +328,20 @@ 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
"#,
)?;

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(" ");
Expand All @@ -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")?;

Expand Down

0 comments on commit 119ad43

Please sign in to comment.