diff --git a/Cargo.lock b/Cargo.lock index ea86b1c..a7e5c98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,6 +196,16 @@ dependencies = [ "utils 0.1.0", ] +[[package]] +name = "ctrlc" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dtoa" version = "0.4.2" @@ -326,6 +336,7 @@ dependencies = [ "chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.29.2 (registry+https://github.com/rust-lang/crates.io-index)", "content 0.1.0", + "ctrlc 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "errors 0.1.0", "front_matter 0.1.0", "iron 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1436,6 +1447,7 @@ dependencies = [ "checksum clap 2.29.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4151c5790817c7d21bbdc6c3530811f798172915f93258244948b93ba19604a6" "checksum cmake 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)" = "56d741ea7a69e577f6d06b36b7dff4738f680593dc27a701ffa8506b73ce28bb" "checksum coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c06169f5beb7e31c7c67ebf5540b8b472d23e3eade3b2ec7d1f5b504a85f91bd" +"checksum ctrlc 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "653abc99aa905f693d89df4797fadc08085baee379db92be9f2496cefe8a6f2c" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum duct 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8c553d79f40e74f7f611e49bf3429b6760cff79596b61818291c27cc0b18549d" "checksum either 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "740178ddf48b1a9e878e6d6509a1442a2d42fd2928aae8e7a6f8a36fb01981b3" diff --git a/Cargo.toml b/Cargo.toml index 5a2f15f..a747fcd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ iron = "0.6" mount = "0.4" notify = "4" ws = "0.7" +ctrlc = "3" site = { path = "components/site" } errors = { path = "components/errors" } diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index a243e27..ff06949 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -460,7 +460,7 @@ impl Site { pub fn clean(&self) -> Result<()> { if self.output_path.exists() { // Delete current `public` directory so we can start fresh - remove_dir_all(&self.output_path).chain_err(|| "Couldn't delete `public` directory")?; + remove_dir_all(&self.output_path).chain_err(|| "Couldn't delete output directory")?; } Ok(()) diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 7efdf5c..88bbb9e 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -22,6 +22,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. use std::env; +use std::fs::remove_dir_all; use std::path::Path; use std::sync::mpsc::channel; use std::time::{Instant, Duration}; @@ -33,6 +34,8 @@ use mount::Mount; use staticfile::Static; use notify::{Watcher, RecursiveMode, watcher}; use ws::{WebSocket, Sender, Message}; +use ctrlc; + use site::Site; use errors::{Result, ResultExt}; @@ -100,11 +103,10 @@ fn create_new_site(interface: &str, port: &str, output_dir: &str, config_file: & pub fn serve(interface: &str, port: &str, output_dir: &str, config_file: &str) -> Result<()> { let start = Instant::now(); let (mut site, address) = create_new_site(interface, port, output_dir, config_file)?; - console::report_elapsed_time(start); - let mut watching_static = false; // Setup watchers + let mut watching_static = false; let (tx, rx) = channel(); let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap(); watcher.watch("content/", RecursiveMode::Recursive) @@ -167,6 +169,12 @@ pub fn serve(interface: &str, port: &str, output_dir: &str, config_file: &str) - println!("Listening for changes in {}/{{{}}}", pwd, watchers.join(", ")); println!("Web server is available at http://{}", address); println!("Press Ctrl+C to stop\n"); + // Delete the output folder on ctrl+C + let output_path = Path::new(output_dir).to_path_buf(); + ctrlc::set_handler(move || { + remove_dir_all(&output_path).expect("Failed to delete output directory"); + ::std::process::exit(0); + }).expect("Error setting Ctrl-C handler"); use notify::DebouncedEvent::*; diff --git a/src/main.rs b/src/main.rs index 12add62..253561d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ extern crate mount; extern crate notify; extern crate url; extern crate ws; +extern crate ctrlc; extern crate site; #[macro_use]