aboutsummaryrefslogtreecommitdiffstats
path: root/src/state/threads.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/threads.rs')
-rw-r--r--src/state/threads.rs114
1 files changed, 0 insertions, 114 deletions
diff --git a/src/state/threads.rs b/src/state/threads.rs
deleted file mode 100644
index be566e5..0000000
--- a/src/state/threads.rs
+++ /dev/null
@@ -1,114 +0,0 @@
-use crate::db;
-use super::State;
-
-use notmuch::DatabaseMode;
-use std::io::Write;
-use termion::{color, event::Key};
-
-pub struct Threads {
- threads: Vec<Thread>,
- i: usize,
-
- query: String,
-}
-
-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 reload(&mut self) {
- self.threads = db::open(DatabaseMode::ReadOnly)
- .unwrap()
- .create_query(&self.query)
- .unwrap()
- .search_threads()
- .unwrap()
- .map(Thread::new)
- .collect();
- }
-
- pub fn init<W: Write>(&self, out: &mut W) {
- draw(&self, out);
- }
-
- pub fn tick<W: Write>(&mut self, out: &mut W, key: Key) -> Option<State> {
- 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('i') => {
- self.threads[self.i].remove_tag("inbox");
- }
- Key::Char('s') => {
- let sent = Threads::from_query(String::from("tag:sent"));
- sent.init(out);
- return Some(State::Threads(sent));
- }
- Key::Char('r') => self.reload(),
- _ => (),
- }
- if !self.threads.is_empty() {
- self.i = self.i.rem_euclid(self.threads.len());
- }
- draw(&self, out);
- None
- }
-}
-
-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;
- 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();
-}