From 28448310c7c7930bba51af2c3822a569afca6dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Mon, 31 May 2021 14:19:55 +0200 Subject: [PATCH] refactor stdin for send and tpl cmds --- Cargo.lock | 1 + Cargo.toml | 1 + src/msg/cli.rs | 5 +++-- src/msg/tpl/cli.rs | 14 +++++++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9067b5b..3d4b107 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -305,6 +305,7 @@ dependencies = [ name = "himalaya" version = "0.3.2" dependencies = [ + "atty", "chrono", "clap", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index d12c73c..87827f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ authors = ["soywod "] 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" diff --git a/src/msg/cli.rs b/src/msg/cli.rs index 6e350d5..67d7d73 100644 --- a/src/msg/cli.rs +++ b/src/msg/cli.rs @@ -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> { .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 { 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() diff --git a/src/msg/tpl/cli.rs b/src/msg/tpl/cli.rs index 4b21968..15b2512 100644 --- a/src/msg/tpl/cli.rs +++ b/src/msg/tpl/cli.rs @@ -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 { 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::>() + .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); };