fix(calc): Extract value from output expression on activate
This commit is contained in:
parent
908364687a
commit
c330552c20
1 changed files with 20 additions and 1 deletions
|
|
@ -56,7 +56,7 @@ 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(mut outcome) = self.outcome.take() {
|
||||||
outcome = ["= ", outcome.as_str()].concat();
|
outcome = ["= ", extract_value(&outcome)].concat();
|
||||||
crate::send(&mut self.out, PluginResponse::Fill(outcome)).await;
|
crate::send(&mut self.out, PluginResponse::Fill(outcome)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -216,3 +216,22 @@ pub async fn uses_decimal_comma() -> bool {
|
||||||
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extracts the value from an outcome expression.
|
||||||
|
fn extract_value(expression: &str) -> &str {
|
||||||
|
expression
|
||||||
|
.find('=')
|
||||||
|
.map(|p| p + 1)
|
||||||
|
.or_else(|| expression.find('≈').map(|p| p + 3))
|
||||||
|
.map(|pos| expression[pos..].trim())
|
||||||
|
.unwrap_or(&expression)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn extract_value() {
|
||||||
|
assert_eq!("7.5", super::extract_value("7 + 1/2 = 7.5"));
|
||||||
|
assert_eq!("1.333333333", super::extract_value("1 + 1/3 ≈ 1.333333333"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue