More clearly handle quitting

This commit is contained in:
Austen Adler 2021-05-13 22:23:05 -04:00
parent 4e1e256461
commit 426c6850f2

View File

@ -39,6 +39,11 @@ struct App {
current_macro: Option<char>, current_macro: Option<char>,
} }
enum CalculatorResponse {
Quit,
Continue,
}
impl Default for App { impl Default for App {
fn default() -> Self { fn default() -> Self {
let (calculator, error_msg) = match Calculator::load_config() { let (calculator, error_msg) = match Calculator::load_config() {
@ -263,8 +268,8 @@ 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(true) => break 'outer Ok(()), Ok(CalculatorResponse::Quit) => break 'outer Ok(()),
Ok(false) => None, Ok(CalculatorResponse::Continue) => None,
Err(e) => Some(format!("{}", e)), Err(e) => Some(format!("{}", e)),
}; };
} }
@ -274,8 +279,8 @@ 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(true) => break 'outer Ok(()), Ok(CalculatorResponse::Quit) => break 'outer Ok(()),
Ok(false) => None, Ok(CalculatorResponse::Continue) => None,
Err(e) => Some(format!("{}", e)), Err(e) => Some(format!("{}", e)),
}; };
} }
@ -288,11 +293,11 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
} }
fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult<bool> { fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult<CalculatorResponse> {
match (&app.state, app.calculator.get_state()) { match (&app.state, app.calculator.get_state()) {
(AppState::Calculator, CalculatorState::Normal) => match key { (AppState::Calculator, CalculatorState::Normal) => match key {
Key::Char('q') => { Key::Char('q') => {
return Ok(true); return Ok(CalculatorResponse::Quit);
} }
Key::Ctrl('s') => { Key::Ctrl('s') => {
app.calculator.save_config()?; app.calculator.save_config()?;
@ -341,7 +346,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult<bool
_ => {} _ => {}
}, },
} }
Ok(false) Ok(CalculatorResponse::Continue)
} }
struct ClippyRectangle<'a> { struct ClippyRectangle<'a> {