diff --git a/src/args.rs b/src/args.rs index c3e6b9f..0db9737 100644 --- a/src/args.rs +++ b/src/args.rs @@ -3,6 +3,7 @@ use log::LevelFilter; use std::{ ffi::OsString, path::{PathBuf, MAIN_SEPARATOR}, + time::Duration, }; use crate::config::{Config, ConfigBuilder}; @@ -205,11 +206,11 @@ where builder.ignores(ignores); if args.occurrences_of("poll") > 0 { - builder.poll_interval(value_t!(args.value_of("poll"), u32).unwrap_or_else(|e| e.exit())); + builder.poll_interval(Duration::from_millis(value_t!(args.value_of("poll"), u64).unwrap_or_else(|e| e.exit()))); } if args.occurrences_of("debounce") > 0 { - builder.debounce(value_t!(args.value_of("debounce"), u64).unwrap_or_else(|e| e.exit())); + builder.debounce(Duration::from_millis(value_t!(args.value_of("debounce"), u64).unwrap_or_else(|e| e.exit()))); } builder.on_busy_update(if args.is_present("restart") { diff --git a/src/config.rs b/src/config.rs index 20d8241..dcbeea6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,7 +13,7 @@ //! .expect("mission failed"); //! ``` -use std::path::PathBuf; +use std::{time::Duration, path::PathBuf}; use crate::process::Shell; use crate::run::OnBusyUpdate; @@ -43,9 +43,9 @@ pub struct Config { /// Specify what to do when receiving updates while the command is running. #[builder(default)] pub on_busy_update: OnBusyUpdate, - /// Interval to debounce the changes. (milliseconds) - #[builder(default = "500")] - pub debounce: u64, + /// Interval to debounce the changes. + #[builder(default = "Duration::from_millis(500)")] + pub debounce: Duration, /// Run the commands right after starting. #[builder(default = "true")] pub run_initially: bool, @@ -72,9 +72,9 @@ pub struct Config { /// Force using the polling backend. #[builder(default)] pub poll: bool, - /// Interval for polling. (milliseconds) - #[builder(default = "1000")] - pub poll_interval: u32, + /// Interval for polling. + #[builder(default = "Duration::from_secs(1)")] + pub poll_interval: Duration, } impl ConfigBuilder { diff --git a/src/run.rs b/src/run.rs index 849e2e5..e7f4413 100644 --- a/src/run.rs +++ b/src/run.rs @@ -131,7 +131,7 @@ where })?; if watcher.is_polling() { - warn!("Polling for changes every {} ms", args.poll_interval); + warn!("Polling for changes every {:?} ms", args.poll_interval); } // Call handler initially, if necessary @@ -278,7 +278,7 @@ pub fn run(args: Config) -> Result<()> { fn wait_fs( rx: &Receiver, filter: &NotificationFilter, - debounce: u64, + debounce: Duration, no_meta: bool, ) -> Vec { let mut paths = Vec::new(); @@ -310,8 +310,7 @@ fn wait_fs( } // Wait for filesystem activity to cool off - let timeout = Duration::from_millis(debounce); - while let Ok(e) = rx.recv_timeout(timeout) { + while let Ok(e) = rx.recv_timeout(debounce) { if let Some(ref path) = e.path { let pathop = PathOp::new(path, e.op.ok(), e.cookie); if cache.contains_key(&pathop) { diff --git a/src/watcher.rs b/src/watcher.rs index 9899291..bc4ca03 100644 --- a/src/watcher.rs +++ b/src/watcher.rs @@ -1,4 +1,6 @@ use notify::{raw_watcher, PollWatcher, RecommendedWatcher, RecursiveMode}; +use std::convert::TryFrom; +use std::time::Duration; use std::path::PathBuf; use std::sync::mpsc::Sender; @@ -25,12 +27,12 @@ impl Watcher { tx: Sender, paths: &[PathBuf], poll: bool, - interval_ms: u32, + interval: Duration, ) -> Result { use notify::Watcher; let imp = if poll { - let mut watcher = PollWatcher::with_delay_ms(tx, interval_ms)?; + let mut watcher = PollWatcher::with_delay_ms(tx, u32::try_from(interval.as_millis()).unwrap_or(u32::MAX))?; for path in paths { watcher.watch(path, RecursiveMode::Recursive)?; debug!("Watching {:?}", path);