Fix break

This commit is contained in:
Austen Adler 2021-04-26 23:39:47 -04:00
parent d18b995228
commit 5762fd868a
2 changed files with 84 additions and 75 deletions

View File

@ -9,7 +9,6 @@ use calc::operations::CalculatorOperation;
use calc::Calculator; use calc::Calculator;
use std::cmp; use std::cmp;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::process;
use util::event::{Event, Events}; use util::event::{Event, Events};
//use util::event::T; //use util::event::T;
use std::{error::Error, io}; use std::{error::Error, io};
@ -72,7 +71,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let events = Events::new(); let events = Events::new();
let mut app = App::default(); let mut app = App::default();
loop { 'outer: loop {
terminal.draw(|f| { terminal.draw(|f| {
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
@ -218,12 +217,19 @@ fn main() -> Result<(), Box<dyn Error>> {
})?; })?;
if let Event::Input(key) = events.next()? { 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() { for e in events.try_iter() {
match e { 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, Event::MacroEnd => app.current_macro = None,
_ => continue, _ => continue,
} }
@ -232,80 +238,18 @@ fn main() -> Result<(), Box<dyn Error>> {
app.current_macro = None; app.current_macro = None;
} }
// TODO: Bubble up return Ok so we can handle saving // TODO: Bubble up return Ok so we can handle saving
//Ok(()) Ok(())
} }
fn calc_operation(app: &mut App, c: char) { fn handle_key(app: &mut App, events: &Events, key: Key) -> bool {
if let Ok(op) = CalculatorOperation::from_char(c) {
if let Ok(f) = app.input.parse::<f64>() {
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<T: std::io::Write>(c: ClippyRectangle, f: &mut Frame<TermionBackend<T>>) {
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) {
match &app.state { match &app.state {
AppState::Calculator => { AppState::Calculator => {
app.error_msg = None; app.error_msg = None;
match key { match key {
Key::Char('q') => { Key::Char('q') => {
process::exit(0); //process::exit(0);
print!("XX");
return true;
} }
Key::Ctrl('c') => { Key::Ctrl('c') => {
app.state = AppState::Constants; 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() { if let Ok(x) = app.calculator.pop() {
app.input = x.to_string(); app.input = x.to_string();
} else { } 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() { if app.calculator.push(f).is_ok() {
app.input.clear(); app.input.clear();
} else { } else {
return; return false;
} }
} else { } 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::<f64>() {
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<T: std::io::Write>(c: ClippyRectangle, f: &mut Frame<TermionBackend<T>>) {
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);
} }

View File

@ -38,7 +38,7 @@ impl Default for Config {
fn default() -> Config { fn default() -> Config {
Config { Config {
exit_key: Key::Char('q'), exit_key: Key::Char('q'),
tick_rate: Duration::from_millis(1000), tick_rate: Duration::from_millis(250),
} }
} }
} }