From d3120fc252886cf06110ce089a21f07f4ff59ed7 Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu+github@posteo.de> Date: Wed, 14 Apr 2021 17:47:10 +0200 Subject: [PATCH] overview: unread count of each mailbox --- src/bin/overview.rs | 35 +++++++++++++++++++++++++++++++++++ src/lib.rs | 17 ++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/bin/overview.rs diff --git a/src/bin/overview.rs b/src/bin/overview.rs new file mode 100644 index 0000000..ce832d2 --- /dev/null +++ b/src/bin/overview.rs @@ -0,0 +1,35 @@ +use std::array::IntoIter; + +use ascii_table::{AsciiTable, Align, Column}; +use inboxid::*; + +fn main() -> Result<()> { + let mut dirs = get_maildirs()?; + dirs.sort_unstable(); + let mut rows = vec![]; + for dir in dirs { + let maildir = get_maildir(&dir)?; + let unread = maildir + .list_cur() + .map(|x| if x.map(|x| !x.flags().contains(SEEN)).unwrap_or(true) { 1 } else { 0 }) + .sum::(); + if unread > 0 { + rows.push(IntoIter::new([dir, unread.to_string()])); + } + } + let mut ascii_table = AsciiTable::default(); + ascii_table.draw_lines = false; + ascii_table.max_width = usize::MAX; + for (i, &(header, align)) in [ + ("Mailbox", Align::Left), + ("Unread", Align::Right), + ].iter().enumerate() { + let mut column = Column::default(); + column.header = header.to_owned(); + column.align = align; + column.max_width = usize::MAX; + ascii_table.columns.insert(i, column); + } + ascii_table.print(rows); // prints a 0 if empty :) + Ok(()) +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 6e2f2c1..773d63e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, convert::{TryFrom, TryInto}, env, fmt::{Debug, Display}, fs, hash::Hash, io, net::TcpStream, ops::Deref, path::PathBuf}; -use anyhow::Context; +use anyhow::{anyhow, Context}; use chrono::{DateTime, Local, NaiveDateTime, TimeZone}; use cursive::{theme::{BaseColor, Color, ColorStyle, ColorType, Effect, Style}, utils::span::{IndexedCow, IndexedSpan, SpannedString}}; use cursive_tree_view::TreeEntry; @@ -43,6 +43,21 @@ pub fn connect(host: &str, port: u16, user: &str, password: &str) -> Result Result> { + let maildir = env::var("MAILDIR").expect("missing envvar MAILDIR"); + let mut dirs = vec![]; + for dir in fs::read_dir(&maildir)? { + let dir = dir?; + if dir.file_type()?.is_dir() { + let name = dir.file_name().into_string().map_err(|_| anyhow!("failed to decode directory name"))?; + if !name.starts_with('.') { + dirs.push(name); + } + } + } + Ok(dirs) +} + pub fn get_maildir(mailbox: &str) -> Result { let maildir = env::var("MAILDIR").expect("missing envvar MAILDIR"); let maildir = format!("{}/{}", maildir, mailbox);