mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-25 17:57:19 +02:00
Refactor app
This commit is contained in:
parent
0cc6946d83
commit
b730c11d36
2 changed files with 43 additions and 27 deletions
36
src/app.rs
36
src/app.rs
|
@ -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)]
|
||||
|
|
34
src/main.rs
34
src/main.rs
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue