From 1d031d99c1967a3f21d0ea6bd9424bdd02d83b01 Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 24 Jan 2022 18:25:23 -0500 Subject: [PATCH] feat: Remove "approx." keyword and improve decimal point/comma handling --- plugins/src/calc/mod.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/plugins/src/calc/mod.rs b/plugins/src/calc/mod.rs index 732c60b..6f1c0eb 100644 --- a/plugins/src/calc/mod.rs +++ b/plugins/src/calc/mod.rs @@ -116,8 +116,13 @@ impl App { async fn qcalc(regex: &mut Regex, expression: &str, decimal_comma: bool) -> Option { let mut command = Command::new("qalc"); + command.args(&["-u8"]); + command.args(&["-set", "maxdeci 9"]); + if decimal_comma { command.args(&["-set", "decimal comma on"]); + } else { + command.args(&["-set", "decimal comma off"]); } let spawn = command @@ -193,10 +198,10 @@ async fn qcalc(regex: &mut Regex, expression: &str, decimal_comma: bool) -> Opti } } - let cut = if let Some(pos) = normalized.find('=') { + let cut = if let Some(pos) = normalized.rfind('≈') { + pos + } else if let Some(pos) = normalized.rfind('=') { pos + 1 - } else if let Some(pos) = normalized.find('≈') { - pos + '≈'.len_utf8() } else { return None; }; @@ -242,6 +247,8 @@ fn extract_value(expression: &str) -> &str { #[cfg(test)] mod tests { + use crate::calc::App; + #[test] fn extract_value() { assert_eq!("7.5", super::extract_value("7 + 1/2 = 7.5")); @@ -252,4 +259,22 @@ mod tests { super::extract_value("4/3 ≈ 1 + 1/3 ≈ 1.333333333") ); } + + #[test] + fn approximate_result_formatting() { + let task = smol::spawn(async { + let mut app = App { + decimal_comma: false, + ..Default::default() + }; + app.search("7 / 3").await; + app.outcome.take() + }); + + smol::block_on(async { + if let Some(result) = task.await { + assert_eq!("≈ 2.333333333", result); + } + }) + } }