mirror of
https://github.com/FliegendeWurst/inboxid.git
synced 2024-11-08 10:20:39 +00:00
overview: unread count of each mailbox
This commit is contained in:
parent
14bc4d15c8
commit
d3120fc252
35
src/bin/overview.rs
Normal file
35
src/bin/overview.rs
Normal 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(())
|
||||
}
|
17
src/lib.rs
17
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<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);
|
||||
|
Loading…
Reference in New Issue
Block a user