aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-04-28 23:20:16 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-04-28 23:20:16 +0200
commit49ee6470ad8f748f85bf1c9ad4364d47f8b2252b (patch)
treeb99edc2cc5ff85711590444b0c96ebbd9f6de662
parent91888f054ae12ff36eeced64801c497e44fb7722 (diff)
downloadmail-49ee6470ad8f748f85bf1c9ad4364d47f8b2252b.tar.gz
reload queries
-rw-r--r--src/main.rs2
-rw-r--r--src/state/threads.rs48
2 files changed, 36 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs
index 53d6eaf..1149f83 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -26,7 +26,7 @@ fn main() {
let query = db.create_query("tag:inbox").unwrap();
let threads = query.search_threads().unwrap();
- Threads::new(threads)
+ Threads::from_query(Some(String::from("tag:inbox")), threads)
};
threads.init(&mut screen);
diff --git a/src/state/threads.rs b/src/state/threads.rs
index 4248d93..9d81e09 100644
--- a/src/state/threads.rs
+++ b/src/state/threads.rs
@@ -6,8 +6,10 @@ use std::io::Write;
use termion::{color, event::Key};
pub struct Threads {
- pub threads: Vec<Thread>,
- pub i: isize,
+ threads: Vec<Thread>,
+ i: isize,
+
+ query: Option<String>,
}
pub struct Thread {
@@ -17,7 +19,16 @@ pub struct Thread {
messages: Vec<String>,
}
-impl Thread {
+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() {
@@ -32,17 +43,27 @@ impl Thread {
}
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(),
- messages: t.messages().map(|m| m.id().to_string()).collect(),
- }).collect();
-
+ pub fn from_query(query: Option<String>, threads: notmuch::Threads<'d, 'q>) -> Self {
Self {
- threads,
- i: 0
+ threads: threads.map(Thread::new).collect(),
+ i: 0,
+ query,
+ }
+ }
+
+ pub fn reload(&mut self) {
+ if let Some(query) = &self.query {
+ self.threads = db::open(DatabaseMode::ReadOnly)
+ .unwrap()
+ .create_query(query)
+ .unwrap()
+ .search_threads()
+ .unwrap()
+ .map(Thread::new)
+ .collect();
+ if self.i >= self.threads.len() as isize {
+ self.i = self.threads.len() as isize - 1;
+ }
}
}
@@ -57,6 +78,7 @@ impl<'d, 'q> Threads {
Key::Char('i') => {
self.threads[self.i as usize].remove_tag("inbox");
}
+ Key::Char('r') => self.reload(),
_ => (),
}
self.i = self.i.rem_euclid(self.threads.len() as isize);