forked from kageru/tracc
use macro for current focus
This commit is contained in:
parent
b52311e840
commit
292aeb417f
|
@ -2,6 +2,7 @@ use super::tracc::ListView;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::from_reader;
|
use serde_json::from_reader;
|
||||||
|
use std::default;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
|
@ -42,6 +43,12 @@ impl fmt::Display for TimePoint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl default::Default for TimePoint {
|
||||||
|
fn default() -> Self {
|
||||||
|
TimePoint::new("")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn read_times(path: &str) -> Option<Vec<TimePoint>> {
|
fn read_times(path: &str) -> Option<Vec<TimePoint>> {
|
||||||
File::open(path)
|
File::open(path)
|
||||||
.ok()
|
.ok()
|
||||||
|
|
49
src/tracc.rs
49
src/tracc.rs
|
@ -45,6 +45,15 @@ impl Tracc {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self) -> Result<(), io::Error> {
|
pub fn run(&mut self) -> Result<(), io::Error> {
|
||||||
|
macro_rules! with_focused {
|
||||||
|
($action: expr $(, $arg: expr)*) => {
|
||||||
|
match self.focus {
|
||||||
|
Focus::Top => $action(&mut self.todos, $($arg,)*),
|
||||||
|
Focus::Bottom => $action(&mut self.times, $($arg,)*),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
let mut inputs = io::stdin().keys();
|
let mut inputs = io::stdin().keys();
|
||||||
loop {
|
loop {
|
||||||
self.refresh()?;
|
self.refresh()?;
|
||||||
|
@ -53,39 +62,21 @@ impl Tracc {
|
||||||
match self.input_mode {
|
match self.input_mode {
|
||||||
Mode::Normal => match input {
|
Mode::Normal => match input {
|
||||||
Key::Char('q') => break,
|
Key::Char('q') => break,
|
||||||
Key::Char('j') => match self.focus {
|
Key::Char('j') => with_focused!(ListView::selection_down),
|
||||||
Focus::Top => self.todos.selection_down(),
|
Key::Char('k') => with_focused!(ListView::selection_up),
|
||||||
Focus::Bottom => self.times.selection_down(),
|
|
||||||
},
|
|
||||||
Key::Char('k') => match self.focus {
|
|
||||||
Focus::Top => self.todos.selection_up(),
|
|
||||||
Focus::Bottom => self.times.selection_up(),
|
|
||||||
},
|
|
||||||
Key::Char('o') => {
|
Key::Char('o') => {
|
||||||
match self.focus {
|
with_focused!(ListView::insert, Default::default(), None);
|
||||||
Focus::Top => self.todos.insert(Default::default(), None),
|
|
||||||
Focus::Bottom => self.times.insert(TimePoint::new(""), None),
|
|
||||||
}
|
|
||||||
self.set_mode(Mode::Insert)?;
|
self.set_mode(Mode::Insert)?;
|
||||||
}
|
}
|
||||||
Key::Char('a') | Key::Char('A') => self.set_mode(Mode::Insert)?,
|
Key::Char('a') | Key::Char('A') => self.set_mode(Mode::Insert)?,
|
||||||
Key::Char(' ') => match self.focus {
|
Key::Char(' ') => with_focused!(ListView::toggle_current),
|
||||||
Focus::Top => self.todos.toggle_current(),
|
|
||||||
Focus::Bottom => self.times.toggle_current(),
|
|
||||||
},
|
|
||||||
// dd
|
// dd
|
||||||
Key::Char('d') => {
|
Key::Char('d') => {
|
||||||
if let Key::Char('d') = inputs.next().unwrap()? {
|
if let Key::Char('d') = inputs.next().unwrap()? {
|
||||||
match self.focus {
|
with_focused!(ListView::remove_current)
|
||||||
Focus::Top => self.todos.remove_current(),
|
|
||||||
Focus::Bottom => self.times.remove_current(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
Key::Char('p') => with_focused!(ListView::paste),
|
||||||
Key::Char('p') => match self.focus {
|
|
||||||
Focus::Top => self.todos.paste(),
|
|
||||||
Focus::Bottom => self.times.paste(),
|
|
||||||
},
|
|
||||||
Key::Char('\t') => {
|
Key::Char('\t') => {
|
||||||
self.focus = match self.focus {
|
self.focus = match self.focus {
|
||||||
Focus::Top => Focus::Bottom,
|
Focus::Top => Focus::Bottom,
|
||||||
|
@ -96,14 +87,8 @@ impl Tracc {
|
||||||
},
|
},
|
||||||
Mode::Insert => match input {
|
Mode::Insert => match input {
|
||||||
Key::Char('\n') | Key::Esc => self.set_mode(Mode::Normal)?,
|
Key::Char('\n') | Key::Esc => self.set_mode(Mode::Normal)?,
|
||||||
Key::Backspace => match self.focus {
|
Key::Backspace => with_focused!(ListView::backspace),
|
||||||
Focus::Top => self.todos.backspace(),
|
Key::Char(x) => with_focused!(ListView::append_to_current, x),
|
||||||
Focus::Bottom => self.times.backspace(),
|
|
||||||
},
|
|
||||||
Key::Char(x) => match self.focus {
|
|
||||||
Focus::Top => self.todos.append_to_current(x),
|
|
||||||
Focus::Bottom => self.times.append_to_current(x),
|
|
||||||
},
|
|
||||||
_ => (),
|
_ => (),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user