diff options
author | Tin Lai <oscar@tinyiu.com> | 2023-09-21 13:31:31 +1000 |
---|---|---|
committer | Tin Lai <oscar@tinyiu.com> | 2023-09-21 13:31:51 +1000 |
commit | 5c3332884a917d4da05bfa437ee8b7c7ba5a56b9 (patch) | |
tree | 10bd4e8e0b4e54b7ad53b932784a9fe1cadc8490 | |
parent | bb1791d14e64ad83fb57116a24eb913a4946afed (diff) | |
download | rbw-5c3332884a917d4da05bfa437ee8b7c7ba5a56b9.tar.gz rbw-5c3332884a917d4da05bfa437ee8b7c7ba5a56b9.zip |
implements ability to edit from taking piped inputs
Signed-off-by: Tin Lai <oscar@tinyiu.com>
-rw-r--r-- | Cargo.lock | 27 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/edit.rs | 8 | ||||
-rw-r--r-- | src/error.rs | 5 |
4 files changed, 38 insertions, 3 deletions
@@ -133,6 +133,17 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -683,6 +694,15 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" @@ -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", @@ -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<String> { + 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(), |