Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove "approx." keyword and improve decimal point/comma handling #70

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions plugins/src/calc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,13 @@ impl App {
async fn qcalc(regex: &mut Regex, expression: &str, decimal_comma: bool) -> Option<String> {
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
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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"));
Expand All @@ -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);
}
})
}
}