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 --- Cargo.lock | 27 ++++++++++++++++++++++++--- Cargo.toml | 1 + src/edit.rs | 8 ++++++++ src/error.rs | 5 +++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7d4d24b..fcfae70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,6 +132,17 @@ dependencies = [ "syn", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -681,6 +692,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.2" @@ -828,7 +848,7 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.2", "libc", "windows-sys 0.48.0", ] @@ -845,7 +865,7 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.2", "io-lifetimes", "rustix", "windows-sys 0.48.0", @@ -1075,7 +1095,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.2", "libc", ] @@ -1296,6 +1316,7 @@ dependencies = [ "argon2", "arrayvec", "async-trait", + "atty", "base32", "base64", "block-padding", diff --git a/Cargo.toml b/Cargo.toml index 2c5698b..a57be0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ anyhow = "1.0.72" argon2 = "0.5.1" arrayvec = "0.7.4" async-trait = "0.1.71" +atty="0.2.*" base32 = "0.4.0" base64 = "0.21.2" block-padding = "0.3.3" 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