Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 22 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -40,7 +41,8 @@ async fn home(file_info: web::Data<FileInfo>) -> actix_web::Result<HttpResponse>
&& extension != "md"
{
eprintln!(
"Warning: Unsupported file type: .{}",
"{} Unsupported file type: .{}",
"Warning:".yellow().bold(),
extension.to_string_lossy()
);
return Err(actix_web::error::ErrorInternalServerError(
Expand All @@ -51,7 +53,10 @@ async fn home(file_info: web::Data<FileInfo>) -> actix_web::Result<HttpResponse>
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",
));
Expand All @@ -66,7 +71,7 @@ async fn home(file_info: web::Data<FileInfo>) -> actix_web::Result<HttpResponse>
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")
Expand Down Expand Up @@ -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 || {
Expand All @@ -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);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/ws_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use actix_web::{HttpRequest, Responder, web};
use colored::Colorize;
use notify::RecursiveMode;
use notify::event::ModifyKind;
use notify::event::RemoveKind;
Expand Down Expand Up @@ -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)?;
Expand All @@ -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 =
Expand All @@ -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;
}
};
Expand Down
Loading