add functions so that it's actually usable, and add start and end time to CmdOutput
This commit is contained in:
parent
b1ad0e9f02
commit
20abd83ae8
1 changed files with 62 additions and 3 deletions
65
src/lib.rs
65
src/lib.rs
|
@ -5,11 +5,12 @@ use std::thread;
|
|||
use std::time::{Duration, Instant};
|
||||
|
||||
/// Holds the output for a command
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CmdOutput {
|
||||
lines: Vec<Line>,
|
||||
status: Option<i32>,
|
||||
duration: Duration,
|
||||
start_time: Instant,
|
||||
end_time: Instant,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Ord)]
|
||||
|
@ -19,6 +20,62 @@ pub struct Line {
|
|||
pub content: String,
|
||||
}
|
||||
|
||||
impl CmdOutput {
|
||||
/// Returns only stdout
|
||||
pub fn stdout(self) -> Vec<Line> {
|
||||
return self
|
||||
.lines
|
||||
.into_iter()
|
||||
.filter(|l| {
|
||||
if l.stdout {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
/// Returns only stdout
|
||||
pub fn stderr(self) -> Vec<Line> {
|
||||
return self
|
||||
.lines
|
||||
.into_iter()
|
||||
.filter(|l| {
|
||||
if !l.stdout {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
/// Returns all output
|
||||
pub fn lines(self) -> Vec<Line> {
|
||||
return self.lines;
|
||||
}
|
||||
|
||||
/// Returns the exit status code, if there was one
|
||||
pub fn status(self) -> Option<i32> {
|
||||
return self.status;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the duration the command ran for
|
||||
pub fn duration(self) -> Duration {
|
||||
return self.end_time.duration_since(self.start_time);
|
||||
}
|
||||
|
||||
/// Returns the time the command was started at
|
||||
pub fn start_time(self) -> Instant {
|
||||
return self.start_time;
|
||||
}
|
||||
|
||||
/// Returns the time the command finished at
|
||||
pub fn end_time(self) -> Instant {
|
||||
return self.end_time;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(command: &mut Command) -> CmdOutput {
|
||||
// https://stackoverflow.com/a/72831067/16432246
|
||||
let start = Instant::now();
|
||||
|
@ -62,6 +119,7 @@ pub fn run(command: &mut Command) -> CmdOutput {
|
|||
});
|
||||
|
||||
let status = child.wait().unwrap().code();
|
||||
let end = Instant::now();
|
||||
|
||||
let mut lines = stdout_rx.into_iter().collect::<Vec<Line>>();
|
||||
lines.append(&mut stderr_rx.into_iter().collect::<Vec<Line>>());
|
||||
|
@ -70,7 +128,8 @@ pub fn run(command: &mut Command) -> CmdOutput {
|
|||
return CmdOutput {
|
||||
lines: lines,
|
||||
status: status,
|
||||
duration: start.elapsed(),
|
||||
start_time: start,
|
||||
end_time: end,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue