Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plock-18]: add std err support #23

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src-tauri/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub(crate) async fn generate(
.arg("/C")
.arg(final_context)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
#[cfg(not(target_os = "windows"))]
Expand All @@ -64,6 +65,7 @@ pub(crate) async fn generate(
.arg("-c")
.arg(final_context)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
} else {
Expand All @@ -72,6 +74,7 @@ pub(crate) async fn generate(
.args(&custom_command[1..])
.arg(&final_context)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
};

Expand All @@ -83,16 +86,20 @@ pub(crate) async fn generate(
};

let stdout = BufReader::new(child.stdout.expect("Failed to take stdout of child"));
let stderr = BufReader::new(child.stderr.expect("Failed to take stderr of child"));

let stream = async_stream::stream! {
let mut reader = stdout;
let mut std_err_reader = stderr;
let mut buffer = Vec::new();
let mut err_buffer = Vec::new();

let mut should_break = false;
loop {
buffer.clear();
let mut temp_buf = [0; 1024]; // Temporary buffer for each read
match reader.read(&mut temp_buf).await {
Ok(0) => break, // EOF reached
Ok(0) => { should_break = true }, // EOF reached
Ok(size) => {
buffer.extend_from_slice(&temp_buf[..size]);
yield String::from_utf8_lossy(&buffer).to_string();
Expand All @@ -102,6 +109,19 @@ pub(crate) async fn generate(
break;
}
}

err_buffer.clear();
let mut err_buf = [0; 1024]; // Temporary buffer for each read
if let Ok(size) = std_err_reader.read(&mut err_buf).await {
err_buffer.extend_from_slice(&err_buf[..size]);
yield String::from_utf8_lossy(&err_buffer).to_string();
} else {
should_break = true;
}

if should_break {
break;
}
}
};

Expand Down