diff --git a/lib/examples/fs.rs b/lib/examples/fs.rs index 6649b30..ee03d16 100644 --- a/lib/examples/fs.rs +++ b/lib/examples/fs.rs @@ -1,14 +1,10 @@ -use std::{process::exit, time::Duration}; +use std::time::Duration; use tokio::{ sync::{mpsc, watch}, time::sleep, }; -use watchexec::{ - event::{Event, Particle}, - fs, - signal::{self, Signal}, -}; +use watchexec::{event::Event, fs}; // Run with: `env RUST_LOG=debug cargo run --example fs`, // then touch some files within the first 15 seconds, and afterwards. @@ -28,12 +24,6 @@ async fn main() -> color_eyre::eyre::Result<()> { tokio::spawn(async move { while let Some(e) = ev_r.recv().await { tracing::info!("event: {:?}", e); - - if e.particulars.contains(&Particle::Signal(Signal::Interrupt)) - || e.particulars.contains(&Particle::Signal(Signal::Terminate)) - { - exit(0); - } } }); @@ -43,8 +33,6 @@ async fn main() -> color_eyre::eyre::Result<()> { } }); - tokio::spawn(signal::worker(er_s.clone(), ev_s.clone())); - let wd_sh = tokio::spawn(async move { sleep(Duration::from_secs(15)).await; wkd.pathset = Vec::new(); diff --git a/lib/examples/signal.rs b/lib/examples/signal.rs new file mode 100644 index 0000000..8f72f83 --- /dev/null +++ b/lib/examples/signal.rs @@ -0,0 +1,40 @@ +use std::process::exit; + +use tokio::sync::mpsc; +use watchexec::{ + event::{Event, Particle}, + signal::{self, Signal}, +}; + +// Run with: `env RUST_LOG=debug cargo run --example signal`, +// then issue some signals to the printed PID, or hit e.g. Ctrl-C. +// Send a SIGTERM (unix) or Ctrl-Break (windows) to exit. +#[tokio::main] +async fn main() -> color_eyre::eyre::Result<()> { + tracing_subscriber::fmt::init(); + color_eyre::install()?; + + let (ev_s, mut ev_r) = mpsc::channel::(1024); + let (er_s, mut er_r) = mpsc::channel(64); + + tokio::spawn(async move { + while let Some(e) = ev_r.recv().await { + tracing::info!("event: {:?}", e); + + if e.particulars.contains(&Particle::Signal(Signal::Terminate)) { + exit(0); + } + } + }); + + tokio::spawn(async move { + while let Some(e) = er_r.recv().await { + tracing::error!("{}", e); + } + }); + + tracing::info!("PID is {}", std::process::id()); + signal::worker(er_s.clone(), ev_s.clone()).await?; + + Ok(()) +} diff --git a/lib/src/lib.rs b/lib/src/lib.rs index b9f17af..6a6fc9e 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,11 +1,19 @@ //! Watchexec: a library for utilities and programs which respond to events; //! file changes, human interaction, and more. //! -//! Also see the CLI tool: https://watchexec.github.io/ +//! Also see the CLI tool: //! -//! This library is powered by [Tokio](https://tokio.rs), minimum version 1.10. +//! This library is powered by [Tokio](https://tokio.rs), minimum version 1.10. This requirement may +//! change (upwards) in the future without breaking change. //! -//! The main way to use this crate involves constructing a [`Handler`] and running it. +//! The main way to use this crate involves constructing a [`Watchexec`] around a [`Config`] and +//! running it. The config may contain some instances of [`Handler`]s, which hook into watchexec +//! processing at various points. +//! +//! Alternatively, one can use the modules exposed by the crate and the external crates such as +//! [ClearScreen][clearscreen] and [Command Group][command_group] to build something more advanced, +//! at the cost of reimplementing the glue code. See the examples folder for some basic/demo tools +//! written with the individual modules. //! //! This crate does not itself use `unsafe`. However, it depends on a number of libraries which do, //! most because they interact with the operating system.