Skip to content

Commit

Permalink
Separate expression box handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mroik committed May 7, 2024
1 parent 103cd74 commit 18cd9f9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 42 deletions.
92 changes: 50 additions & 42 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crossterm::event::{self, Event, KeyCode, KeyEventKind};

use crate::{
fitch::Fitch,
parser::{self, parse_expression},
ui::Renderer,
};
use crossterm::event::{self, Event, KeyCode, KeyEventKind};

pub struct App {
model: Fitch,
Expand Down Expand Up @@ -67,51 +66,33 @@ impl App {
}

fn listen_add_subproof(&mut self, code: &KeyCode) {
match code {
KeyCode::Enter => match parse_expression(&self.expression_buffer) {
parser::Result::Failure => {
self.info_buffer.push_str("Expression entered is invalid")
}
parser::Result::Success(expr, _) => {
self.model.add_subproof(&expr);
self.state = State::Noraml;
self.expression_buffer.clear();
}
},
KeyCode::Backspace if !self.expression_buffer.is_empty() => {
self.expression_buffer.pop();
let handler = |app_context: &mut App, result: parser::Result| match result {
parser::Result::Failure => app_context
.info_buffer
.push_str("Expression entered is invalid"),
parser::Result::Success(expr, _) => {
app_context.model.add_subproof(&expr);
app_context.state = State::Noraml;
app_context.expression_buffer.clear();
}
KeyCode::Char(c) => self.expression_buffer.push(*c),
KeyCode::Esc => {
self.expression_buffer.clear();
self.state = State::Noraml
}
_ => (),
}
};

self.handle_expression_box_event(code, handler);
}

fn listen_add_assumption(&mut self, code: &KeyCode) {
match code {
KeyCode::Enter => match parse_expression(&self.expression_buffer) {
parser::Result::Failure => {
self.info_buffer.push_str("Expression entered is invalid")
}
parser::Result::Success(expr, _) => {
self.model.add_assumption(&expr);
self.state = State::Noraml;
self.expression_buffer.clear();
}
},
KeyCode::Backspace if !self.expression_buffer.is_empty() => {
self.expression_buffer.pop();
let handler = |app_context: &mut App, result: parser::Result| match result {
parser::Result::Failure => app_context
.info_buffer
.push_str("Expression entered is invalid"),
parser::Result::Success(expr, _) => {
app_context.model.add_assumption(&expr);
app_context.state = State::Noraml;
app_context.expression_buffer.clear();
}
KeyCode::Char(c) => self.expression_buffer.push(*c),
KeyCode::Esc => {
self.expression_buffer.clear();
self.state = State::Noraml
}
_ => (),
}
};

self.handle_expression_box_event(code, handler);
}

fn listen_normal(&mut self, code: &KeyCode) {
Expand All @@ -127,6 +108,32 @@ impl App {
}
}

// Not sure if it avoids much code duplication. It also requires a clone thus another
// allocation. If it starts to become bothersome just revert this code section to
// 103cd74bd33e8bb550512cb745b2ff6bf89e35e1
fn handle_expression_box_event(
&mut self,
code: &KeyCode,
mut handler: impl FnMut(&mut App, parser::Result),
) {
match code {
KeyCode::Enter => {
let buf = self.expression_buffer.clone();
let res = parse_expression(&buf);
handler(self, res);
}
KeyCode::Backspace if !self.expression_buffer.is_empty() => {
self.expression_buffer.pop();
}
KeyCode::Char(c) => self.expression_buffer.push(*c),
KeyCode::Esc => {
self.expression_buffer.clear();
self.state = State::Noraml
}
_ => (),
}
}

fn info_text(&self) -> String {
match self.state {
State::Noraml => [
Expand All @@ -141,6 +148,7 @@ impl App {
.join(" ")
.to_string(),
State::AddAssumption => self.info_buffer.clone(),
State::AddSubproof => self.info_buffer.clone(),
_ => "".to_string(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::fitch::Proposition;
use std::rc::Rc;

#[derive(Clone)]
pub enum Result<'a> {
Success(Rc<Proposition>, &'a str),
Failure,
Expand Down

0 comments on commit 18cd9f9

Please sign in to comment.