Skip to content

Commit

Permalink
Support case-insensitive search in CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX committed Jul 26, 2024
1 parent ea43996 commit ec1f353
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 3 additions & 2 deletions cli/command-reference-short.golden
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ Arguments:
<QUERY> The query string to search for

Options:
-r, --regex Interpret the query string as regex instead of a plain-text match
-h, --help Print help (use `--help` for more detail)
-r, --regex Interpret the query string as regex instead of a plain-text match
-i, --ignore-case Ignore ASCII casing when searching
-h, --help Print help (use `--help` for more detail)

---

Expand Down
3 changes: 3 additions & 0 deletions cli/command-reference.golden
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Options:
-r, --regex
Interpret the query string as regex instead of a plain-text match

-i, --ignore-case
Ignore ASCII casing when searching

-h, --help
Print help (use `-h` for a summary)

Expand Down
17 changes: 16 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ struct Search {
#[arg(short, long)]
regex: bool,

/// Ignore ASCII casing when searching.
#[arg(short, long)]
#[arg(conflicts_with = "regex")]
ignore_case: bool,

/// The query string to search for.
#[arg(required = true)]
query: String,
Expand Down Expand Up @@ -487,7 +492,13 @@ fn get(EntryAction { id }: EntryAction) -> Result<(), CliError> {
Ok(())
}

fn search(Search { regex, query }: Search) -> Result<(), CliError> {
fn search(
Search {
regex,
ignore_case,
mut query,
}: Search,
) -> Result<(), CliError> {
const PREFIX_CONTEXT: usize = 40;
const CONTEXT_WINDOW: usize = 100;

Expand Down Expand Up @@ -536,11 +547,15 @@ fn search(Search { regex, query }: Search) -> Result<(), CliError> {
let (result_stream, threads) = ringboard_sdk::search(
if regex {
Query::Regex(Regex::new(&query)?)
} else if ignore_case {
query.make_ascii_lowercase();
Query::PlainIgnoreCase(query.as_bytes())
} else {
Query::Plain(query.as_bytes())
},
reader.clone(),
);
drop(query);
let mut results = BTreeMap::<BucketAndIndex, (u16, u16)>::new();
let mut buf = [0; CONTEXT_WINDOW];
for result in result_stream {
Expand Down

0 comments on commit ec1f353

Please sign in to comment.