use super::State; use std::io::Write; use termion::{color, event::Key}; pub struct Threads { pub threads: Vec, pub i: isize, } pub struct Thread { subject: String, authors: Vec, id: String, } impl<'d, 'q> Threads { pub fn new(threads: notmuch::Threads<'d, 'q>) -> Self { let threads = threads.map(|t| Thread { subject: t.subject().to_string(), authors: t.authors().clone(), id: t.id().to_string(), }).collect(); Self { threads, i: 0 } } pub fn init(&mut self, out: &mut W) { draw(&self, out); } pub fn tick(mut self, out: &mut W, key: Key) -> State { 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(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(); }