-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ynqa/v0.1.2/main
v0.1.2
- Loading branch information
Showing
9 changed files
with
269 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "sigrs" | ||
version = "0.1.1" | ||
version = "0.1.2" | ||
authors = ["ynqa <[email protected]>"] | ||
edition = "2021" | ||
description = "Interactive grep (for streaming)" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::process::Stdio; | ||
|
||
use tokio::{ | ||
io::{AsyncBufReadExt, BufReader}, | ||
process::Command, | ||
sync::mpsc, | ||
time::{timeout, Duration}, | ||
}; | ||
use tokio_util::sync::CancellationToken; | ||
|
||
pub async fn execute( | ||
cmdstr: &str, | ||
tx: mpsc::Sender<String>, | ||
retrieval_timeout: Duration, | ||
canceled: CancellationToken, | ||
) -> anyhow::Result<()> { | ||
let args: Vec<&str> = cmdstr.split_whitespace().collect(); | ||
let mut child = Command::new(args[0]) | ||
.args(&args[1..]) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::piped()) | ||
.spawn()?; | ||
|
||
let stdout = child | ||
.stdout | ||
.take() | ||
.ok_or_else(|| anyhow::anyhow!("stdout is not available"))?; | ||
let stderr = child | ||
.stderr | ||
.take() | ||
.ok_or_else(|| anyhow::anyhow!("stderr is not available"))?; | ||
let mut stdout_reader = BufReader::new(stdout).lines(); | ||
let mut stderr_reader = BufReader::new(stderr).lines(); | ||
|
||
while !canceled.is_cancelled() { | ||
tokio::select! { | ||
stdout_res = timeout(retrieval_timeout, stdout_reader.next_line()) => { | ||
if let Ok(Ok(Some(line))) = stdout_res { | ||
let escaped = strip_ansi_escapes::strip_str(line.replace(['\n', '\t'], " ")); | ||
tx.send(escaped).await?; | ||
} | ||
}, | ||
stderr_res = timeout(retrieval_timeout, stderr_reader.next_line()) => { | ||
if let Ok(Ok(Some(line))) = stderr_res { | ||
let escaped = strip_ansi_escapes::strip_str(line.replace(['\n', '\t'], " ")); | ||
tx.send(escaped).await?; | ||
} | ||
} | ||
} | ||
} | ||
|
||
child.kill().await?; | ||
Ok(()) | ||
} |
Oops, something went wrong.