improve attachments command (#281)

Also fixed a printer typo.
This commit is contained in:
Clément DOUIN
2022-03-04 23:05:01 +01:00
parent 130ed24a5a
commit 212f5e6eb1
18 changed files with 148 additions and 118 deletions
+3 -3
View File
@@ -16,7 +16,7 @@ pub fn add<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
backend: Box<&'a mut B>,
) -> Result<()> {
backend.add_flags(mbox, seq_range, flags)?;
printer.print(format!(
printer.print_struct(format!(
"Flag(s) {:?} successfully added to message(s) {:?}",
flags, seq_range
))
@@ -32,7 +32,7 @@ pub fn remove<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
backend: Box<&'a mut B>,
) -> Result<()> {
backend.del_flags(mbox, seq_range, flags)?;
printer.print(format!(
printer.print_struct(format!(
"Flag(s) {:?} successfully removed from message(s) {:?}",
flags, seq_range
))
@@ -48,7 +48,7 @@ pub fn set<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
backend: Box<&'a mut B>,
) -> Result<()> {
backend.set_flags(mbox, seq_range, flags)?;
printer.print(format!(
printer.print_struct(format!(
"Flag(s) {:?} successfully set for message(s) {:?}",
flags, seq_range
))
+4 -3
View File
@@ -367,7 +367,7 @@ impl Msg {
.unwrap_or(DEFAULT_SENT_FOLDER);
backend.add_msg(&sent_folder, &sent_msg.formatted(), "seen")?;
msg_utils::remove_local_draft()?;
printer.print("Message successfully sent")?;
printer.print_struct("Message successfully sent")?;
break;
}
Ok(PostEditChoice::Edit) => {
@@ -375,7 +375,7 @@ impl Msg {
continue;
}
Ok(PostEditChoice::LocalDraft) => {
printer.print("Message successfully saved locally")?;
printer.print_struct("Message successfully saved locally")?;
break;
}
Ok(PostEditChoice::RemoteDraft) => {
@@ -387,7 +387,8 @@ impl Msg {
.unwrap_or(DEFAULT_DRAFT_FOLDER);
backend.add_msg(&draft_folder, tpl.as_bytes(), "seen draft")?;
msg_utils::remove_local_draft()?;
printer.print(format!("Message successfully saved to {}", draft_folder))?;
printer
.print_struct(format!("Message successfully saved to {}", draft_folder))?;
break;
}
Ok(PostEditChoice::Discard) => {
+20 -12
View File
@@ -32,21 +32,29 @@ pub fn attachments<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
) -> Result<()> {
let attachments = backend.get_msg(mbox, seq)?.attachments();
let attachments_len = attachments.len();
debug!(
r#"{} attachment(s) found for message "{}""#,
attachments_len, seq
);
if attachments_len == 0 {
return printer.print_struct(format!("No attachment found for message {:?}", seq));
}
printer.print_str(format!(
"Found {:?} attachment{} for message {:?}",
attachments_len,
if attachments_len > 1 { "s" } else { "" },
seq
))?;
for attachment in attachments {
let file_path = config.get_download_file_path(&attachment.filename)?;
debug!("downloading {}", attachment.filename);
printer.print_str(format!("Downloading {:?}", file_path))?;
fs::write(&file_path, &attachment.content)
.context(format!("cannot download attachment {:?}", file_path))?;
}
printer.print(format!(
"{} attachment(s) successfully downloaded to {:?}",
attachments_len, config.downloads_dir
printer.print_struct(format!(
"Attachment{} successfully downloaded to {:?}",
if attachments_len > 1 { "s" } else { "" },
config.downloads_dir
))
}
@@ -59,7 +67,7 @@ pub fn copy<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
backend: Box<&mut B>,
) -> Result<()> {
backend.copy_msg(mbox_src, mbox_dst, seq)?;
printer.print(format!(
printer.print_struct(format!(
r#"Message {} successfully copied to folder "{}""#,
seq, mbox_dst
))
@@ -73,7 +81,7 @@ pub fn delete<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
backend: Box<&'a mut B>,
) -> Result<()> {
backend.del_msg(mbox, seq)?;
printer.print(format!(r#"Message(s) {} successfully deleted"#, seq))
printer.print_struct(format!(r#"Message(s) {} successfully deleted"#, seq))
}
/// Forward the given message UID from the selected mailbox.
@@ -189,7 +197,7 @@ pub fn move_<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
backend: Box<&'a mut B>,
) -> Result<()> {
backend.move_msg(mbox_src, mbox_dst, seq)?;
printer.print(format!(
printer.print_struct(format!(
r#"Message {} successfully moved to folder "{}""#,
seq, mbox_dst
))
@@ -212,7 +220,7 @@ pub fn read<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
msg.fold_text_parts(text_mime)
};
printer.print(msg)
printer.print_struct(msg)
}
/// Reply to the given message UID.
+5 -5
View File
@@ -21,7 +21,7 @@ pub fn new<'a, P: PrinterService>(
printer: &'a mut P,
) -> Result<()> {
let tpl = Msg::default().to_tpl(opts, account)?;
printer.print(tpl)
printer.print_struct(tpl)
}
/// Generate a reply message template.
@@ -38,7 +38,7 @@ pub fn reply<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
.get_msg(mbox, seq)?
.into_reply(all, config)?
.to_tpl(opts, config)?;
printer.print(tpl)
printer.print_struct(tpl)
}
/// Generate a forward message template.
@@ -54,7 +54,7 @@ pub fn forward<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
.get_msg(mbox, seq)?
.into_forward(config)?
.to_tpl(opts, config)?;
printer.print(tpl)
printer.print_struct(tpl)
}
/// Saves a message based on a template.
@@ -79,7 +79,7 @@ pub fn save<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
let msg = Msg::from_tpl(&tpl)?.add_attachments(attachments_paths)?;
let raw_msg = msg.into_sendable_msg(config)?.formatted();
backend.add_msg(mbox, &raw_msg, "seen")?;
printer.print("Template successfully saved")
printer.print_struct("Template successfully saved")
}
/// Sends a message based on a template.
@@ -105,5 +105,5 @@ pub fn send<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
let msg = Msg::from_tpl(&tpl)?.add_attachments(attachments_paths)?;
let sent_msg = smtp.send_msg(account, &msg)?;
backend.add_msg(mbox, &sent_msg.formatted(), "seen")?;
printer.print("Template successfully sent")
printer.print_struct("Template successfully sent")
}