Add filtering to demo CLI

This commit is contained in:
Félix Saparelli 2021-09-28 22:24:06 +13:00
parent 7cdb6ac5ad
commit 30dae61a02
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
2 changed files with 17 additions and 7 deletions

View File

@ -1,5 +1,6 @@
use std::{ use std::{
convert::Infallible, env::current_dir, io::stderr, path::Path, str::FromStr, time::Duration, convert::Infallible, env::current_dir, io::stderr, path::Path, str::FromStr, sync::Arc,
time::Duration,
}; };
use clap::ArgMatches; use clap::ArgMatches;
@ -8,13 +9,15 @@ use watchexec::{
action::{Action, Outcome, Signal}, action::{Action, Outcome, Signal},
command::Shell, command::Shell,
config::{InitConfig, RuntimeConfig}, config::{InitConfig, RuntimeConfig},
filter::tagged::TaggedFilterer,
fs::Watcher, fs::Watcher,
handler::PrintDisplay, handler::PrintDisplay,
signal::Signal as InputSignal, signal::Signal as InputSignal,
}; };
pub fn new(args: &ArgMatches<'static>) -> Result<(InitConfig, RuntimeConfig)> { pub fn new(args: &ArgMatches<'static>) -> Result<(InitConfig, RuntimeConfig, Arc<TaggedFilterer>)> {
Ok((init(args)?, runtime(args)?)) let r = runtime(args)?;
Ok((init(args)?, r.0, r.1))
} }
fn init(_args: &ArgMatches<'static>) -> Result<InitConfig> { fn init(_args: &ArgMatches<'static>) -> Result<InitConfig> {
@ -25,7 +28,7 @@ fn init(_args: &ArgMatches<'static>) -> Result<InitConfig> {
Ok(config.build()?) Ok(config.build()?)
} }
fn runtime(args: &ArgMatches<'static>) -> Result<RuntimeConfig> { fn runtime(args: &ArgMatches<'static>) -> Result<(RuntimeConfig, Arc<TaggedFilterer>)> {
let mut config = RuntimeConfig::default(); let mut config = RuntimeConfig::default();
config.command( config.command(
@ -90,6 +93,9 @@ fn runtime(args: &ArgMatches<'static>) -> Result<RuntimeConfig> {
let print_events = args.is_present("print-events"); let print_events = args.is_present("print-events");
let once = args.is_present("once"); let once = args.is_present("once");
let filterer = TaggedFilterer::new(".", ".");
config.filterer(filterer.clone());
config.on_action(move |action: Action| { config.on_action(move |action: Action| {
let fut = async { Ok::<(), Infallible>(()) }; let fut = async { Ok::<(), Infallible>(()) };
@ -180,10 +186,10 @@ fn runtime(args: &ArgMatches<'static>) -> Result<RuntimeConfig> {
fut fut
}); });
Ok(config) Ok((config, filterer))
} }
// until 2.0 // until 2.0, then Powershell
#[cfg(windows)] #[cfg(windows)]
fn default_shell() -> Shell { fn default_shell() -> Shell {
Shell::Cmd Shell::Cmd

View File

@ -28,7 +28,11 @@ async fn main() -> Result<()> {
.ok(); .ok();
} }
let (init, runtime) = config::new(&args)?; let (init, runtime, filterer) = config::new(&args)?;
for filter in args.values_of("filter").unwrap_or_default() {
filterer.add_filter(filter.parse()?).await?;
}
let wx = Watchexec::new(init, runtime)?; let wx = Watchexec::new(init, runtime)?;