refactor stdin for send and tpl cmds

This commit is contained in:
Clément DOUIN 2021-05-31 14:19:55 +02:00
parent f71720e6b9
commit 28448310c7
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
4 changed files with 18 additions and 3 deletions

1
Cargo.lock generated
View file

@ -305,6 +305,7 @@ dependencies = [
name = "himalaya"
version = "0.3.2"
dependencies = [
"atty",
"chrono",
"clap",
"env_logger",

View file

@ -6,6 +6,7 @@ authors = ["soywod <clement.douin@posteo.net>"]
edition = "2018"
[dependencies]
atty = "0.2.14"
chrono = "0.4.19"
clap = {version = "2.33.3", default-features = false, features = ["suggestions", "color"]}
env_logger = "0.8.3"

View file

@ -1,3 +1,4 @@
use atty::Stream;
use clap;
use error_chain::error_chain;
use log::{debug, error, trace};
@ -100,7 +101,7 @@ pub fn msg_subcmds<'a>() -> Vec<clap::App<'a, 'a>> {
.arg(attachment_arg()),
clap::SubCommand::with_name("send")
.about("Sends a raw message")
.arg(clap::Arg::with_name("message").raw(true)),
.arg(clap::Arg::with_name("message").raw(true).last(true)),
clap::SubCommand::with_name("save")
.about("Saves a raw message")
.arg(clap::Arg::with_name("message").raw(true)),
@ -558,7 +559,7 @@ fn msg_matches_send(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
let mut imap_conn = ImapConnector::new(&app.account)?;
let msg = if matches.is_present("message") {
let msg = if atty::is(Stream::Stdin) {
matches
.value_of("message")
.unwrap_or_default()

View file

@ -1,7 +1,9 @@
use atty::Stream;
use clap;
use error_chain::error_chain;
use log::{debug, trace};
use mailparse;
use std::io::{self, BufRead};
use crate::{app::App, imap::model::ImapConnector, msg::tpl::model::Tpl};
@ -156,7 +158,17 @@ fn tpl_matches_new(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
tpl.header(key, val);
}
if let Some(body) = matches.value_of("body") {
if atty::isnt(Stream::Stdin) {
let body = io::stdin()
.lock()
.lines()
.filter_map(|ln| ln.ok())
.map(|ln| ln.to_string())
.collect::<Vec<_>>()
.join("\n");
debug!("overriden body from stdin: {:?}", body);
tpl.body(body);
} else if let Some(body) = matches.value_of("body") {
debug!("overriden body: {:?}", body);
tpl.body(body);
};