aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTin Lai <oscar@tinyiu.com>2023-09-21 13:31:31 +1000
committerTin Lai <oscar@tinyiu.com>2023-09-21 13:31:51 +1000
commit5c3332884a917d4da05bfa437ee8b7c7ba5a56b9 (patch)
tree10bd4e8e0b4e54b7ad53b932784a9fe1cadc8490 /src
parentbb1791d14e64ad83fb57116a24eb913a4946afed (diff)
downloadrbw-5c3332884a917d4da05bfa437ee8b7c7ba5a56b9.tar.gz
rbw-5c3332884a917d4da05bfa437ee8b7c7ba5a56b9.zip
implements ability to edit from taking piped inputs
Signed-off-by: Tin Lai <oscar@tinyiu.com>
Diffstat (limited to 'src')
-rw-r--r--src/edit.rs8
-rw-r--r--src/error.rs5
2 files changed, 13 insertions, 0 deletions
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(),