summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-02-28 15:05:36 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-06-01 13:58:11 +0200
commit4628ec6ee93b5fd21c39eab6161708a94d58696f (patch)
tree91e0ed7e5eee732d4ad4d84f53418707f7068632
parent2a0357e325db162acbbb5fad7f1ce22d6de309a2 (diff)
Collect all mails before printing
This patch changes the algorithm for printing the mailtrees. It does a unique().sort() before actually printing, so that mails apparing in two trees are not printed twice. This is an experimental change, not sure whether it works as intended. Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--bin/domain/imag-mail/Cargo.toml1
-rw-r--r--bin/domain/imag-mail/src/lib.rs60
2 files changed, 27 insertions, 34 deletions
diff --git a/bin/domain/imag-mail/Cargo.toml b/bin/domain/imag-mail/Cargo.toml
index 91eb1e40..1a195a3b 100644
--- a/bin/domain/imag-mail/Cargo.toml
+++ b/bin/domain/imag-mail/Cargo.toml
@@ -24,6 +24,7 @@ log = "0.4.6"
anyhow = "1"
resiter = "0.4"
handlebars = "2"
+itertools = "0.8"
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
diff --git a/bin/domain/imag-mail/src/lib.rs b/bin/domain/imag-mail/src/lib.rs
index 30df9d0c..720f7a93 100644
--- a/bin/domain/imag-mail/src/lib.rs
+++ b/bin/domain/imag-mail/src/lib.rs
@@ -40,6 +40,7 @@ extern crate clap;
extern crate toml_query;
extern crate resiter;
extern crate handlebars;
+extern crate itertools;
extern crate libimagrt;
extern crate libimagmail;
@@ -59,12 +60,12 @@ use resiter::IterInnerOkOrElse;
use resiter::Filter;
use resiter::Map;
use handlebars::Handlebars;
+use itertools::Itertools;
use libimagmail::store::MailStore;
use libimagmail::store::Sorting;
use libimagmail::mail::Mail;
use libimagmail::store::MailStoreWithConnection;
-use libimagmail::mailtree::Mailtree;
use libimagmail::notmuch::connection::NotmuchConnection;
use libimagrt::runtime::Runtime;
use libimagrt::application::ImagApplication;
@@ -195,7 +196,6 @@ fn list(rt: &Runtime) -> Result<()> {
/// This function implements the tree-style printing functionality ("--tree").
fn process_tree<'a, I>(iter: I,
mailstore: &'a MailStoreWithConnection<'a>,
- i: &mut usize,
conn: &NotmuchConnection,
list_format: &Handlebars,
rt: &Runtime,
@@ -238,9 +238,28 @@ fn list(rt: &Runtime) -> Result<()> {
.collect::<Result<Vec<_>>>()?;
trace!("mss = {:?}", mss);
- mss.into_iter()
- .map(|mt| print_traverse(&mailstore, mt, i, conn, list_format, rt, out))
- .collect::<Result<Vec<_>>>()
+ mss.iter()
+ .map(|mailtree| mailtree.traverse())
+ .flatten()
+ .unique_by(|tpl| tpl.1.clone())
+ .sorted_by(|a, b| a.0.cmp(&b.0))
+ .enumerate()
+ .map(|(i, (indent, mid))| {
+ let fle = mailstore.get_mail_by_id(&mid)?.ok_or_else(|| format_err!("Cannot find mail with id = {}", mid))?;
+ trace!("Printing: {}", fle.get_location());
+
+ let loaded = fle.load(&conn)?
+ .ok_or_else(|| format_err!("Mail not found: {}", fle.get_location()))?;
+ trace!("Loaded: {}", fle.get_location());
+
+ let parsed = loaded.parsed()?;
+ trace!("Parsed: {}", fle.get_location());
+
+ crate::util::list_mail(&parsed, i, indent, list_format, out)?;
+ trace!("Listed: {}", fle.get_location());
+ rt.report_touched(fle.get_location())
+ })
+ .collect::<Result<Vec<()>>>()
.map(|_| ())
}
@@ -258,7 +277,7 @@ fn list(rt: &Runtime) -> Result<()> {
.map(Ok);
if tree {
- process_tree(iter, &mailstore, &mut i, &notmuch_connection, &list_format, rt, &mut out, find_root)
+ process_tree(iter, &mailstore, &notmuch_connection, &list_format, rt, &mut out, find_root)
} else {
process_nontree(iter, &mut i, &notmuch_connection, &list_format, rt, &mut out)
}
@@ -276,7 +295,7 @@ fn list(rt: &Runtime) -> Result<()> {
if tree {
let mailstore = store.with_connection(&notmuch_connection);
- process_tree(iter, &mailstore, &mut i, &notmuch_connection, &list_format, rt, &mut out, find_root)
+ process_tree(iter, &mailstore, &notmuch_connection, &list_format, rt, &mut out, find_root)
} else {
process_nontree(iter, &mut i, &notmuch_connection, &list_format, rt, &mut out)
}
@@ -300,30 +319,3 @@ fn print_id(rt: &Runtime) -> Result<()> {
.map(|_| ())
}
-fn print_traverse<'a>(store: &'a MailStoreWithConnection<'a>,
- tree: Mailtree,
- i: &mut usize,
- conn: &NotmuchConnection,
- list_format: &Handlebars,
- rt: &Runtime,
- out: &mut dyn Write) -> Result<()> {
- trace!("Print-Traversing starting with: {:?}", tree.root());
- for (indent, mid) in tree.traverse() {
- let fle = store.get_mail_by_id(&mid)?.ok_or_else(|| format_err!("Cannot find mail with id = {}", mid))?;
- trace!("Printing: {}", fle.get_location());
-
- let loaded = fle.load(&conn)?
- .ok_or_else(|| format_err!("Mail not found: {}", fle.get_location()))?;
- trace!("Loaded: {}", fle.get_location());
-
- let parsed = loaded.parsed()?;
- trace!("Parsed: {}", fle.get_location());
-
- crate::util::list_mail(&parsed, *i, indent, list_format, out)?;
- trace!("Listed: {}", fle.get_location());
- rt.report_touched(fle.get_location())?;
- *i += 1; // poor mans enumerate()
- }
-
- Ok(())
-}