From 4668d51476b31202fdf5ca749c94765e46dddc8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Tue, 4 May 2021 00:15:11 +0200 Subject: [PATCH] use stdin to send message (#78) --- CHANGELOG.md | 5 +++++ src/msg/cli.rs | 23 +++++++++++++++++++++-- vim/autoload/himalaya/msg.vim | 2 +- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c75df67..6f8a727 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] +### Added + +- Send message via stdin [#78] + ### Fixed - Table with subject containing `\r` or `\n`[#141] @@ -215,6 +219,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#71]: https://github.com/soywod/himalaya/issues/71 [#74]: https://github.com/soywod/himalaya/issues/74 [#75]: https://github.com/soywod/himalaya/issues/75 +[#78]: https://github.com/soywod/himalaya/issues/78 [#79]: https://github.com/soywod/himalaya/issues/79 [#83]: https://github.com/soywod/himalaya/issues/83 [#84]: https://github.com/soywod/himalaya/issues/84 diff --git a/src/msg/cli.rs b/src/msg/cli.rs index 500146d..20facae 100644 --- a/src/msg/cli.rs +++ b/src/msg/cli.rs @@ -1,7 +1,11 @@ use clap; use error_chain::error_chain; use log::{debug, error, trace}; -use std::{fs, ops::Deref}; +use std::{ + fs, + io::{self, BufRead}, + ops::Deref, +}; use crate::{ app::App, @@ -590,7 +594,22 @@ pub fn msg_matches(app: &App) -> Result { debug!("send command matched"); let mut imap_conn = ImapConnector::new(&app.account)?; - let msg = matches.value_of("message").unwrap(); + + let msg = if matches.is_present("message") { + matches + .value_of("message") + .unwrap_or_default() + .replace("\r", "") + .replace("\n", "\r\n") + } else { + io::stdin() + .lock() + .lines() + .filter_map(|ln| ln.ok()) + .map(|ln| ln.to_string()) + .collect::>() + .join("\r\n") + }; let msg = Msg::from(msg.to_string()); let msg = msg.to_sendable_msg()?; smtp::send(&app.account, &msg)?; diff --git a/vim/autoload/himalaya/msg.vim b/vim/autoload/himalaya/msg.vim index 2167d9e..ef22cf1 100644 --- a/vim/autoload/himalaya/msg.vim +++ b/vim/autoload/himalaya/msg.vim @@ -148,7 +148,7 @@ function! himalaya#msg#forward() endfunction function! himalaya#msg#draft_save() - let s:draft = join(getline(1, "$"), "\r\n") + let s:draft = join(getline(1, "$"), "\n") redraw | call s:log("Save draft [OK]") let &modified = 0 endfunction