aboutsummaryrefslogtreecommitdiffstats
path: root/src/state/threads.rs
blob: 20b365eca49c1ce2f45981c3c421fed6289781e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
}