@@ -35,6 +35,8 @@ use clap::{App, ArgMatches, SubCommand, AppSettings};
35
35
#[ cfg( feature = "watch" ) ]
36
36
use notify:: Watcher ;
37
37
#[ cfg( feature = "watch" ) ]
38
+ use std:: time:: Duration ;
39
+ #[ cfg( feature = "watch" ) ]
38
40
use std:: sync:: mpsc:: channel;
39
41
40
42
@@ -187,15 +189,13 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
187
189
open ( book. get_dest ( ) . join ( "index.html" ) ) ;
188
190
}
189
191
190
- trigger_on_change ( & mut book, |event, book| {
191
- if let Some ( path) = event. path {
192
- println ! ( "File changed: {:?}\n Building book...\n " , path) ;
193
- match book. build ( ) {
194
- Err ( e) => println ! ( "Error while building: {:?}" , e) ,
195
- _ => { } ,
196
- }
197
- println ! ( "" ) ;
192
+ trigger_on_change ( & mut book, |path, book| {
193
+ println ! ( "File changed: {:?}\n Building book...\n " , path) ;
194
+ match book. build ( ) {
195
+ Err ( e) => println ! ( "Error while building: {:?}" , e) ,
196
+ _ => { } ,
198
197
}
198
+ println ! ( "" ) ;
199
199
} ) ;
200
200
201
201
Ok ( ( ) )
@@ -258,15 +258,13 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
258
258
open ( format ! ( "http://{}" , address) ) ;
259
259
}
260
260
261
- trigger_on_change ( & mut book, move |event, book| {
262
- if let Some ( path) = event. path {
263
- println ! ( "File changed: {:?}\n Building book...\n " , path) ;
264
- match book. build ( ) {
265
- Err ( e) => println ! ( "Error while building: {:?}" , e) ,
266
- _ => broadcaster. send ( RELOAD_COMMAND ) . unwrap ( ) ,
267
- }
268
- println ! ( "" ) ;
261
+ trigger_on_change ( & mut book, move |path, book| {
262
+ println ! ( "File changed: {:?}\n Building book...\n " , path) ;
263
+ match book. build ( ) {
264
+ Err ( e) => println ! ( "Error while building: {:?}" , e) ,
265
+ _ => broadcaster. send ( RELOAD_COMMAND ) . unwrap ( ) ,
269
266
}
267
+ println ! ( "" ) ;
270
268
} ) ;
271
269
272
270
Ok ( ( ) )
@@ -307,53 +305,55 @@ fn open<P: AsRef<OsStr>>(path: P) {
307
305
// Calls the closure when a book source file is changed. This is blocking!
308
306
#[ cfg( feature = "watch" ) ]
309
307
fn trigger_on_change < F > ( book : & mut MDBook , closure : F ) -> ( )
310
- where F : Fn ( notify :: Event , & mut MDBook ) -> ( )
308
+ where F : Fn ( & Path , & mut MDBook ) -> ( )
311
309
{
310
+ use notify:: RecursiveMode :: * ;
311
+ use notify:: DebouncedEvent :: * ;
312
+
312
313
// Create a channel to receive the events.
313
314
let ( tx, rx) = channel ( ) ;
314
315
315
- let w: Result < notify:: RecommendedWatcher , notify:: Error > = notify:: Watcher :: new ( tx) ;
316
+ let mut watcher = match notify:: watcher ( tx, Duration :: from_secs ( 1 ) ) {
317
+ Ok ( w) => w,
318
+ Err ( e) => {
319
+ println ! ( "Error while trying to watch the files:\n \n \t {:?}" , e) ;
320
+ :: std:: process:: exit ( 0 ) ;
321
+ }
322
+ } ;
316
323
317
- match w {
318
- Ok ( mut watcher) => {
319
- // Add the source directory to the watcher
320
- if let Err ( e) = watcher. watch ( book. get_src ( ) ) {
321
- println ! ( "Error while watching {:?}:\n {:?}" , book. get_src( ) , e) ;
322
- :: std:: process:: exit ( 0 ) ;
323
- } ;
324
+ // Add the source directory to the watcher
325
+ if let Err ( e) = watcher. watch ( book. get_src ( ) , Recursive ) {
326
+ println ! ( "Error while watching {:?}:\n {:?}" , book. get_src( ) , e) ;
327
+ :: std:: process:: exit ( 0 ) ;
328
+ } ;
324
329
325
- // Add the book.json file to the watcher if it exists, because it's not
326
- // located in the source directory
327
- if let Err ( _) = watcher. watch ( book. get_root ( ) . join ( "book.json" ) ) {
328
- // do nothing if book.json is not found
329
- }
330
+ // Add the book.{json,toml} file to the watcher if it exists, because it's not
331
+ // located in the source directory
332
+ if let Err ( _) = watcher. watch ( book. get_root ( ) . join ( "book.json" ) , NonRecursive ) {
333
+ // do nothing if book.json is not found
334
+ }
335
+ if let Err ( _) = watcher. watch ( book. get_root ( ) . join ( "book.toml" ) , NonRecursive ) {
336
+ // do nothing if book.toml is not found
337
+ }
330
338
331
- let mut previous_time = time:: get_time ( ) ;
332
-
333
- println ! ( "\n Listening for changes...\n " ) ;
334
-
335
- loop {
336
- match rx. recv ( ) {
337
- Ok ( event) => {
338
- // Skip the event if an event has already been issued in the last second
339
- let time = time:: get_time ( ) ;
340
- if time - previous_time < time:: Duration :: seconds ( 1 ) {
341
- continue ;
342
- } else {
343
- previous_time = time;
344
- }
345
-
346
- closure ( event, book) ;
347
- } ,
348
- Err ( e) => {
349
- println ! ( "An error occured: {:?}" , e) ;
350
- } ,
339
+ println ! ( "\n Listening for changes...\n " ) ;
340
+
341
+ loop {
342
+ match rx. recv ( ) {
343
+ Ok ( event) => match event {
344
+ NoticeWrite ( path) |
345
+ NoticeRemove ( path) |
346
+ Create ( path) |
347
+ Write ( path) |
348
+ Remove ( path) |
349
+ Rename ( _, path) => {
350
+ closure ( & path, book) ;
351
351
}
352
- }
353
- } ,
354
- Err ( e) => {
355
- println ! ( "Error while trying to watch the files: \n \n \t {:?}" , e) ;
356
- :: std :: process :: exit ( 0 ) ;
357
- } ,
352
+ _ => { }
353
+ } ,
354
+ Err ( e) => {
355
+ println ! ( "An error occured: {:?}" , e) ;
356
+ } ,
357
+ }
358
358
}
359
359
}
0 commit comments