From 719e48c0a5f36cad505d2b0b6dd4a2bc9b212a6e Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Wed, 6 Jul 2022 16:16:16 +0800 Subject: [PATCH] feat: beautify console --- Cargo.toml | 3 ++- src/cli/mod.rs | 6 +----- src/lib.rs | 4 +++- src/server/mod.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 42de2ba4c..fa14b36b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dioxus-cli" -version = "0.1.4" +version = "0.1.5" authors = ["Jonathan Kelley"] edition = "2021" description = "CLI tool for developing, testing, and publishing Dioxus apps" @@ -14,6 +14,7 @@ license = "MIT/Apache-2.0" clap = { version = "3.0.14", features = ["derive"] } thiserror = "1.0.30" wasm-bindgen-cli-support = "0.2.79" +colored = "2.0.0" # features log = "0.4.14" diff --git a/src/cli/mod.rs b/src/cli/mod.rs index d639d28a7..b456d4705 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -27,7 +27,7 @@ use std::{ /// Build, bundle, & ship your Dioxus app. #[derive(Parser)] -#[clap(name = "dioxus")] +#[clap(name = "dioxus", version)] pub struct Cli { #[clap(subcommand)] pub action: Commands, @@ -35,10 +35,6 @@ pub struct Cli { /// Enable verbose logging. #[clap(short)] pub v: bool, - // // note: dioxus is still roughly compatible with trunk - // /// Path to the Trunk config file [default: Trunk.toml] - // #[clap(long, parse(from_os_str), env = "TRUNK_CONFIG")] - // pub config: Option, } #[derive(Parser)] diff --git a/src/lib.rs b/src/lib.rs index 6d009c18c..9956989b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +pub const DIOXUS_CLI_VERSION: &'static str = "0.1.5"; + pub mod builder; pub mod server; pub mod tools; @@ -19,4 +21,4 @@ pub use error::*; pub mod logging; pub use logging::*; -pub mod hot_reload; +pub mod hot_reload; \ No newline at end of file diff --git a/src/server/mod.rs b/src/server/mod.rs index 446d9d75c..886088fd9 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -6,11 +6,12 @@ use axum::{ routing::{get, get_service}, Router, }; +use colored::Colorize; use dioxus::rsx_interpreter::SetRsxMessage; use notify::{RecommendedWatcher, Watcher}; use syn::spanned::Spanned; -use std::{path::PathBuf, sync::Arc}; +use std::{path::PathBuf, process::Command, sync::Arc}; use tower::ServiceBuilder; use tower_http::services::fs::{ServeDir, ServeFileSystemResponseBody}; @@ -180,7 +181,7 @@ pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> { } // start serve dev-server at 0.0.0.0:8080 - log::info!("📡 Dev-Server is started at: http://127.0.0.1:{}/", port); + print_console_info(port, &config); let file_service_config = config.clone(); let file_service = ServiceBuilder::new() @@ -290,7 +291,7 @@ pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> { } // start serve dev-server at 0.0.0.0 - log::info!("📡 Dev-Server is started at: http://127.0.0.1:{}/", port); + print_console_info(port, &config); let file_service_config = config.clone(); let file_service = ServiceBuilder::new() @@ -348,6 +349,44 @@ pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> { Ok(()) } +fn print_console_info(port: u16, config: &CrateConfig) { + print!( + "{}", + String::from_utf8_lossy( + &Command::new(if cfg!(target_os = "windows") { + "cls" + } else { + "clear" + }) + .output() + .unwrap() + .stdout + ) + ); + + let mut profile = if config.release { "Release" } else { "Debug" }.to_string(); + if config.custom_profile.is_some() { + profile = config.custom_profile.as_ref().unwrap().to_string(); + } + let hot_reload = if config.hot_reload { "RSX" } else { "Normal" }; + let workspace = config.workspace_dir.to_str().unwrap().to_string(); + + println!( + "{} @ v{}\n", + "Dioxus".bold().green(), + crate::DIOXUS_CLI_VERSION + ); + println!( + "\t> Local: {}", + format!("https://localhost:{}/", port).blue() + ); + println!("\t> Profile: {}", profile.green()); + println!("\t> Hot Reload: {}", hot_reload.green()); + println!("\t> Workspace: {}", workspace.yellow()); + + println!("\n{}", "Server startup completed.".bold()); +} + async fn ws_handler( ws: WebSocketUpgrade, _: Option>,