diff --git a/src/main.rs b/src/main.rs index 322ab03..6d5db6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,6 @@ use calc::operations::CalculatorOperation; use calc::Calculator; use std::cmp; use std::convert::TryFrom; -use std::process; use util::event::{Event, Events}; //use util::event::T; use std::{error::Error, io}; @@ -72,7 +71,7 @@ fn main() -> Result<(), Box> { let events = Events::new(); let mut app = App::default(); - loop { + 'outer: loop { terminal.draw(|f| { let chunks = Layout::default() .direction(Direction::Vertical) @@ -218,12 +217,19 @@ fn main() -> Result<(), Box> { })?; if let Event::Input(key) = events.next()? { - handle_key(&mut app, &events, key); + if handle_key(&mut app, &events, key) { + break 'outer; + }; } for e in events.try_iter() { match e { - Event::Input(key) => handle_key(&mut app, &events, key), + Event::Input(key) => { + if handle_key(&mut app, &events, key) { + //return Ok(()); + break 'outer; + } + } Event::MacroEnd => app.current_macro = None, _ => continue, } @@ -232,80 +238,18 @@ fn main() -> Result<(), Box> { app.current_macro = None; } // TODO: Bubble up return Ok so we can handle saving - //Ok(()) + Ok(()) } -fn calc_operation(app: &mut App, c: char) { - if let Ok(op) = CalculatorOperation::from_char(c) { - if let Ok(f) = app.input.parse::() { - if app.calculator.push(f).is_ok() { - app.input.clear(); - } - } - - app.error_msg = match app.calculator.op(op) { - Err(e) => Some(e.message()), - Ok(()) => None, - } - } -} - -struct ClippyRectangle<'a> { - title: &'a str, - msg: &'a str, -} - -impl ClippyRectangle<'_> { - // TODO: Make this static somehow - fn size(&self) -> Dimensions { - let (width, height) = self.msg.lines().fold((0, 0), |(width, height), l| { - (cmp::max(width, l.len()), height + 1) - }); - Dimensions { - width: u16::try_from(width).unwrap_or(u16::MAX), - height: u16::try_from(height).unwrap_or(u16::MAX), - } - } -} - -fn draw_clippy_rect(c: ClippyRectangle, f: &mut Frame>) { - let block = Block::default().title(c.title).borders(Borders::ALL); - let dimensions = c.size(); - let popup_layout = Layout::default() - .direction(Direction::Vertical) - .constraints( - [ - Constraint::Min(1), - Constraint::Length(dimensions.height + 2), - ] - .as_ref(), - ) - .split(f.size()); - let area = Layout::default() - .direction(Direction::Horizontal) - .constraints( - [ - Constraint::Min(1), - Constraint::Length(cmp::max(dimensions.width, c.title.len() as u16) + 2), - ] - .as_ref(), - ) - .split(popup_layout[1])[1]; - f.render_widget(Clear, area); - - let help_message = Paragraph::new(c.msg) - .style(Style::default()) - .block(Block::default().borders(Borders::ALL).title(c.title)); - f.render_widget(help_message, area); -} - -fn handle_key(app: &mut App, events: &Events, key: Key) { +fn handle_key(app: &mut App, events: &Events, key: Key) -> bool { match &app.state { AppState::Calculator => { app.error_msg = None; match key { Key::Char('q') => { - process::exit(0); + //process::exit(0); + print!("XX"); + return true; } Key::Ctrl('c') => { app.state = AppState::Constants; @@ -330,7 +274,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) { if let Ok(x) = app.calculator.pop() { app.input = x.to_string(); } else { - return; + return false; } } @@ -435,10 +379,10 @@ fn handle_key(app: &mut App, events: &Events, key: Key) { if app.calculator.push(f).is_ok() { app.input.clear(); } else { - return; + return false; } } else { - return; + return false; } } @@ -456,4 +400,69 @@ fn handle_key(app: &mut App, events: &Events, key: Key) { _ => {} }, } + return false; +} + +fn calc_operation(app: &mut App, c: char) { + if let Ok(op) = CalculatorOperation::from_char(c) { + if let Ok(f) = app.input.parse::() { + if app.calculator.push(f).is_ok() { + app.input.clear(); + } + } + + app.error_msg = match app.calculator.op(op) { + Err(e) => Some(e.message()), + Ok(()) => None, + } + } +} + +struct ClippyRectangle<'a> { + title: &'a str, + msg: &'a str, +} + +impl ClippyRectangle<'_> { + // TODO: Make this static somehow + fn size(&self) -> Dimensions { + let (width, height) = self.msg.lines().fold((0, 0), |(width, height), l| { + (cmp::max(width, l.len()), height + 1) + }); + Dimensions { + width: u16::try_from(width).unwrap_or(u16::MAX), + height: u16::try_from(height).unwrap_or(u16::MAX), + } + } +} + +fn draw_clippy_rect(c: ClippyRectangle, f: &mut Frame>) { + let block = Block::default().title(c.title).borders(Borders::ALL); + let dimensions = c.size(); + let popup_layout = Layout::default() + .direction(Direction::Vertical) + .constraints( + [ + Constraint::Min(1), + Constraint::Length(dimensions.height + 2), + ] + .as_ref(), + ) + .split(f.size()); + let area = Layout::default() + .direction(Direction::Horizontal) + .constraints( + [ + Constraint::Min(1), + Constraint::Length(cmp::max(dimensions.width, c.title.len() as u16) + 2), + ] + .as_ref(), + ) + .split(popup_layout[1])[1]; + f.render_widget(Clear, area); + + let help_message = Paragraph::new(c.msg) + .style(Style::default()) + .block(Block::default().borders(Borders::ALL).title(c.title)); + f.render_widget(help_message, area); } diff --git a/src/util/event.rs b/src/util/event.rs index fdcb282..d032ae3 100644 --- a/src/util/event.rs +++ b/src/util/event.rs @@ -38,7 +38,7 @@ impl Default for Config { fn default() -> Config { Config { exit_key: Key::Char('q'), - tick_rate: Duration::from_millis(1000), + tick_rate: Duration::from_millis(250), } } }