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 ++++++++ src/error.rs | 5 +++++ 2 files changed, 13 insertions(+) (limited to 'src') 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"; diff --git a/src/error.rs b/src/error.rs index 31edaf3..1ffbcdd 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,6 +21,11 @@ pub enum Error { #[error("failed to parse pinentry output ({out:?})")] FailedToParsePinentry { out: String }, + #[error("failed to read from stdin: {err}")] + FailedToReadFromStdin { + err: std::io::Error, + }, + #[error( "failed to run editor {}: {err}", .editor.to_string_lossy(), -- cgit v1.2.3-54-g00ecf