From abecc3b586a1d955fb0b1fe8c03132f755c559c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 26 Apr 2021 22:54:32 +0200 Subject: move out thread state --- src/main.rs | 59 ++++++++++++++++------------------------------------------- 1 file changed, 16 insertions(+), 43 deletions(-) (limited to 'src/main.rs') 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>, -} - -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(stdout: &mut W, threads: &Vec, 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(); } -- cgit v1.2.1