diff options
Diffstat (limited to 'src/edit.rs')
-rw-r--r-- | src/edit.rs | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/edit.rs b/src/edit.rs index 1a831a7..360f31f 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -2,7 +2,17 @@ use crate::prelude::*; use std::io::{Read as _, Write as _}; +use is_terminal::IsTerminal as _; + pub fn edit(contents: &str, help: &str) -> Result<String> { + if !std::io::stdin().is_terminal() { + // directly read from piped content + return match std::io::read_to_string(std::io::stdin()) { + Err(e) => Err(Error::FailedToReadFromStdin { err: e }), + Ok(res) => Ok(res), + }; + } + let mut var = "VISUAL"; let editor = std::env::var_os(var).unwrap_or_else(|| { var = "EDITOR"; @@ -30,6 +40,7 @@ pub fn edit(contents: &str, help: &str) -> Result<String> { let editor = std::path::Path::new(&editor); let mut editor_args = vec![]; + #[allow(clippy::single_match_else)] // more to come match editor.file_name() { Some(editor) => match editor.to_str() { Some("vim" | "nvim") => { @@ -52,7 +63,7 @@ pub fn edit(contents: &str, help: &str) -> Result<String> { (editor, editor_args) }; - let res = std::process::Command::new(&cmd).args(&args).status(); + let res = std::process::Command::new(cmd).args(&args).status(); match res { Ok(res) => { if !res.success() { @@ -80,8 +91,6 @@ pub fn edit(contents: &str, help: &str) -> Result<String> { } fn contains_shell_metacharacters(cmd: &std::ffi::OsStr) -> bool { - match cmd.to_str() { - Some(s) => s.contains(&[' ', '$', '\'', '"'][..]), - None => false, - } + cmd.to_str() + .map_or(false, |s| s.contains(&[' ', '$', '\'', '"'][..])) } |