aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main.rs59
-rw-r--r--src/state/mod.rs7
-rw-r--r--src/state/threads.rs43
3 files changed, 66 insertions, 43 deletions
diff --git a/src/main.rs b/src/main.rs
index 1aa4922..7d29d72 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,25 +1,14 @@
-use std::io::{Write, stdin, stdout};
-use notmuch::Thread;
-use termion::color;
+mod state;
+
+use crate::state::State;
+use crate::state::Threads;
+
+use std::io::{stdin, stdout};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;
-struct ShowThreads<'d, 'q> {
- threads: Vec<Thread<'d, 'q>>,
-}
-
-impl<'d, 'q> ShowThreads<'d, 'q> {
- fn tick(self) -> State<'d, 'q> {
- State::ShowThreads(self)
- }
-}
-
-enum State<'d, 'q> {
- ShowThreads(ShowThreads<'d, 'q>),
-}
-
fn main() {
let stdin = stdin();
let screen = AlternateScreen::from(stdout().into_raw_mode().unwrap());
@@ -34,42 +23,26 @@ fn main() {
let query = db.create_query("tag:inbox").unwrap();
let threads = query.search_threads().unwrap().collect();
- let mut state = State::ShowThreads(ShowThreads {
- threads
- });
+ // init initial state
+ let mut threads = Threads {
+ threads,
+ i: 0,
+ };
+ threads.init(&mut screen);
- let mut i: isize = 0;
- // show_threads(&mut screen, &threads, i as usize);
+ let mut state = State::Threads(threads);
for c in stdin.keys() {
+ let c = c.unwrap();
// Global keybinds
- match c.unwrap() {
+ match c {
Key::Char('q') => break,
_ => ()
}
// Pass to current state
state = match state {
- State::ShowThreads(s) => s.tick(),
+ State::Threads(s) => s.tick(&mut screen, c),
};
- // i = i.rem_euclid(threads.len() as isize);
- // show_threads(&mut screen, &threads, i as usize);
- }
-}
-
-fn show_threads<W: Write>(stdout: &mut W, threads: &Vec<Thread>, highlight: usize) {
- write!(stdout, "{}", termion::clear::All).unwrap();
-
- for (i, thread) in threads.iter().enumerate() {
- write!(stdout, "{}", termion::cursor::Goto(1, (i + 1) as u16)).unwrap();
- let highlight = highlight == i;
- if highlight {
- write!(stdout, "{}", color::Fg(color::Red)).unwrap();
- }
- write!(stdout, "thread {:?}, {:?}", thread.subject(), thread.authors()).unwrap();
- if highlight {
- write!(stdout, "{}", color::Fg(color::Reset)).unwrap();
- }
}
- stdout.flush().unwrap();
}
diff --git a/src/state/mod.rs b/src/state/mod.rs
new file mode 100644
index 0000000..fc563e7
--- /dev/null
+++ b/src/state/mod.rs
@@ -0,0 +1,7 @@
+mod threads;
+
+pub use threads::Threads;
+
+pub enum State<'d, 'q> {
+ Threads(Threads<'d, 'q>),
+}
diff --git a/src/state/threads.rs b/src/state/threads.rs
new file mode 100644
index 0000000..20b365e
--- /dev/null
+++ b/src/state/threads.rs
@@ -0,0 +1,43 @@
+use super::State;
+
+use std::io::Write;
+use termion::{color, event::Key};
+
+pub struct Threads<'d, 'q> {
+ pub threads: Vec<notmuch::Thread<'d, 'q>>,
+ pub i: isize,
+}
+
+impl<'d, 'q> Threads<'d, 'q> {
+ pub fn init<W: Write>(&mut self, out: &mut W) {
+ draw(&self, out);
+ }
+
+ pub fn tick<W: Write>(mut self, out: &mut W, key: Key) -> State<'d, 'q> {
+ match key {
+ Key::Char('j') => self.i += 1,
+ Key::Char('k') => self.i -= 1,
+ _ => (),
+ }
+ self.i = self.i.rem_euclid(self.threads.len() as isize);
+ draw(&self, out);
+ State::Threads(self)
+ }
+}
+
+fn draw<W: Write>(state: &Threads, out: &mut W) {
+ write!(out, "{}", termion::clear::All).unwrap();
+
+ for (i, thread) in state.threads.iter().enumerate() {
+ write!(out, "{}", termion::cursor::Goto(1, (i + 1) as u16)).unwrap();
+ let highlight = i == state.i as usize;
+ if highlight {
+ write!(out, "{}", color::Fg(color::Red)).unwrap();
+ }
+ write!(out, "thread {:?}, {:?}", thread.subject(), thread.authors()).unwrap();
+ if highlight {
+ write!(out, "{}", color::Fg(color::Reset)).unwrap();
+ }
+ }
+ out.flush().unwrap();
+}