aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 1aa49226e5a5242c1f085deaa478725e2f5d9974 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::io::{Write, stdin, stdout};
use notmuch::Thread;
use termion::color;
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());

    // hide the cursor
    let mut screen = termion::cursor::HideCursor::from(screen);

    // open database
    let db = notmuch::Database::open(&"/home/gustav/.mail", notmuch::DatabaseMode::ReadOnly).unwrap();

    // get threads
    let query = db.create_query("tag:inbox").unwrap();
    let threads = query.search_threads().unwrap().collect();

    let mut state = State::ShowThreads(ShowThreads {
        threads
    });

    let mut i: isize = 0;
    // show_threads(&mut screen, &threads, i as usize);

    for c in stdin.keys() {
        // Global keybinds
        match c.unwrap() {
            Key::Char('q') => break,
            _ => ()
        }

        // Pass to current state
        state = match state {
            State::ShowThreads(s) => s.tick(),
        };
        // 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();
}