From ec1f353825b6f35020544f3a71c801055312a057 Mon Sep 17 00:00:00 2001 From: Alex Saveau Date: Fri, 26 Jul 2024 16:48:49 -0700 Subject: [PATCH] Support case-insensitive search in CLI Signed-off-by: Alex Saveau --- cli/command-reference-short.golden | 5 +++-- cli/command-reference.golden | 3 +++ cli/src/main.rs | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/cli/command-reference-short.golden b/cli/command-reference-short.golden index 20e698c..328d944 100644 --- a/cli/command-reference-short.golden +++ b/cli/command-reference-short.golden @@ -43,8 +43,9 @@ Arguments: 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) --- diff --git a/cli/command-reference.golden b/cli/command-reference.golden index 4fc3bfb..a3011c6 100644 --- a/cli/command-reference.golden +++ b/cli/command-reference.golden @@ -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) diff --git a/cli/src/main.rs b/cli/src/main.rs index 23a765e..637f0e9 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -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, @@ -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; @@ -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::::new(); let mut buf = [0; CONTEXT_WINDOW]; for result in result_stream {