diff --git a/src/calc.rs b/src/calc.rs index 61acf75..f9339d5 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -205,7 +205,8 @@ impl<'a> Calculator<'a> { for c in value.chars() { self.take_input(c).map_err(|e| { - self.cancel(); + // Try cancelling, but if you cannot, that is okay + self.cancel().unwrap_or(()); e })?; } @@ -239,9 +240,15 @@ impl<'a> Calculator<'a> { } } - pub fn cancel(&mut self) { + pub fn cancel(&mut self) -> CalculatorResult<()> { self.state = CalculatorState::Normal; - self.active_macros.clear(); + // We died in a macro. Quit and push an end macro state + if !self.active_macros.is_empty() { + self.active_macros.clear(); + // Should always be successful, but report the error if there is one + self.op(CalculatorOperation::Macro(MacroState::End))?; + } + Ok(()) } pub fn backspace(&mut self) -> CalculatorResult<()> { self.l.pop(); diff --git a/src/main.rs b/src/main.rs index 6e55f9f..01143ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -268,7 +268,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult match key { Key::Esc | Key::Char('q') => { app.state = AppState::Calculator; - app.calculator.cancel(); + app.calculator.cancel()?; } _ => {} }, @@ -277,7 +277,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult match key { Key::Esc => { app.state = AppState::Calculator; - app.calculator.cancel(); + app.calculator.cancel()?; } Key::Char(c) => { app.calculator.take_input(c)?;