diff --git a/src/calc.rs b/src/calc.rs index 95bbe6d..6749971 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -36,6 +36,7 @@ impl CalculatorStateChange { pub struct Calculator { #[serde(skip)] l: String, + save_on_close: bool, stack: VecDeque, #[serde(serialize_with = "ordered_char_map")] macros: CalculatorMacros, @@ -79,6 +80,7 @@ impl Default for Calculator { registers: CalculatorRegisters::new(), state: CalculatorState::Normal, stack: vec![1.0, 2.0].into_iter().collect(), + save_on_close: false, macros: [ ( 'm', @@ -132,6 +134,14 @@ impl Default for Calculator { } impl Calculator { + pub fn close(&self) -> CalculatorResult<()> { + if self.save_on_close { + self.save_config() + } else { + Ok(()) + } + } + pub fn load_config() -> CalculatorResult { load(APP_NAME).map_err(|e| CalculatorError::LoadError(Some(e))) } @@ -302,6 +312,8 @@ impl Calculator { precision: self.pop_usize()?, } } + 'w' => self.save_on_close = false, + 'W' => self.save_on_close = true, _ => return Err(CalculatorError::NoSuchSetting(c)), }; self.state = CalculatorState::Normal; @@ -388,6 +400,9 @@ impl Calculator { pub fn get_display_mode(&self) -> &CalculatorDisplayMode { &self.display_mode } + pub fn get_save_on_close(&self) -> bool { + self.save_on_close + } pub fn flush_l(&mut self) -> CalculatorResult { if self.l.is_empty() { diff --git a/src/calc/errors.rs b/src/calc/errors.rs index 37f29d2..168058e 100644 --- a/src/calc/errors.rs +++ b/src/calc/errors.rs @@ -1,4 +1,5 @@ use confy::ConfyError; +use std::error; use std::fmt; pub type CalculatorResult = Result; @@ -21,6 +22,8 @@ pub enum CalculatorError { LoadError(Option), } +impl error::Error for CalculatorError {} + impl fmt::Display for CalculatorError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { diff --git a/src/main.rs b/src/main.rs index f8ba608..4e6732e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,9 +94,14 @@ fn main() -> Result<(), Box> { Span::raw(" to exit, "), Span::styled("h", Style::default().add_modifier(Modifier::BOLD)), Span::raw(format!( - " for help - [{}] [{}]", + " for help - [{}] [{}] [{}]", app.calculator.get_display_mode(), - app.calculator.get_angle_mode() + app.calculator.get_angle_mode(), + if app.calculator.get_save_on_close() { + "W" + } else { + "w" + } )), ] } @@ -253,7 +258,9 @@ fn main() -> Result<(), Box> { e => Engineering\n\ E => Engineering (stack precision)\n\ f => Engineering\n\ - F => Engineering (stack precision)\ + F => Engineering (stack precision)\n\ + w => Do not write settings and stack on quit (default)\n\ + W => Write stack and settings on quit\ ", }, f, @@ -266,7 +273,7 @@ fn main() -> Result<(), Box> { if let Event::Input(key) = events.next()? { app.error_msg = match handle_key(&mut app, &events, key) { // Exit the program - Ok(CalculatorResponse::Quit) => break 'outer Ok(()), + Ok(CalculatorResponse::Quit) => break 'outer, Ok(CalculatorResponse::Continue) => None, Err(e) => Some(format!("{}", e)), }; @@ -278,7 +285,7 @@ fn main() -> Result<(), Box> { Event::Input(key) => { app.error_msg = match handle_key(&mut app, &events, key) { // Exit the program - Ok(CalculatorResponse::Quit) => break 'outer Ok(()), + Ok(CalculatorResponse::Quit) => break 'outer, Ok(CalculatorResponse::Continue) => None, Err(e) => Some(format!("{}", e)), }; @@ -287,6 +294,7 @@ fn main() -> Result<(), Box> { } } } + app.calculator.close().map_err(|e| e.into()) } fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult {