From 0e34123023e2d351a83e29c70d4c2563db0ad9e9 Mon Sep 17 00:00:00 2001 From: James Kominick Date: Wed, 6 Sep 2017 19:55:58 -0400 Subject: [PATCH] Add `-d, --debounce` option - Add configurable debounce timeout option behind `-d, --debounce` - Move `-d, --debug` flags to `-v, --verbose` --- src/cli.rs | 22 ++++++++++++++++++---- src/run.rs | 6 +++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 303529c..2916650 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,6 +12,7 @@ pub struct Args { pub clear_screen: bool, pub signal: Option, pub restart: bool, + pub debounce: u64, pub debug: bool, pub run_initially: bool, pub no_shell: bool, @@ -72,10 +73,16 @@ pub fn get_args() -> Args { .help("Send SIGKILL to child processes (deprecated, use -s SIGKILL instead)") .short("k") .long("kill")) - .arg(Arg::with_name("debug") - .help("Print debugging messages to stderr") + .arg(Arg::with_name("debounce") + .help("Set the timeout between detected change and command execution, defaults to 500ms") + .takes_value(true) + .value_name("milliseconds") .short("d") - .long("debug")) + .long("debounce")) + .arg(Arg::with_name("verbose") + .help("Print debugging messages to stderr") + .short("v") + .long("verbose")) .arg(Arg::with_name("filter") .help("Ignore all modifications except those matching the pattern") .short("f") @@ -157,6 +164,12 @@ pub fn get_args() -> Args { 1000 }; + let debounce = if args.occurrences_of("debounce") > 0 { + value_t!(args.value_of("debounce"), u64).unwrap_or_else(|e| e.exit()) + } else { + 500 + }; + if signal.is_some() && args.is_present("postpone") { // TODO: Error::argument_conflict() might be the better fit, usage was unclear, though Error::value_validation_auto("--postpone and --signal are mutually exclusive".to_string()) @@ -177,7 +190,8 @@ pub fn get_args() -> Args { signal: signal, clear_screen: args.is_present("clear"), restart: args.is_present("restart"), - debug: args.is_present("debug"), + debounce: debounce, + debug: args.is_present("verbose"), run_initially: !args.is_present("postpone"), no_shell: args.is_present("no-shell"), no_vcs_ignore: args.is_present("no-vcs-ignore"), diff --git a/src/run.rs b/src/run.rs index cdeb66c..f68b2ff 100644 --- a/src/run.rs +++ b/src/run.rs @@ -87,7 +87,7 @@ pub fn run(args: cli::Args) { loop { debug!("Waiting for filesystem activity"); - let paths = wait_fs(&rx, &filter); + let paths = wait_fs(&rx, &filter, args.debounce); if let Some(path) = paths.get(0) { debug!("Path updated: {:?}", path); } @@ -167,7 +167,7 @@ pub fn run(args: cli::Args) { } } -fn wait_fs(rx: &Receiver, filter: &NotificationFilter) -> Vec { +fn wait_fs(rx: &Receiver, filter: &NotificationFilter, debounce: u64) -> Vec { let mut paths = vec![]; let mut cache = HashMap::new(); @@ -190,7 +190,7 @@ fn wait_fs(rx: &Receiver, filter: &NotificationFilter) -> Vec { } // Wait for filesystem activity to cool off - let timeout = Duration::from_millis(500); + let timeout = Duration::from_millis(debounce); while let Ok(e) = rx.recv_timeout(timeout) { if let Some(ref path) = e.path { if cache.contains_key(path) {