diff --git a/CHANGELOG.md b/CHANGELOG.md index d79f444..13a03ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- New/reply/forward from Vim plugin since Tpl refactor [#176] + ## [0.4.0] - 2021-06-03 ### Added @@ -296,3 +300,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#144]: https://github.com/soywod/himalaya/issues/144 [#146]: https://github.com/soywod/himalaya/issues/146 [#160]: https://github.com/soywod/himalaya/issues/160 +[#176]: https://github.com/soywod/himalaya/issues/176 diff --git a/src/msg/cli.rs b/src/msg/cli.rs index 68f0b14..e66f2c1 100644 --- a/src/msg/cli.rs +++ b/src/msg/cli.rs @@ -559,7 +559,7 @@ fn msg_matches_send(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { let mut imap_conn = ImapConnector::new(&ctx.account)?; - let msg = if atty::is(Stream::Stdin) { + let msg = if atty::is(Stream::Stdin) || ctx.output.is_json() { matches .value_of("message") .unwrap_or_default() diff --git a/src/msg/tpl/cli.rs b/src/msg/tpl/cli.rs index 41ad1f7..780613d 100644 --- a/src/msg/tpl/cli.rs +++ b/src/msg/tpl/cli.rs @@ -118,7 +118,7 @@ pub fn tpl_matches(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { } } -fn override_tpl_with_args(tpl: &mut Tpl, matches: &clap::ArgMatches) { +fn override_tpl_with_args(ctx: &Ctx, tpl: &mut Tpl, matches: &clap::ArgMatches) { if let Some(from) = matches.value_of("from") { debug!("overriden from: {:?}", from); tpl.header("From", from); @@ -155,7 +155,7 @@ fn override_tpl_with_args(tpl: &mut Tpl, matches: &clap::ArgMatches) { tpl.header(key, val); } - if atty::isnt(Stream::Stdin) { + if atty::isnt(Stream::Stdin) && ctx.output.is_plain() { let body = io::stdin() .lock() .lines() @@ -180,7 +180,7 @@ fn tpl_matches_new(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { debug!("new command matched"); let mut tpl = Tpl::new(&ctx); - override_tpl_with_args(&mut tpl, &matches); + override_tpl_with_args(&ctx, &mut tpl, &matches); trace!("tpl: {:?}", tpl); ctx.output.print(tpl); @@ -197,11 +197,11 @@ fn tpl_matches_reply(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { let msg = &imap_conn.read_msg(&ctx.mbox, &uid)?; let msg = mailparse::parse_mail(&msg)?; let mut tpl = if matches.is_present("reply-all") { - Tpl::reply(&ctx, &msg) - } else { Tpl::reply_all(&ctx, &msg) + } else { + Tpl::reply(&ctx, &msg) }; - override_tpl_with_args(&mut tpl, &matches); + override_tpl_with_args(&ctx, &mut tpl, &matches); trace!("tpl: {:?}", tpl); ctx.output.print(tpl); @@ -218,7 +218,7 @@ fn tpl_matches_forward(ctx: &Ctx, matches: &clap::ArgMatches) -> Result { let msg = &imap_conn.read_msg(&ctx.mbox, &uid)?; let msg = mailparse::parse_mail(&msg)?; let mut tpl = Tpl::forward(&ctx, &msg); - override_tpl_with_args(&mut tpl, &matches); + override_tpl_with_args(&ctx, &mut tpl, &matches); trace!("tpl: {:?}", tpl); ctx.output.print(tpl); diff --git a/src/msg/tpl/model.rs b/src/msg/tpl/model.rs index 4451c41..29b7cbf 100644 --- a/src/msg/tpl/model.rs +++ b/src/msg/tpl/model.rs @@ -14,6 +14,7 @@ pub struct Tpl { headers: HashMap, body: Option, signature: Option, + raw: String, } impl Tpl { @@ -23,11 +24,14 @@ impl Tpl { headers.insert("To".to_string(), String::new()); headers.insert("Subject".to_string(), String::new()); - Self { + let mut tpl = Self { headers, body: None, signature: ctx.config.signature(ctx.account), - } + raw: String::new(), + }; + tpl.raw = tpl.to_string(); + tpl } pub fn reply(ctx: &Ctx, msg: &mailparse::ParsedMail) -> Self { @@ -65,11 +69,14 @@ impl Tpl { .collect::>() .join("\n"); - Self { + let mut tpl = Self { headers, body: Some(body), signature: ctx.config.signature(&ctx.account), - } + raw: String::new(), + }; + tpl.raw = tpl.to_string(); + tpl } pub fn reply_all(ctx: &Ctx, msg: &mailparse::ParsedMail) -> Self { @@ -140,11 +147,14 @@ impl Tpl { .collect::>() .join("\n"); - Self { + let mut tpl = Self { headers, body: Some(body), signature: ctx.config.signature(&ctx.account), - } + raw: String::new(), + }; + tpl.raw = tpl.to_string(); + tpl } pub fn forward(ctx: &Ctx, msg: &mailparse::ParsedMail) -> Self { @@ -167,11 +177,14 @@ impl Tpl { let mut body = String::from("-------- Forwarded Message --------\n"); body.push_str(&parts.join("\r\n\r\n").replace("\r", "")); - Self { + let mut tpl = Self { headers, body: Some(body), signature: ctx.config.signature(&ctx.account), - } + raw: String::new(), + }; + tpl.raw = tpl.to_string(); + tpl } pub fn header(&mut self, key: K, val: V) -> &Self { diff --git a/src/output/model.rs b/src/output/model.rs index da6a08a..13b9f6c 100644 --- a/src/output/model.rs +++ b/src/output/model.rs @@ -3,7 +3,7 @@ use std::fmt; // Output format -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub enum OutputFmt { Plain, Json, @@ -64,6 +64,14 @@ impl Output { } } } + + pub fn is_plain(&self) -> bool { + self.fmt == OutputFmt::Plain + } + + pub fn is_json(&self) -> bool { + self.fmt == OutputFmt::Json + } } impl Default for Output { diff --git a/vim/autoload/himalaya/msg.vim b/vim/autoload/himalaya/msg.vim index 2f55a11..f780496 100644 --- a/vim/autoload/himalaya/msg.vim +++ b/vim/autoload/himalaya/msg.vim @@ -86,7 +86,7 @@ function! himalaya#msg#write() let account = himalaya#account#curr() let msg = s:cli("--account %s template new", [shellescape(account)], "Fetching new template", 0) silent! edit Himalaya write - call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) + call append(0, split(substitute(msg.raw, "\r", "", "g"), "\n")) silent execute "$d" setlocal filetype=himalaya-msg-write let &modified = 0 @@ -112,7 +112,7 @@ function! himalaya#msg#reply() \0, \) execute printf("silent! edit Himalaya reply [%d]", msg_id) - call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) + call append(0, split(substitute(msg.raw, "\r", "", "g"), "\n")) silent execute "$d" setlocal filetype=himalaya-msg-write let &modified = 0 @@ -138,7 +138,7 @@ function! himalaya#msg#reply_all() \0 \) execute printf("silent! edit Himalaya reply all [%d]", msg_id) - call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) + call append(0, split(substitute(msg.raw, "\r", "", "g"), "\n")) silent execute "$d" setlocal filetype=himalaya-msg-write let &modified = 0 @@ -164,7 +164,7 @@ function! himalaya#msg#forward() \0 \) execute printf("silent! edit Himalaya forward [%d]", msg_id) - call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) + call append(0, split(substitute(msg.raw, "\r", "", "g"), "\n")) silent execute "$d" setlocal filetype=himalaya-msg-write let &modified = 0 diff --git a/wiki b/wiki index 5f3d7ed..2c064c8 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit 5f3d7ed3519ddaeced9521a698d31319c6cd7836 +Subproject commit 2c064c801e174d9cc84c1753f2c5f3e0240fa2ed