From e5f176c8691e778e5de3973fd10321871ce4fdbb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 28 Dec 2019 13:02:48 +0100 Subject: Add imag-mail-unread command Signed-off-by: Matthias Beyer --- bin/domain/imag-mail/src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ bin/domain/imag-mail/src/ui.rs | 13 +++++++++++++ 2 files changed, 50 insertions(+) diff --git a/bin/domain/imag-mail/src/lib.rs b/bin/domain/imag-mail/src/lib.rs index 4bb217ff..d069ee0e 100644 --- a/bin/domain/imag-mail/src/lib.rs +++ b/bin/domain/imag-mail/src/lib.rs @@ -66,6 +66,7 @@ use resiter::Map; use rayon::prelude::*; use libimagmail::store::MailStore; +use libimagmail::mail::Mail; use libimagentryref::util::get_ref_config; use libimagentryref::reference::Config as RefConfig; use libimagrt::runtime::Runtime; @@ -88,6 +89,7 @@ impl ImagApplication for ImagMail { "scan" => scan(&rt), "import-mail" => import_mail(&rt), "list" => list(&rt), + "unread" => unread(&rt), "mail-store" => mail_store(&rt), other => { debug!("Unknown command"); @@ -237,6 +239,41 @@ fn list(rt: &Runtime) -> Result<()> { .map(|_| ()) } +fn unread(rt: &Runtime) -> Result<()> { + let refconfig = get_ref_config(rt, "imag-mail")?; + let scmd = rt.cli().subcommand_matches("unread").unwrap(); // safe via clap + let list_format = util::get_mail_print_format("mail.list_format", rt, &scmd)?; + let mut i = 0; + + if rt.ids_from_stdin() { + let iter = rt + .ids::()? + .ok_or_else(|| err_msg("No ids supplied"))? + .into_iter() + .map(Ok); + + StoreIdIterator::new(Box::new(iter)) + } else { + rt.store().all_mails()?.into_storeid_iter() + } + .inspect(|id| debug!("Found: {:?}", id)) + .into_get_iter(rt.store()) + .map_inner_ok_or_else(|| err_msg("Did not find one entry")) + .and_then_ok(|m| { + if !m.is_seen(&refconfig)? { + let data = util::build_data_object_for_handlebars(i, &m, &refconfig)?; + let s = list_format.render("format", &data)?; + writeln!(rt.stdout(), "{}", s)?; + rt.report_touched(m.get_location())?; + i += 1; + } + + Ok(()) + }) + .collect::>>() + .map(|_| ()) +} + fn mail_store(rt: &Runtime) -> Result<()> { let _ = rt.cli().subcommand_matches("mail-store").unwrap(); Err(format_err!("This feature is currently not implemented.")) diff --git a/bin/domain/imag-mail/src/ui.rs b/bin/domain/imag-mail/src/ui.rs index 2462e15e..324fabb4 100644 --- a/bin/domain/imag-mail/src/ui.rs +++ b/bin/domain/imag-mail/src/ui.rs @@ -93,6 +93,19 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .help("The format to list the mails with")) ) + .subcommand(SubCommand::with_name("unread") + .about("Show unread mail") + .version("0.1") + + .arg(Arg::with_name("unread-read") + .long("read") + .short("r") + .takes_value(false) + .required(false) + .multiple(false) + .help("Print the textual content of the mail itself as well")) + ) + .subcommand(SubCommand::with_name("mail-store") .about("Operations on (subsets of) all mails") .version("0.1") -- cgit v1.2.3