add list mailboxes cmd

This commit is contained in:
Clément DOUIN 2021-01-03 10:59:51 +01:00
parent d94b86ca4e
commit 187b886a1c
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
3 changed files with 74 additions and 17 deletions

View file

@ -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

View file

@ -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::<Vec<_>>()
.join(", "),
),
]
})
.collect::<Vec<_>>();
table_rows.insert(0, table_head);
println!("{}", table::render(table_rows));
Ok(())
}

View file

@ -26,12 +26,6 @@ fn main() {
.version("0.1.0")
.about("📫 Minimalist CLI email client")
.author("soywod <clement.douin@posteo.net>")
.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();
}
}