feat(calc): Show calculations globally if query is a math expression

This commit is contained in:
Michael Aaron Murphy 2021-10-20 21:40:10 +02:00
parent e7e6007e26
commit b8192e1df1
2 changed files with 27 additions and 13 deletions

View file

@ -55,9 +55,9 @@ impl Default for App {
impl App { impl App {
pub async fn activate(&mut self) { pub async fn activate(&mut self) {
if let Some(mut outcome) = self.outcome.take() { if let Some(outcome) = self.outcome.take() {
outcome = ["= ", extract_value(&outcome)].concat(); let value = ["= ", extract_value(&outcome)].concat();
crate::send(&mut self.out, PluginResponse::Fill(outcome)).await; 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; crate::send(&mut self.out, PluginResponse::Context { id: 0, options }).await;
} }
pub async fn search(&mut self, query: &str) { pub async fn search(&mut self, mut query: &str) {
if let Some(mut search) = query.strip_prefix('=') { let had_prefix = if let Some(stripped) = query.strip_prefix('=') {
search = search.trim(); query = stripped;
self.outcome = qcalc(&mut self.regex, search, self.decimal_comma).await; 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( crate::send(
&mut self.out, &mut self.out,
PluginResponse::Append(PluginSearchResult { PluginResponse::Append(PluginSearchResult {
id: 0, id: 0,
name: self name,
.outcome
.clone()
.unwrap_or_else(|| [search, " x = ?"].concat()),
description: String::new(), description: String::new(),
icon: Some(IconSource::Name(Cow::Borrowed("accessories-calculator"))), icon: Some(IconSource::Name(Cow::Borrowed("accessories-calculator"))),
..Default::default() ..Default::default()

View file

@ -2,9 +2,10 @@
name: "Calculator with unit conversion (uses Qalculate! expressions)", name: "Calculator with unit conversion (uses Qalculate! expressions)",
description: "Syntax: = <expression>\nExample: = 10 J / (196x^2) = 4 kW/s", description: "Syntax: = <expression>\nExample: = 10 J / (196x^2) = 4 kW/s",
query: ( query: (
regex: "^(=)+", regex: ".*[0-9].*",
help: "= ", help: "= ",
isolate: true, priority: High,
isolate_with: "^(=).*",
), ),
bin: (path: "calc"), bin: (path: "calc"),
icon: Name("x-office-spreadsheet") icon: Name("x-office-spreadsheet")