Allow saving on quit
This commit is contained in:
parent
d4f9fc904d
commit
f9f26f1ca7
15
src/calc.rs
15
src/calc.rs
@ -36,6 +36,7 @@ impl CalculatorStateChange {
|
|||||||
pub struct Calculator {
|
pub struct Calculator {
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
l: String,
|
l: String,
|
||||||
|
save_on_close: bool,
|
||||||
stack: VecDeque<f64>,
|
stack: VecDeque<f64>,
|
||||||
#[serde(serialize_with = "ordered_char_map")]
|
#[serde(serialize_with = "ordered_char_map")]
|
||||||
macros: CalculatorMacros,
|
macros: CalculatorMacros,
|
||||||
@ -79,6 +80,7 @@ impl Default for Calculator {
|
|||||||
registers: CalculatorRegisters::new(),
|
registers: CalculatorRegisters::new(),
|
||||||
state: CalculatorState::Normal,
|
state: CalculatorState::Normal,
|
||||||
stack: vec![1.0, 2.0].into_iter().collect(),
|
stack: vec![1.0, 2.0].into_iter().collect(),
|
||||||
|
save_on_close: false,
|
||||||
macros: [
|
macros: [
|
||||||
(
|
(
|
||||||
'm',
|
'm',
|
||||||
@ -132,6 +134,14 @@ impl Default for Calculator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Calculator {
|
impl Calculator {
|
||||||
|
pub fn close(&self) -> CalculatorResult<()> {
|
||||||
|
if self.save_on_close {
|
||||||
|
self.save_config()
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_config() -> CalculatorResult<Calculator> {
|
pub fn load_config() -> CalculatorResult<Calculator> {
|
||||||
load(APP_NAME).map_err(|e| CalculatorError::LoadError(Some(e)))
|
load(APP_NAME).map_err(|e| CalculatorError::LoadError(Some(e)))
|
||||||
}
|
}
|
||||||
@ -302,6 +312,8 @@ impl Calculator {
|
|||||||
precision: self.pop_usize()?,
|
precision: self.pop_usize()?,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
'w' => self.save_on_close = false,
|
||||||
|
'W' => self.save_on_close = true,
|
||||||
_ => return Err(CalculatorError::NoSuchSetting(c)),
|
_ => return Err(CalculatorError::NoSuchSetting(c)),
|
||||||
};
|
};
|
||||||
self.state = CalculatorState::Normal;
|
self.state = CalculatorState::Normal;
|
||||||
@ -388,6 +400,9 @@ impl Calculator {
|
|||||||
pub fn get_display_mode(&self) -> &CalculatorDisplayMode {
|
pub fn get_display_mode(&self) -> &CalculatorDisplayMode {
|
||||||
&self.display_mode
|
&self.display_mode
|
||||||
}
|
}
|
||||||
|
pub fn get_save_on_close(&self) -> bool {
|
||||||
|
self.save_on_close
|
||||||
|
}
|
||||||
|
|
||||||
pub fn flush_l(&mut self) -> CalculatorResult<bool> {
|
pub fn flush_l(&mut self) -> CalculatorResult<bool> {
|
||||||
if self.l.is_empty() {
|
if self.l.is_empty() {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use confy::ConfyError;
|
use confy::ConfyError;
|
||||||
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
pub type CalculatorResult<T> = Result<T, CalculatorError>;
|
pub type CalculatorResult<T> = Result<T, CalculatorError>;
|
||||||
@ -21,6 +22,8 @@ pub enum CalculatorError {
|
|||||||
LoadError(Option<ConfyError>),
|
LoadError(Option<ConfyError>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl error::Error for CalculatorError {}
|
||||||
|
|
||||||
impl fmt::Display for CalculatorError {
|
impl fmt::Display for CalculatorError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
18
src/main.rs
18
src/main.rs
@ -94,9 +94,14 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
Span::raw(" to exit, "),
|
Span::raw(" to exit, "),
|
||||||
Span::styled("h", Style::default().add_modifier(Modifier::BOLD)),
|
Span::styled("h", Style::default().add_modifier(Modifier::BOLD)),
|
||||||
Span::raw(format!(
|
Span::raw(format!(
|
||||||
" for help - [{}] [{}]",
|
" for help - [{}] [{}] [{}]",
|
||||||
app.calculator.get_display_mode(),
|
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\n\
|
||||||
E => Engineering (stack precision)\n\
|
E => Engineering (stack precision)\n\
|
||||||
f => Engineering\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,
|
f,
|
||||||
@ -266,7 +273,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
if let Event::Input(key) = events.next()? {
|
if let Event::Input(key) = events.next()? {
|
||||||
app.error_msg = match handle_key(&mut app, &events, key) {
|
app.error_msg = match handle_key(&mut app, &events, key) {
|
||||||
// Exit the program
|
// Exit the program
|
||||||
Ok(CalculatorResponse::Quit) => break 'outer Ok(()),
|
Ok(CalculatorResponse::Quit) => break 'outer,
|
||||||
Ok(CalculatorResponse::Continue) => None,
|
Ok(CalculatorResponse::Continue) => None,
|
||||||
Err(e) => Some(format!("{}", e)),
|
Err(e) => Some(format!("{}", e)),
|
||||||
};
|
};
|
||||||
@ -278,7 +285,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
Event::Input(key) => {
|
Event::Input(key) => {
|
||||||
app.error_msg = match handle_key(&mut app, &events, key) {
|
app.error_msg = match handle_key(&mut app, &events, key) {
|
||||||
// Exit the program
|
// Exit the program
|
||||||
Ok(CalculatorResponse::Quit) => break 'outer Ok(()),
|
Ok(CalculatorResponse::Quit) => break 'outer,
|
||||||
Ok(CalculatorResponse::Continue) => None,
|
Ok(CalculatorResponse::Continue) => None,
|
||||||
Err(e) => Some(format!("{}", e)),
|
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> {
|
fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult<CalculatorResponse> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user