diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f54ba5..a751697 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,10 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Set up IMAP connection [#3] - List new emails [#6] - Set up CLI arg parser [#15] +- List mailboxes command [#5] [unreleased]: https://github.com/soywod/himalaya [#1]: https://github.com/soywod/himalaya/issues/1 [#2]: https://github.com/soywod/himalaya/issues/2 [#3]: https://github.com/soywod/himalaya/issues/3 +[#5]: https://github.com/soywod/himalaya/issues/5 [#15]: https://github.com/soywod/himalaya/issues/15 diff --git a/src/imap.rs b/src/imap.rs index 9b7f32b..03cdf70 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -147,5 +147,48 @@ pub fn read_emails(imap_sess: &mut ImapSession, mbox: &str, query: &str) -> imap Ok(()) } -// List mailboxes -// let mboxes = imap_sess.list(Some(""), Some("*"))?; +pub fn list_mailboxes(imap_sess: &mut ImapSession) -> imap::Result<()> { + let mboxes = imap_sess.list(Some(""), Some("*"))?; + + let table_head = vec![ + table::Cell::new( + vec![table::BOLD, table::UNDERLINE, table::WHITE], + String::from("DELIM"), + ), + table::Cell::new( + vec![table::BOLD, table::UNDERLINE, table::WHITE], + String::from("NAME"), + ), + table::Cell::new( + vec![table::BOLD, table::UNDERLINE, table::WHITE], + String::from("ATTRIBUTES"), + ), + ]; + + let mut table_rows = mboxes + .iter() + .map(|mbox| { + vec![ + table::Cell::new( + vec![table::BLUE], + mbox.delimiter().unwrap_or("").to_string(), + ), + table::Cell::new(vec![table::GREEN], mbox.name().to_string()), + table::Cell::new( + vec![table::YELLOW], + mbox.attributes() + .iter() + .map(|a| format!("{:?}", a)) + .collect::>() + .join(", "), + ), + ] + }) + .collect::>(); + + table_rows.insert(0, table_head); + + println!("{}", table::render(table_rows)); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 415cd07..f6e23f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,12 +26,6 @@ fn main() { .version("0.1.0") .about("📫 Minimalist CLI email client") .author("soywod ") - .subcommand( - SubCommand::with_name("read") - .about("Reads an email by its UID") - .arg(mailbox_arg()) - .arg(uid_arg()), - ) .subcommand( SubCommand::with_name("query") .about("Prints emails filtered by the given IMAP query") @@ -44,6 +38,13 @@ fn main() { .required(true), ), ) + .subcommand(SubCommand::with_name("list").about("Lists all available mailboxes")) + .subcommand( + SubCommand::with_name("read") + .about("Reads an email by its UID") + .arg(mailbox_arg()) + .arg(uid_arg()), + ) .subcommand(SubCommand::with_name("write").about("Writes a new email")) .subcommand( SubCommand::with_name("forward") @@ -71,15 +72,22 @@ fn main() { if let Some(matches) = matches.values_of("query") { let query = matches .fold((false, vec![]), |(escape, mut cmds), cmd| { - if ["subject", "body", "text"].contains(&cmd.to_lowercase().as_str()) { - cmds.push(cmd.to_string()); - (true, cmds) - } else if escape { - cmds.push(format!("\"{}\"", cmd)); - (false, cmds) - } else { - cmds.push(cmd.to_string()); - (false, cmds) + match (cmd, escape) { + // Next command needs to be escaped + ("subject", _) | ("body", _) | ("text", _) => { + cmds.push(cmd.to_string()); + (true, cmds) + } + // Escaped commands + (_, true) => { + cmds.push(format!("\"{}\"", cmd)); + (false, cmds) + } + // Regular commands + (_, false) => { + cmds.push(cmd.to_string()); + (false, cmds) + } } }) .1 @@ -88,4 +96,8 @@ fn main() { imap::read_emails(&mut imap_sess, &mbox, &query).unwrap(); } } + + if let Some(_) = matches.subcommand_matches("list") { + imap::list_mailboxes(&mut imap_sess).unwrap(); + } }