mirror of
https://github.com/watchexec/watchexec.git
synced 2024-09-28 22:21:33 +02:00
75 lines
1.5 KiB
Rust
75 lines
1.5 KiB
Rust
|
use std::process::{Child, Command};
|
||
|
|
||
|
pub struct Runner {
|
||
|
process: Option<Child>,
|
||
|
restart: bool,
|
||
|
cls: bool,
|
||
|
verbose: bool
|
||
|
}
|
||
|
|
||
|
impl Runner {
|
||
|
pub fn new(restart: bool, clear: bool, verbose: bool) -> Runner {
|
||
|
Runner {
|
||
|
process: None,
|
||
|
restart: restart,
|
||
|
cls: clear,
|
||
|
verbose: verbose
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn clear(&self) {
|
||
|
// TODO: determine better way to do this
|
||
|
let clear_cmd;
|
||
|
if cfg!(target_os = "windows") {
|
||
|
clear_cmd = "cls";
|
||
|
}
|
||
|
else {
|
||
|
clear_cmd = "clear";
|
||
|
}
|
||
|
|
||
|
let _ = Command::new(clear_cmd).status();
|
||
|
}
|
||
|
|
||
|
|
||
|
fn invoke(&self, cmd: &str) -> Option<Child> {
|
||
|
let shell;
|
||
|
let shell_cmd_arg;
|
||
|
|
||
|
if cfg!(target_os = "windows") {
|
||
|
shell = "cmd.exe";
|
||
|
shell_cmd_arg = "/C";
|
||
|
}
|
||
|
else {
|
||
|
shell = "sh";
|
||
|
shell_cmd_arg = "-c";
|
||
|
}
|
||
|
|
||
|
Command::new(shell)
|
||
|
.arg(shell_cmd_arg)
|
||
|
.arg(cmd)
|
||
|
.spawn()
|
||
|
.ok()
|
||
|
}
|
||
|
|
||
|
|
||
|
pub fn run_command(&mut self, cmd: &str) {
|
||
|
if let Some(ref mut child) = self.process {
|
||
|
if self.restart {
|
||
|
let _ = child.kill();
|
||
|
}
|
||
|
|
||
|
let _ = child.wait();
|
||
|
}
|
||
|
|
||
|
if self.cls {
|
||
|
self.clear();
|
||
|
}
|
||
|
|
||
|
if self.verbose {
|
||
|
println!("*** Executing: {}", cmd);
|
||
|
}
|
||
|
|
||
|
self.process = self.invoke(cmd);
|
||
|
}
|
||
|
}
|