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)) => { Some(msg_args::Cmd::Send(raw_msg)) => {
return msg_handlers::send(raw_msg, &account_config, &mut printer, backend, &mut smtp); 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( return msg_handlers::write(
tpl,
atts, atts,
encrypt, encrypt,
&account_config, &account_config,

View file

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

View file

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

View file

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

View file

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