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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
use crate::db;
use crate::window::{Line, Window};
use super::Buffer;
use notmuch::DatabaseMode;
use termion::event::Key;
pub struct Threads {
threads: Vec<Thread>,
i: usize,
query: String,
}
#[derive(Clone)]
pub struct Thread {
subject: String,
authors: Vec<String>,
_id: String,
messages: Vec<String>,
}
impl<'d, 'q> Thread {
pub fn new(thread: notmuch::Thread<'d, 'q>) -> Self {
Self {
subject: thread.subject().to_string(),
authors: thread.authors().clone(),
_id: thread.id().to_string(),
messages: thread.messages().map(|m| m.id().to_string()).collect(),
}
}
pub fn remove_tag(&self, tag: &str) {
let db = db::open(DatabaseMode::ReadWrite).unwrap();
for m_id in self.messages.iter() {
db
.find_message(m_id)
.unwrap()
.unwrap()
.remove_tag(tag)
.unwrap();
}
}
}
impl Threads {
pub fn from_query(query: String) -> Self {
let mut res = Self {
threads: Vec::new(),
i: 0,
query,
};
res.reload();
res
}
pub fn name(&self) -> String {
format!("threads: '{}'", self.query)
}
pub fn reload(&mut self) {
self.threads = db::open(DatabaseMode::ReadOnly)
.unwrap()
.create_query(&self.query)
.unwrap()
.search_threads()
.unwrap()
.map(Thread::new)
.collect();
self.threads.append(&mut self.threads.clone());
}
pub fn fill_window(&self, window: &mut Window) {
for (i, thread) in self.threads.iter().enumerate() {
let s = format!("thread: \'{}\' {:?}", thread.subject, thread.authors);
window.lines.push(
if self.i == i {
Line::Highlight(s)
} else {
Line::Normal(s)
}
);
}
}
pub fn tick(&mut self, window: &mut Window, key: Key) -> Option<Buffer> {
match key {
Key::Char('j') => self.i += 1,
Key::Char('k') => self.i =
if self.i == 0 {
self.threads.len()
} else {
self.i - 1
},
Key::Char('e') => window.scroll_down(self.threads.len()),
Key::Char('y') => window.scroll_up(),
Key::Char('i') => {
self.threads[self.i].remove_tag("inbox");
self.i += 1;
}
Key::Char('s') => {
let sent = Threads::from_query(String::from("tag:sent"));
sent.fill_window(window);
return Some(Buffer::Threads(sent));
}
Key::Char('r') => self.reload(),
_ => (),
}
if !self.threads.is_empty() {
self.i = self.i.rem_euclid(self.threads.len());
}
self.fill_window(window);
None
}
}
|