Compare commits

...

2 commits

Author SHA1 Message Date
askiiart
dedacf0ea4
fix readme 2025-01-11 19:16:35 -06:00
askiiart
5deaaa4265
misc improvements
notably, run() is better and no longer uses channels
2025-01-11 18:06:29 -06:00
3 changed files with 31 additions and 51 deletions

View file

@ -5,4 +5,4 @@ A better way of running commands - get stdout and stderr together, in order with
## TODO ## TODO
- Improve docs - Improve docs
- Add `lines` capability to `run_with_funcs()` - Add `lines` capability to `run_funcs()`

View file

@ -167,41 +167,38 @@ pub fn run(command: &mut Command) -> CmdOutput {
let child_stdout = child.stdout.take().unwrap(); let child_stdout = child.stdout.take().unwrap();
let child_stderr = child.stderr.take().unwrap(); let child_stderr = child.stderr.take().unwrap();
let (stdout_tx, stdout_rx) = std::sync::mpsc::channel();
let (stderr_tx, stderr_rx) = std::sync::mpsc::channel();
let stdout_lines = BufReader::new(child_stdout).lines(); let stdout_lines = BufReader::new(child_stdout).lines();
thread::spawn(move || { let stdout_thread = thread::spawn(move || {
let mut lines: Vec<Line> = Vec::new();
for line in stdout_lines { for line in stdout_lines {
stdout_tx lines.push(Line {
.send(Line {
content: line.unwrap(), content: line.unwrap(),
printed_to: LineType::Stdout, printed_to: LineType::Stdout,
time: Instant::now(), time: Instant::now(),
}) });
.unwrap();
} }
return lines;
}); });
let stderr_lines = BufReader::new(child_stderr).lines(); let stderr_lines = BufReader::new(child_stderr).lines();
thread::spawn(move || { let stderr_thread = thread::spawn(move || {
let mut lines: Vec<Line> = Vec::new();
for line in stderr_lines { for line in stderr_lines {
let time = Instant::now(); let time = Instant::now();
stderr_tx lines.push(Line {
.send(Line {
content: line.unwrap(), content: line.unwrap(),
printed_to: LineType::Stderr, printed_to: LineType::Stderr,
time: time, time: time,
}) });
.unwrap();
} }
return lines;
}); });
let status = child.wait().unwrap().code(); let status = child.wait().unwrap().code();
let end = Instant::now(); let end = Instant::now();
let mut lines = stdout_rx.into_iter().collect::<Vec<Line>>(); let mut lines = stdout_thread.join().unwrap();
lines.append(&mut stderr_rx.into_iter().collect::<Vec<Line>>()); lines.append(&mut stderr_thread.join().unwrap());
lines.sort(); lines.sort();
return CmdOutput { return CmdOutput {

View file

@ -6,38 +6,23 @@ use std::{
hash::{BuildHasher, Hasher, RandomState}, hash::{BuildHasher, Hasher, RandomState},
}; };
use std::{fs::File, os::unix::fs::FileExt, thread::sleep}; use std::{fs::File, os::unix::fs::FileExt, thread::sleep};
use crate::command;
#[macro_export]
macro_rules! command {
($command:expr, $($args:expr),*) => {
{
Command::new($command)
$(
.arg($args)
)*
}
};
}
/// Tests what stdout prints /// Tests what stdout prints
#[test] #[test]
fn stdout_content() { fn stdout_content() {
let expected = "[\"helloooooooooo\", \"hiiiiiiiiiiiii\"]"; let expected = vec!["helloooooooooo".to_string(), "hiiiiiiiiiiiii".to_string()];
assert_eq!(
expected, let output = run(Command::new("echo").arg(
format!( "helloooooooooo
"{:?}", hiiiiiiiiiiiii",
run(&mut Command::new("echo") ))
.arg("-n") .stdout()
.arg("helloooooooooo\nhiiiiiiiiiiiii")) .unwrap()
.stdout() .into_iter()
.unwrap() .map(|line| line.content)
.into_iter() .collect::<Vec<String>>();
.map(|line| { line.content })
.collect::<Vec<String>>() assert_eq!(expected, output);
)
);
} }
/// Tests what stderr prints /// Tests what stderr prints
@ -111,7 +96,7 @@ fn test_run_funcs() {
|stdout_lines| { |stdout_lines| {
sleep(Duration::from_secs(1)); sleep(Duration::from_secs(1));
for _ in stdout_lines { for _ in stdout_lines {
command!("bash", "-c", "echo stdout >> ./tmp-run_funcs") // col Command::new("bash").arg("-c").arg("echo stdout >> ./tmp-run_funcs") // col
.output() .output()
.unwrap(); .unwrap();
} }
@ -121,7 +106,7 @@ fn test_run_funcs() {
|stderr_lines| { |stderr_lines| {
sleep(Duration::from_secs(3)); sleep(Duration::from_secs(3));
for _ in stderr_lines { for _ in stderr_lines {
command!("bash", "-c", "echo stderr >> ./tmp-run_funcs") // col Command::new("bash").arg("-c").arg("echo stderr >> ./tmp-run_funcs") // col
.output() .output()
.unwrap(); .unwrap();
} }
@ -152,9 +137,7 @@ fn test_run_funcs() {
fn test_run_funcs_with_lines() { fn test_run_funcs_with_lines() {
let threads = thread::spawn(|| { let threads = thread::spawn(|| {
return run_funcs_with_lines( return run_funcs_with_lines(
Command::new("bash") &mut Command::new("bash").arg("-c").arg("echo hi; >&2 echo hello"),
.arg("-c")
.arg("echo hi; >&2 echo hello"),
{ {
|stdout_lines| { |stdout_lines| {
let mut lines: Vec<Line> = Vec::new(); let mut lines: Vec<Line> = Vec::new();
@ -163,7 +146,7 @@ fn test_run_funcs_with_lines() {
let line = line.unwrap(); let line = line.unwrap();
lines.push(Line::from_stdout(&line)); lines.push(Line::from_stdout(&line));
assert_eq!(&line, "hi"); assert_eq!(&line, "hi");
command!("bash", "-c", "echo stdout >> ./tmp-run_funcs_with_lines") Command::new("bash").arg("-c").arg("echo stdout >> ./tmp-run_funcs_with_lines")
.output() .output()
.unwrap(); .unwrap();
} }
@ -178,7 +161,7 @@ fn test_run_funcs_with_lines() {
let line = line.unwrap(); let line = line.unwrap();
lines.push(Line::from_stdout(&line)); lines.push(Line::from_stdout(&line));
assert_eq!(line, "hello"); assert_eq!(line, "hello");
command!("bash", "-c", "echo stderr >> ./tmp-run_funcs_with_lines") // col Command::new("bash").arg("-c").arg("echo stderr >> ./tmp-run_funcs_with_lines")
.output() .output()
.unwrap(); // oops sorry lol .unwrap(); // oops sorry lol
} }