From 5c3332884a917d4da05bfa437ee8b7c7ba5a56b9 Mon Sep 17 00:00:00 2001 From: Tin Lai Date: Thu, 21 Sep 2023 13:31:31 +1000 Subject: implements ability to edit from taking piped inputs Signed-off-by: Tin Lai --- src/edit.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/edit.rs') diff --git a/src/edit.rs b/src/edit.rs index aa8c7b1..057996d 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -3,6 +3,14 @@ use crate::prelude::*; use std::io::{Read as _, Write as _}; pub fn edit(contents: &str, help: &str) -> Result { + if ! atty::is(atty::Stream::Stdin) { + // 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"; -- cgit v1.2.3-54-g00ecf