mirror of
https://github.com/soywod/himalaya.git
synced 2024-07-08 10:35:13 +00:00
refactor stdin for send and tpl cmds
This commit is contained in:
parent
f71720e6b9
commit
28448310c7
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -305,6 +305,7 @@ dependencies = [
|
||||||
name = "himalaya"
|
name = "himalaya"
|
||||||
version = "0.3.2"
|
version = "0.3.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"atty",
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
|
|
@ -6,6 +6,7 @@ authors = ["soywod <clement.douin@posteo.net>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
atty = "0.2.14"
|
||||||
chrono = "0.4.19"
|
chrono = "0.4.19"
|
||||||
clap = {version = "2.33.3", default-features = false, features = ["suggestions", "color"]}
|
clap = {version = "2.33.3", default-features = false, features = ["suggestions", "color"]}
|
||||||
env_logger = "0.8.3"
|
env_logger = "0.8.3"
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use atty::Stream;
|
||||||
use clap;
|
use clap;
|
||||||
use error_chain::error_chain;
|
use error_chain::error_chain;
|
||||||
use log::{debug, error, trace};
|
use log::{debug, error, trace};
|
||||||
|
@ -100,7 +101,7 @@ pub fn msg_subcmds<'a>() -> Vec<clap::App<'a, 'a>> {
|
||||||
.arg(attachment_arg()),
|
.arg(attachment_arg()),
|
||||||
clap::SubCommand::with_name("send")
|
clap::SubCommand::with_name("send")
|
||||||
.about("Sends a raw message")
|
.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")
|
clap::SubCommand::with_name("save")
|
||||||
.about("Saves a raw message")
|
.about("Saves a raw message")
|
||||||
.arg(clap::Arg::with_name("message").raw(true)),
|
.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 mut imap_conn = ImapConnector::new(&app.account)?;
|
||||||
|
|
||||||
let msg = if matches.is_present("message") {
|
let msg = if atty::is(Stream::Stdin) {
|
||||||
matches
|
matches
|
||||||
.value_of("message")
|
.value_of("message")
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
use atty::Stream;
|
||||||
use clap;
|
use clap;
|
||||||
use error_chain::error_chain;
|
use error_chain::error_chain;
|
||||||
use log::{debug, trace};
|
use log::{debug, trace};
|
||||||
use mailparse;
|
use mailparse;
|
||||||
|
use std::io::{self, BufRead};
|
||||||
|
|
||||||
use crate::{app::App, imap::model::ImapConnector, msg::tpl::model::Tpl};
|
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);
|
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);
|
debug!("overriden body: {:?}", body);
|
||||||
tpl.body(body);
|
tpl.body(body);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue