aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/no/notify-run
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name/no/notify-run')
-rw-r--r--pkgs/by-name/no/notify-run/Cargo.lock4
-rw-r--r--pkgs/by-name/no/notify-run/Cargo.toml2
-rw-r--r--pkgs/by-name/no/notify-run/src/main.rs82
3 files changed, 59 insertions, 29 deletions
diff --git a/pkgs/by-name/no/notify-run/Cargo.lock b/pkgs/by-name/no/notify-run/Cargo.lock
index ecc6e6b0..c1ff746e 100644
--- a/pkgs/by-name/no/notify-run/Cargo.lock
+++ b/pkgs/by-name/no/notify-run/Cargo.lock
@@ -13,9 +13,9 @@ version = 4
[[package]]
name = "anyhow"
-version = "1.0.103"
+version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2a4385e2e34eb35d6b3efe798b9eb88096925d87726c0798709bf56d9ed84af3"
+checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "notify-run"
diff --git a/pkgs/by-name/no/notify-run/Cargo.toml b/pkgs/by-name/no/notify-run/Cargo.toml
index 463438c6..ceff208e 100644
--- a/pkgs/by-name/no/notify-run/Cargo.toml
+++ b/pkgs/by-name/no/notify-run/Cargo.toml
@@ -17,4 +17,4 @@ edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-anyhow = "1.0.103"
+anyhow = "1.0.104"
diff --git a/pkgs/by-name/no/notify-run/src/main.rs b/pkgs/by-name/no/notify-run/src/main.rs
index a6a0165a..94f9ad4e 100644
--- a/pkgs/by-name/no/notify-run/src/main.rs
+++ b/pkgs/by-name/no/notify-run/src/main.rs
@@ -8,7 +8,13 @@
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
-use std::{env::args, path::PathBuf, process::Command};
+use std::{
+ env::args,
+ io::{BufRead, BufReader, Read, Write},
+ path::PathBuf,
+ process::{Command, Stdio},
+ thread::JoinHandle,
+};
use anyhow::{Context, Result};
@@ -20,18 +26,40 @@ fn main() -> Result<()> {
cmd.args(arguments.split(" ").collect::<Vec<_>>().as_slice());
}
- eprintln!("Spawning {:?}", cmd);
+ eprintln!("notify-run> Spawning {:?}", cmd);
- let output = cmd
- .output()
- .with_context(|| format!("Failed to spawn and await output of {:?}", cmd))?;
+ let mut child = cmd
+ .stderr(Stdio::piped())
+ .stdout(Stdio::piped())
+ .spawn()
+ .with_context(|| format!("Failed to spawn {:?}", cmd))?;
- if !output.status.success() {
+ let (stdout, stderr) = (
+ child.stdout.take().expect("Was piped"),
+ child.stderr.take().expect("Was piped"),
+ );
+
+ let name = PathBuf::from(&args[0])
+ .file_name()
+ .expect("this to be a command, and thus have a file_name")
+ .to_string_lossy()
+ .to_string();
+
+ let stdout_t = write_thread(stdout, name.to_owned());
+ let stderr_t = write_thread(stderr, name.to_owned());
+ stdout_t
+ .join()
+ .unwrap()
+ .context("Failed to join stdout thread")?;
+ stderr_t
+ .join()
+ .unwrap()
+ .context("Failed to join stderr thread")?;
+
+ let status = child.wait().context("Failed to wait for child output")?;
+ if !status.success() {
let mut notify_send = Command::new("notify-send");
- notify_send.args([
- format!("Command {:?} failed", cmd).as_str(),
- &String::from_utf8_lossy(output.stderr.as_slice()),
- ]);
+ notify_send.args([format!("Command {:?} failed", cmd).as_str()]);
notify_send.status().with_context(|| {
format!(
@@ -39,27 +67,29 @@ fn main() -> Result<()> {
cmd
)
})?;
- } else {
- let name = PathBuf::from(&args[0])
- .file_name()
- .expect("this to be a command, and thus have a file_name")
- .to_string_lossy()
- .to_string();
-
- print!("{}", append_name(&name, &output.stdout));
- eprint!("{}", append_name(&name, &output.stderr));
}
Ok(())
}
-fn append_name(name: &str, base: &[u8]) -> String {
- let base = String::from_utf8_lossy(base).to_string();
+fn write_thread<R: Read + Send + 'static>(input: R, name: String) -> JoinHandle<Result<()>> {
+ std::thread::spawn(move || {
+ let mut reader = BufReader::new(input);
- let mut output = String::new();
- for line in base.lines() {
- output.push_str(format!("{name}> {line}\n").as_str());
- }
+ let mut buf = String::new();
+ loop {
+ buf.clear();
+ if reader
+ .read_line(&mut buf)
+ .context("Failed to read from child output")?
+ == 0
+ {
+ break;
+ }
+
+ write!(std::io::stdout(), "{name}> {buf}")?;
+ }
- output
+ Ok(())
+ })
}