Add -d, --debounce option

- Add configurable debounce timeout option behind `-d, --debounce`
- Move `-d, --debug` flags to `-v, --verbose`
This commit is contained in:
James Kominick 2017-09-06 19:55:58 -04:00
parent 7b76d3d268
commit 0e34123023
2 changed files with 21 additions and 7 deletions

View File

@ -12,6 +12,7 @@ pub struct Args {
pub clear_screen: bool, pub clear_screen: bool,
pub signal: Option<String>, pub signal: Option<String>,
pub restart: bool, pub restart: bool,
pub debounce: u64,
pub debug: bool, pub debug: bool,
pub run_initially: bool, pub run_initially: bool,
pub no_shell: 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)") .help("Send SIGKILL to child processes (deprecated, use -s SIGKILL instead)")
.short("k") .short("k")
.long("kill")) .long("kill"))
.arg(Arg::with_name("debug") .arg(Arg::with_name("debounce")
.help("Print debugging messages to stderr") .help("Set the timeout between detected change and command execution, defaults to 500ms")
.takes_value(true)
.value_name("milliseconds")
.short("d") .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") .arg(Arg::with_name("filter")
.help("Ignore all modifications except those matching the pattern") .help("Ignore all modifications except those matching the pattern")
.short("f") .short("f")
@ -157,6 +164,12 @@ pub fn get_args() -> Args {
1000 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") { if signal.is_some() && args.is_present("postpone") {
// TODO: Error::argument_conflict() might be the better fit, usage was unclear, though // 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()) Error::value_validation_auto("--postpone and --signal are mutually exclusive".to_string())
@ -177,7 +190,8 @@ pub fn get_args() -> Args {
signal: signal, signal: signal,
clear_screen: args.is_present("clear"), clear_screen: args.is_present("clear"),
restart: args.is_present("restart"), restart: args.is_present("restart"),
debug: args.is_present("debug"), debounce: debounce,
debug: args.is_present("verbose"),
run_initially: !args.is_present("postpone"), run_initially: !args.is_present("postpone"),
no_shell: args.is_present("no-shell"), no_shell: args.is_present("no-shell"),
no_vcs_ignore: args.is_present("no-vcs-ignore"), no_vcs_ignore: args.is_present("no-vcs-ignore"),

View File

@ -87,7 +87,7 @@ pub fn run(args: cli::Args) {
loop { loop {
debug!("Waiting for filesystem activity"); 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) { if let Some(path) = paths.get(0) {
debug!("Path updated: {:?}", path); debug!("Path updated: {:?}", path);
} }
@ -167,7 +167,7 @@ pub fn run(args: cli::Args) {
} }
} }
fn wait_fs(rx: &Receiver<Event>, filter: &NotificationFilter) -> Vec<PathBuf> { fn wait_fs(rx: &Receiver<Event>, filter: &NotificationFilter, debounce: u64) -> Vec<PathBuf> {
let mut paths = vec![]; let mut paths = vec![];
let mut cache = HashMap::new(); let mut cache = HashMap::new();
@ -190,7 +190,7 @@ fn wait_fs(rx: &Receiver<Event>, filter: &NotificationFilter) -> Vec<PathBuf> {
} }
// Wait for filesystem activity to cool off // 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) { while let Ok(e) = rx.recv_timeout(timeout) {
if let Some(ref path) = e.path { if let Some(ref path) = e.path {
if cache.contains_key(path) { if cache.contains_key(path) {