add tpl_args for write subcommand (#361)

Co-authored-by: Clément DOUIN <soywod@users.noreply.github.com>
This commit is contained in:
ugla 2022-04-16 13:50:49 +02:00 committed by GitHub
parent 0ddcce22e6
commit 6d154abcb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 13 deletions

View file

@ -277,8 +277,9 @@ fn main() -> Result<()> {
Some(msg_args::Cmd::Send(raw_msg)) => {
return msg_handlers::send(raw_msg, &account_config, &mut printer, backend, &mut smtp);
}
Some(msg_args::Cmd::Write(atts, encrypt)) => {
Some(msg_args::Cmd::Write(tpl, atts, encrypt)) => {
return msg_handlers::write(
tpl,
atts,
encrypt,
&account_config,

View file

@ -42,7 +42,7 @@ pub enum Cmd<'a> {
Search(Query, MaxTableWidth, Option<PageSize>, Page),
Sort(Criteria, Query, MaxTableWidth, Option<PageSize>, Page),
Send(RawMsg<'a>),
Write(AttachmentPaths<'a>, Encrypt),
Write(tpl_args::TplOverride<'a>, AttachmentPaths<'a>, Encrypt),
Flag(Option<flag_args::Cmd<'a>>),
Tpl(Option<tpl_args::Cmd<'a>>),
@ -261,7 +261,8 @@ pub fn matches<'a>(m: &'a ArgMatches) -> Result<Option<Cmd<'a>>> {
debug!("attachments paths: {:?}", attachment_paths);
let encrypt = m.is_present("encrypt");
debug!("encrypt: {}", encrypt);
return Ok(Some(Cmd::Write(attachment_paths, encrypt)));
let tpl = tpl_args::TplOverride::from(m);
return Ok(Some(Cmd::Write(tpl, attachment_paths, encrypt)));
}
if let Some(m) = m.subcommand_matches("template") {
@ -412,6 +413,7 @@ pub fn subcmds<'a>() -> Vec<App<'a, 'a>> {
),
SubCommand::with_name("write")
.about("Writes a new message")
.args(&tpl_args::tpl_args())
.arg(attachments_arg())
.arg(encrypt_arg()),
SubCommand::with_name("send")

View file

@ -327,14 +327,15 @@ impl Msg {
Ok(self)
}
fn _edit_with_editor(&self, account: &AccountConfig) -> Result<Self> {
let tpl = self.to_tpl(TplOverride::default(), account)?;
fn _edit_with_editor(&self, tpl: TplOverride, account: &AccountConfig) -> Result<Self> {
let tpl = self.to_tpl(tpl, account)?;
let tpl = editor::open_with_tpl(tpl)?;
Self::from_tpl(&tpl)
}
pub fn edit_with_editor<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
mut self,
tpl: TplOverride,
account: &AccountConfig,
printer: &mut P,
backend: Box<&'a mut B>,
@ -353,7 +354,7 @@ impl Msg {
break;
}
PreEditChoice::Discard => {
self.merge_with(self._edit_with_editor(account)?);
self.merge_with(self._edit_with_editor(tpl.clone(), account)?);
break;
}
PreEditChoice::Quit => return Ok(backend),
@ -365,7 +366,7 @@ impl Msg {
}
}
} else {
self.merge_with(self._edit_with_editor(account)?);
self.merge_with(self._edit_with_editor(tpl.clone(), account)?);
}
loop {
@ -386,7 +387,7 @@ impl Msg {
break;
}
Ok(PostEditChoice::Edit) => {
self.merge_with(self._edit_with_editor(account)?);
self.merge_with(self._edit_with_editor(tpl.clone(), account)?);
continue;
}
Ok(PostEditChoice::LocalDraft) => {

View file

@ -21,6 +21,8 @@ use crate::{
smtp::SmtpService,
};
use super::tpl_args;
/// Downloads all message attachments to the user account downloads directory.
pub fn attachments<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
seq: &str,
@ -99,7 +101,13 @@ pub fn forward<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
.into_forward(config)?
.add_attachments(attachments_paths)?
.encrypt(encrypt)
.edit_with_editor(config, printer, backend, smtp)?;
.edit_with_editor(
tpl_args::TplOverride::default(),
config,
printer,
backend,
smtp,
)?;
Ok(())
}
@ -183,7 +191,13 @@ pub fn mailto<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
};
trace!("message: {:?}", msg);
msg.edit_with_editor(config, printer, backend, smtp)?;
msg.edit_with_editor(
tpl_args::TplOverride::default(),
config,
printer,
backend,
smtp,
)?;
Ok(())
}
@ -240,7 +254,13 @@ pub fn reply<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
.into_reply(all, config)?
.add_attachments(attachments_paths)?
.encrypt(encrypt)
.edit_with_editor(config, printer, backend, smtp)?
.edit_with_editor(
tpl_args::TplOverride::default(),
config,
printer,
backend,
smtp,
)?
.add_flags(mbox, seq, "replied")
}
@ -364,6 +384,7 @@ pub fn send<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
/// Compose a new message.
pub fn write<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
tpl: tpl_args::TplOverride,
attachments_paths: Vec<&str>,
encrypt: bool,
config: &AccountConfig,
@ -374,6 +395,6 @@ pub fn write<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
Msg::default()
.add_attachments(attachments_paths)?
.encrypt(encrypt)
.edit_with_editor(config, printer, backend, smtp)?;
.edit_with_editor(tpl, config, printer, backend, smtp)?;
Ok(())
}

View file

@ -13,7 +13,7 @@ type ReplyAll = bool;
type AttachmentPaths<'a> = Vec<&'a str>;
type Tpl<'a> = &'a str;
#[derive(Debug, Default, PartialEq, Eq)]
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct TplOverride<'a> {
pub subject: Option<&'a str>,
pub from: Option<Vec<&'a str>>,