diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index c5394f8aae..39c1ca34e1 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -100,9 +100,11 @@ pub fn execute(args: &ArgMatches) -> Result<()> { let serving_url = format!("http://{}", address); info!("Serving on: {}", serving_url); - if open_browser { - open(serving_url); - } + let open_thread = if open_browser { + Some(std::thread::spawn(|| open(serving_url))) + } else { + None + }; #[cfg(feature = "watch")] watch::trigger_on_change(&book, move |paths, book_dir| { @@ -123,6 +125,10 @@ pub fn execute(args: &ArgMatches) -> Result<()> { } }); + if let Some(h) = open_thread { + let _ = h.join(); + } + let _ = thread_handle.join(); Ok(()) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index b27516b064..0258a52f32 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -37,10 +37,13 @@ pub fn execute(args: &ArgMatches) -> Result<()> { }; update_config(&mut book); - if args.is_present("open") { + let open_thread = if args.is_present("open") { book.build()?; - open(book.build_dir_for("html").join("index.html")); - } + let path = book.build_dir_for("html").join("index.html"); + Some(std::thread::spawn(|| open(path))) + } else { + None + }; trigger_on_change(&book, |paths, book_dir| { info!("Files changed: {:?}\nBuilding book...\n", paths); @@ -55,6 +58,10 @@ pub fn execute(args: &ArgMatches) -> Result<()> { } }); + if let Some(h) = open_thread { + let _ = h.join(); + } + Ok(()) }