Refactor app

This commit is contained in:
Dheepak Krishnamurthy 2020-07-26 17:00:24 -06:00
parent 0cc6946d83
commit b730c11d36
2 changed files with 43 additions and 27 deletions

View file

@ -23,6 +23,10 @@ use tui::{
Terminal,
};
use termion::event::Key;
use crate::util::{Config, Event, Events};
pub fn cmp(t1: &Task, t2: &Task) -> Ordering {
let urgency1 = match &t1.uda()["urgency"] {
UDAValue::Str(_) => 0.0,
@ -366,6 +370,38 @@ impl App {
_ => (),
}
}
pub fn handle_tick(&mut self) {
self.update();
}
pub fn handle_input(&mut self, event: ::termion::event::Key) {
match self.input_mode {
InputMode::Normal => match event {
Key::Ctrl('c') | Key::Char('q') => self.should_quit = true,
Key::Char('r') => self.update(),
Key::Down | Key::Char('j') => self.next(),
Key::Up | Key::Char('k') => self.previous(),
Key::Char('i') => {
self.input_mode = InputMode::Command;
}
_ => {}
},
InputMode::Command => match event {
Key::Char('\n') | Key::Esc => {
self.input_mode = InputMode::Normal;
}
Key::Char(c) => {
self.filter.push(c);
}
Key::Backspace => {
self.filter.pop();
}
_ => {}
},
}
}
}
#[cfg(test)]

View file

@ -49,33 +49,13 @@ fn main() -> Result<(), Box<dyn Error>> {
terminal.draw(|mut frame| app.draw(&mut frame)).unwrap();
// Handle input
if let Event::Input(input) = events.next()? {
match app.input_mode {
InputMode::Normal => match input {
Key::Ctrl('c') | Key::Char('q') => break,
Key::Char('r') => app.update(),
Key::Down | Key::Char('j') => app.next(),
Key::Up | Key::Char('k') => app.previous(),
Key::Char('i') => {
app.input_mode = InputMode::Command;
}
_ => {}
},
InputMode::Command => match input {
Key::Char('\n') | Key::Esc => {
app.input_mode = InputMode::Normal;
}
Key::Char(c) => {
app.filter.push(c);
app.update();
}
Key::Backspace => {
app.filter.pop();
app.update();
}
_ => {}
},
}
match events.next()? {
Event::Input(input) => app.handle_input(input),
Event::Tick => app.handle_tick(),
}
if app.should_quit {
break
}
}