watchexec/lib/examples/print_out.rs

50 lines
1.1 KiB
Rust
Raw Normal View History

use std::convert::Infallible;
2021-08-21 16:53:31 +02:00
use watchexec::{
action::{Action, Outcome},
2021-08-22 10:49:24 +02:00
config::{InitConfigBuilder, RuntimeConfig},
signal::Signal,
2021-08-21 16:53:31 +02:00
Watchexec,
};
// Run with: `env RUST_LOG=debug cargo run --example print_out`
#[tokio::main]
async fn main() -> color_eyre::eyre::Result<()> {
tracing_subscriber::fmt::init();
color_eyre::install()?;
let mut init = InitConfigBuilder::default();
init.on_error(|err| async move {
eprintln!("Watchexec Runtime Error: {}", err);
Ok::<(), std::convert::Infallible>(())
});
2021-08-21 16:53:31 +02:00
let mut runtime = RuntimeConfig::default();
2021-08-22 17:11:58 +02:00
runtime.pathset(["src"]);
runtime.command(["date"]);
runtime.on_action(|action: Action| async move {
eprintln!("Watchexec Action: {:?}", action);
2021-08-21 16:53:31 +02:00
if action
.events
.iter()
.flat_map(|event| event.signals())
.any(|sig| sig == Signal::Interrupt)
{
action.outcome(Outcome::Exit);
} else {
2021-08-22 17:11:58 +02:00
action.outcome(Outcome::if_running(
Outcome::both(Outcome::Stop, Outcome::Start),
Outcome::Start,
));
}
Ok::<(), Infallible>(())
});
2021-08-21 16:53:31 +02:00
let wx = Watchexec::new(init.build()?, runtime)?;
wx.main().await??;
2021-08-21 16:53:31 +02:00
Ok(())
}