aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2024-01-01 15:50:27 -0500
committerGitHub <noreply@github.com>2024-01-01 15:50:27 -0500
commit27be76f23ed36d595c1f3fa37b0d5de0cae56def (patch)
tree8731ce04614e8b6dc04c1a885e4c4f5b99240611
parent224a660b1d66fda5ee2366086f40339672f321ab (diff)
parent5c3332884a917d4da05bfa437ee8b7c7ba5a56b9 (diff)
downloadrbw-27be76f23ed36d595c1f3fa37b0d5de0cae56def.tar.gz
rbw-27be76f23ed36d595c1f3fa37b0d5de0cae56def.zip
Merge pull request #138 from soraxas/feat-edit-from-stdin
Implements ability to edit from taking piped inputs
-rw-r--r--Cargo.lock27
-rw-r--r--Cargo.toml1
-rw-r--r--src/edit.rs8
-rw-r--r--src/error.rs5
4 files changed, 38 insertions, 3 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 850a8de..18f96b0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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",
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 178e188..cb59366 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(),