Allow saving on quit

This commit is contained in:
Austen Adler 2021-05-13 23:03:39 -04:00
parent d4f9fc904d
commit f9f26f1ca7
3 changed files with 31 additions and 5 deletions

View File

@ -36,6 +36,7 @@ impl CalculatorStateChange {
pub struct Calculator {
#[serde(skip)]
l: String,
save_on_close: bool,
stack: VecDeque<f64>,
#[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<Calculator> {
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<bool> {
if self.l.is_empty() {

View File

@ -1,4 +1,5 @@
use confy::ConfyError;
use std::error;
use std::fmt;
pub type CalculatorResult<T> = Result<T, CalculatorError>;
@ -21,6 +22,8 @@ pub enum CalculatorError {
LoadError(Option<ConfyError>),
}
impl error::Error for CalculatorError {}
impl fmt::Display for CalculatorError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {

View File

@ -94,9 +94,14 @@ fn main() -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
}
}
}
app.calculator.close().map_err(|e| e.into())
}
fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult<CalculatorResponse> {