overview: unread count of each mailbox

This commit is contained in:
FliegendeWurst 2021-04-14 17:47:10 +02:00 committed by Arne Keller
parent 14bc4d15c8
commit d3120fc252
2 changed files with 51 additions and 1 deletions

35
src/bin/overview.rs Normal file
View File

@ -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::<usize>();
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(())
}

View File

@ -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<Imap
Ok(client.login(user, password).map_err(|e| e.0)?)
}
pub fn get_maildirs() -> Result<Vec<String>> {
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<Maildir> {
let maildir = env::var("MAILDIR").expect("missing envvar MAILDIR");
let maildir = format!("{}/{}", maildir, mailbox);