diff --git a/.gitignore b/.gitignore index 847a315..245b2a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /_test_area /site +.idea \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index f09b370..4ab05ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,7 +295,7 @@ dependencies = [ [[package]] name = "doctave" -version = "0.4.0" +version = "0.4.2" dependencies = [ "alphanumeric-sort", "ascii", @@ -312,6 +312,7 @@ dependencies = [ "indoc", "lazy_static", "notify", + "open", "rayon", "scoped_threadpool", "serde", @@ -882,6 +883,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "open" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4a3100141f1733ea40b53381b0ae3117330735ef22309a190ac57b9576ea716" +dependencies = [ + "pathdiff", + "windows-sys", +] + [[package]] name = "parking_lot" version = "0.11.2" @@ -921,6 +932,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "percent-encoding" version = "1.0.1" @@ -1607,6 +1624,49 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" + +[[package]] +name = "windows_i686_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" + +[[package]] +name = "windows_i686_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" + [[package]] name = "ws2_32-sys" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 3bfe17f..1bb6da8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ rayon = "1.4" colorsys = "0.5.7" alphanumeric-sort = "1.4.0" include_dir = "0.7.2" +open = "3.0.3" [dev-dependencies] indoc = "1.0.2" diff --git a/src/main.rs b/src/main.rs index 98b3556..667cc69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,7 +54,13 @@ fn main() { Ok(_) => Ok(()), Err(e) => Err(e.to_string()), }), - ), + ).arg( + Arg::with_name("open") + .long("open") + .short("o") + .help("Opens the docs site on chrome") + ) + , ) .get_matches(); @@ -118,6 +124,8 @@ fn serve(cmd: &ArgMatches) -> doctave::Result<()> { options.port = Some(p.parse::().unwrap()); } + options.open = cmd.is_present("open"); + if cmd.is_present("no-color") { config.disable_colors(); } diff --git a/src/preview_server.rs b/src/preview_server.rs index e33cf85..81d92e2 100644 --- a/src/preview_server.rs +++ b/src/preview_server.rs @@ -14,15 +14,17 @@ pub struct PreviewServer { base_path: String, addr: SocketAddr, site: Arc>, + open: bool } impl PreviewServer { - pub fn new(addr: &str, site: Arc>, color: bool, base_path: String) -> Self { + pub fn new(addr: &str, site: Arc>, color: bool, base_path: String, open: bool) -> Self { PreviewServer { addr: addr.parse().expect("invalid address for preview server"), site, color, base_path, + open } } @@ -44,6 +46,10 @@ impl PreviewServer { self.base_path ) .unwrap(); + + if self.open { + open_web_app(self.addr.to_string().as_str()); + } } for request in server.incoming_requests() { @@ -156,3 +162,7 @@ fn content_type_for(extension: Option<&OsStr>) -> Option<&'static str> { None => None, } } + +fn open_web_app(url: &str) -> std::io::Result<()> { + open::that("http://".to_owned()+url) +} \ No newline at end of file diff --git a/src/serve.rs b/src/serve.rs index e4ba0eb..8ff3921 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -17,6 +17,7 @@ pub struct ServeCommand {} #[derive(Default)] pub struct ServeOptions { pub port: Option, + pub open: bool } impl ServeCommand { @@ -70,6 +71,7 @@ impl ServeCommand { site.clone(), config.color_enabled(), config.base_path().to_owned(), + options.open ); thread::Builder::new() .name("http-server".into())