From 29dfd4b0074dc84c12fdd9425ac3c845e683ff53 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Mon, 2 Sep 2024 15:00:25 -0400 Subject: [PATCH] Start working on watcher --- src/main.rs | 65 +++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index c66cd38..fcc33da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,18 +18,22 @@ struct Cli { #[derive(Subcommand, Debug)] enum Command { Fmt(FmtArgs), - // Watch(WatchArgs), + Watch(WatchArgs), } -// #[derive(Args, Debug)] -// struct WatchArgs { -// dir: PathBuf -// } +#[derive(Args, Debug)] +struct WatchArgs { + path: PathBuf, + + #[clap(short = 'I', long = "inplace")] + inplace: bool, +} #[derive(Args, Debug)] struct FmtArgs { - #[clap(short = 'i', long = "input")] + // #[clap(short = 'i', long = "input")] input: PathBuf, + #[clap(short = 'o', long = "output")] output: Option, @@ -81,6 +85,7 @@ fn main() -> Result<()> { } format_single_file(&a)?; } + Command::Watch(a) => watch(&a)?, } Ok(()) @@ -117,28 +122,30 @@ fn format_single_file(args: &FmtArgs) -> Result<()> { Ok(()) } -// fn x() { -// // Select recommended watcher for debouncer. -// // Using a callback here, could also be a channel. -// let mut debouncer = -// new_debouncer( -// Duration::from_millis(50), -// |res: DebounceEventResult| match res { -// Ok(events) => events -// .iter() -// .for_each(|e| println!("Event {:?} for {:?}", e.kind, e.path)), -// Err(e) => println!("Error {:?}", e), -// }, -// ) -// .unwrap(); +fn watch(args: &WatchArgs) -> Result<()> { + // Select recommended watcher for debouncer. + // Using a callback here, could also be a channel. + let mut debouncer = + new_debouncer( + Duration::from_millis(50), + |res: DebounceEventResult| match res { + Ok(events) => events + .iter() + .for_each(|e| println!("Event {:?} for {:?}", e.kind, e.path)), + Err(e) => println!("Error {:?}", e), + }, + ) + .context("Creating debouncer")?; -// // Add a path to be watched. All files and directories at that path and -// // below will be monitored for changes. -// debouncer -// .watcher() -// .watch(Path::new("."), RecursiveMode::Recursive) -// .unwrap(); + // Add a path to be watched. All files and directories at that path and + // below will be monitored for changes. + debouncer + .watcher() + .watch(Path::new("."), RecursiveMode::Recursive) + .context("Adding watch to debouncer")?; -// // note that dropping the debouncer (as will happen here) also ends the debouncer -// // thus this demo would need an endless loop to keep running -// } + // note that dropping the debouncer (as will happen here) also ends the debouncer + // thus this demo would need an endless loop to keep running + + Ok(()) +}