diff --git a/Cargo.lock b/Cargo.lock index 9949b3b..5a8ce95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -522,6 +522,15 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +[[package]] +name = "colored" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faf9468729b8cbcea668e36183cb69d317348c2e08e994829fb56ebfdfbaac34" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "combine" version = "4.6.7" @@ -1379,6 +1388,7 @@ dependencies = [ "ammonia", "askama", "clap", + "colored", "notify", "notify-debouncer-full", "pulldown-cmark", diff --git a/Cargo.toml b/Cargo.toml index 3b6b9d2..ef9f0c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ actix-ws = "0.4.0" ammonia = "4.1.1" askama = { version = "0.14.0", features = ["full"] } clap = { version = "4.5.46", features = ["derive"] } +colored = "3.1.1" notify = "8.2.0" notify-debouncer-full = "0.7.0" pulldown-cmark = "0.13.0" diff --git a/src/main.rs b/src/main.rs index 259cb30..2ec845d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use actix_web::web; use args::MdwatchArgs; use askama::Template; use clap::Parser; +use colored::Colorize; use std::path::PathBuf; use utils::{get_embedded_file, get_local_ip, get_markdown, get_random_port}; @@ -40,7 +41,8 @@ async fn home(file_info: web::Data) -> actix_web::Result && extension != "md" { eprintln!( - "Warning: Unsupported file type: .{}", + "{} Unsupported file type: .{}", + "Warning:".yellow().bold(), extension.to_string_lossy() ); return Err(actix_web::error::ErrorInternalServerError( @@ -51,7 +53,10 @@ async fn home(file_info: web::Data) -> actix_web::Result let html_output = match get_markdown(&file.to_path_buf()).await { Ok(html) => html, Err(e) => { - eprintln!("Error processing markdown file: {e}"); + eprintln!( + "{} Error processing markdown file: {e}", + "Error:".red().bold() + ); return Err(actix_web::error::ErrorInternalServerError( "Failed to process markdown file", )); @@ -66,7 +71,7 @@ async fn home(file_info: web::Data) -> actix_web::Result match template.render() { Ok(rendered) => Ok(HttpResponse::Ok().content_type("text/html").body(rendered)), Err(e) => { - eprintln!("Template rendering error: {e}"); + eprintln!("{} Template rendering error: {e}", "Error:".red().bold()); Ok(HttpResponse::InternalServerError() .content_type("text/plain") @@ -169,14 +174,20 @@ async fn main() -> std::io::Result<()> { let file_info = FileInfo { file, base_dir }; if ip == "0.0.0.0" { - eprintln!(" Warning: Binding to 0.0.0.0 exposes your server to the entire network!"); - eprintln!(" Make sure you trust your network or firewall settings."); + eprintln!( + "{} Binding to 0.0.0.0 exposes your server to the entire network!", + "Warning:".yellow().bold() + ); + eprintln!( + "{} Make sure you trust your network or firewall settings.", + "Note:".yellow() + ); let local_ip = get_local_ip().unwrap_or(String::from("0.0.0.0")); - println!("Server running at:"); - println!(" - http://{}:{}/", local_ip, port); + println!("{}", "Server running at:".green().bold()); + println!(" - {}", format!("http://{}:{}/", local_ip, port).cyan()); } else { - println!("Server running at:"); - println!(" - http://{}:{}/", ip, port); + println!("{}", "Server running at:".green().bold()); + println!(" - {}", format!("http://{}:{}/", ip, port).cyan()); } match HttpServer::new(move || { @@ -191,12 +202,12 @@ async fn main() -> std::io::Result<()> { { Ok(server) => { if let Err(e) = webbrowser::open(&format!("http://{}:{}/", ip, port)) { - eprintln!("Failed to open browser: {e}"); + eprintln!("{} Failed to open browser: {e}", "Error:".red().bold()); } server.run().await } Err(e) => { - eprintln!("Failed to start server: {e}"); + eprintln!("{} Failed to start server: {e}", "Error:".red().bold()); std::process::exit(1); } } diff --git a/src/utils.rs b/src/utils.rs index be33a46..b540620 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,6 @@ use ammonia::Builder; use ammonia::UrlRelative::PassThrough; +use colored::Colorize; use pulldown_cmark::Options; use regex::Regex; use rust_embed::Embed; @@ -37,12 +38,18 @@ pub fn get_embedded_file(file_path: &str) -> String { Some(file) => match std::str::from_utf8(&file.data) { Ok(content) => content.to_string(), Err(e) => { - eprintln!("Failed to read embedded file: {e}"); + eprintln!( + "{} Failed to read embedded file: {e}", + "Error:".red().bold() + ); String::new() } }, None => { - eprintln!("File not found in embedded files."); + eprintln!( + "{} File not found in embedded files.", + "Error:".red().bold() + ); String::new() } } diff --git a/src/ws_handler.rs b/src/ws_handler.rs index 5c19e8c..4e13b78 100644 --- a/src/ws_handler.rs +++ b/src/ws_handler.rs @@ -1,4 +1,5 @@ use actix_web::{HttpRequest, Responder, web}; +use colored::Colorize; use notify::RecursiveMode; use notify::event::ModifyKind; use notify::event::RemoveKind; @@ -39,7 +40,7 @@ pub async fn ws_handler( }), Err(errors) => errors .iter() - .for_each(|error| eprintln!("watch error: {error:?}")), + .for_each(|error| eprintln!("{} Watch error: {error:?}", "Error:".red().bold())), }, ) .map_err(actix_web::error::ErrorInternalServerError)?; @@ -65,7 +66,11 @@ pub async fn ws_handler( if is_selected_file { if matches!(event.kind, EventKind::Remove(RemoveKind::File)) { - eprintln!("File removed: {}", file.display()); + eprintln!( + "{} File removed: {}", + "Warning:".yellow().bold(), + file.display() + ); break; } let modified_selected_file = @@ -76,7 +81,7 @@ pub async fn ws_handler( let latest_markdown = match get_markdown(&file).await { Ok(md) => md, Err(e) => { - eprintln!("Error reading markdown file: {e}"); + eprintln!("{} Error reading markdown file: {e}", "Error:".red().bold()); continue; } };