From b8192e1df172517868a6d0f672625f6a9c5a8a16 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 20 Oct 2021 21:40:10 +0200 Subject: [PATCH] feat(calc): Show calculations globally if query is a math expression --- plugins/src/calc/mod.rs | 35 ++++++++++++++++++++++++----------- plugins/src/calc/plugin.ron | 5 +++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/plugins/src/calc/mod.rs b/plugins/src/calc/mod.rs index 853b3ed..c1bcb4f 100644 --- a/plugins/src/calc/mod.rs +++ b/plugins/src/calc/mod.rs @@ -55,9 +55,9 @@ impl Default for App { impl App { pub async fn activate(&mut self) { - if let Some(mut outcome) = self.outcome.take() { - outcome = ["= ", extract_value(&outcome)].concat(); - crate::send(&mut self.out, PluginResponse::Fill(outcome)).await; + if let Some(outcome) = self.outcome.take() { + let value = ["= ", extract_value(&outcome)].concat(); + crate::send(&mut self.out, PluginResponse::Fill(value)).await; } } @@ -75,19 +75,32 @@ impl App { crate::send(&mut self.out, PluginResponse::Context { id: 0, options }).await; } - pub async fn search(&mut self, query: &str) { - if let Some(mut search) = query.strip_prefix('=') { - search = search.trim(); - self.outcome = qcalc(&mut self.regex, search, self.decimal_comma).await; + pub async fn search(&mut self, mut query: &str) { + let had_prefix = if let Some(stripped) = query.strip_prefix('=') { + query = stripped; + true + } else { + false + }; + let search = query.trim(); + + self.outcome = qcalc(&mut self.regex, search, self.decimal_comma).await; + + let outcome = self.outcome.clone().or_else(|| { + if had_prefix { + Some([search, " x = ?"].concat()) + } else { + None + } + }); + + if let Some(name) = outcome { crate::send( &mut self.out, PluginResponse::Append(PluginSearchResult { id: 0, - name: self - .outcome - .clone() - .unwrap_or_else(|| [search, " x = ?"].concat()), + name, description: String::new(), icon: Some(IconSource::Name(Cow::Borrowed("accessories-calculator"))), ..Default::default() diff --git a/plugins/src/calc/plugin.ron b/plugins/src/calc/plugin.ron index ccdddee..5bcdb15 100644 --- a/plugins/src/calc/plugin.ron +++ b/plugins/src/calc/plugin.ron @@ -2,9 +2,10 @@ name: "Calculator with unit conversion (uses Qalculate! expressions)", description: "Syntax: = \nExample: = 10 J / (196x^2) = 4 kW/s", query: ( - regex: "^(=)+", + regex: ".*[0-9].*", help: "= ", - isolate: true, + priority: High, + isolate_with: "^(=).*", ), bin: (path: "calc"), icon: Name("x-office-spreadsheet")