From 64fd3bcaae9def83c6773909d406c4583bee05ea Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 21 Dec 2023 04:01:28 -0600 Subject: [PATCH] allow calc queries to end in = --- src/engines/answer/calc.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/engines/answer/calc.rs b/src/engines/answer/calc.rs index 387e56a..58725c8 100644 --- a/src/engines/answer/calc.rs +++ b/src/engines/answer/calc.rs @@ -3,27 +3,35 @@ use crate::engines::EngineResponse; use super::regex; pub fn request(query: &str) -> EngineResponse { - let Some(result_html) = evaluate(query, true) else { + let query = clean_query(query.to_string()); + + let Some(result_html) = evaluate(&query, true) else { return EngineResponse::new(); }; EngineResponse::answer_html(format!( r#"

{query} =

{result_html}

"#, - query = html_escape::encode_text(query), + query = html_escape::encode_text(&query), )) } pub fn request_autocomplete(query: &str) -> Vec { let mut results = Vec::new(); - if let Some(result) = evaluate(query, false) { + let query = clean_query(query.to_string()); + + if let Some(result) = evaluate(&query, false) { results.push(format!("{query}={result}")); } results } +fn clean_query(query: String) -> String { + query.strip_suffix('=').unwrap_or(&query).trim().to_string() +} + fn evaluate(query: &str, html: bool) -> Option { // at least 3 characters and not one of the short constants if query.len() < 3 && !matches!(query.to_lowercase().as_str(), "pi" | "e" | "c") {