aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/by-name
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name')
-rw-r--r--pkgs/by-name/no/notify-run/src/main.rs82
1 files changed, 56 insertions, 26 deletions
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(())
+ })
}