improved the logging
This commit is contained in:
@@ -5,7 +5,7 @@ use std::{
|
||||
|
||||
use crate::app::{
|
||||
build::{docki_build, DockiBuildResult},
|
||||
fs_util,
|
||||
fs_util, log::display_status,
|
||||
};
|
||||
|
||||
pub struct BuildExecution {
|
||||
@@ -73,23 +73,21 @@ impl BuildExecution {
|
||||
|
||||
match result {
|
||||
DockiBuildResult::Err(err) => {
|
||||
self.display_status("Error", in_path, "");
|
||||
self.display_building_status("Error", in_path, "");
|
||||
println!("{}", err)
|
||||
},
|
||||
DockiBuildResult::Copy(out_path) => self.display_status("Copy", &in_path, &out_path),
|
||||
DockiBuildResult::Slide(out_path) => self.display_status("Slide", &in_path, &out_path),
|
||||
DockiBuildResult::Doc(out_path) => self.display_status("Doc", &in_path, &out_path)
|
||||
DockiBuildResult::Copy(out_path) => self.display_building_status("Copy", &in_path, &out_path),
|
||||
DockiBuildResult::Slide(out_path) => self.display_building_status("Slide", &in_path, &out_path),
|
||||
DockiBuildResult::Doc(out_path) => self.display_building_status("Doc", &in_path, &out_path)
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
fn display_status(&self, status_type: &str, in_path: &str, out_path: &str) -> () {
|
||||
println!(
|
||||
"({} / {}) [{}] {} -> {}",
|
||||
self.progress, self.goal, status_type, in_path, out_path
|
||||
);
|
||||
fn display_building_status(&self, status_type: &str, in_path: &str, out_path: &str) -> () {
|
||||
let progress_str = format!("{} / {}", self.progress, self.goal);
|
||||
display_status(&progress_str, status_type, in_path, out_path);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@ use notify::{
|
||||
event::ModifyKind,
|
||||
Event, EventKind, RecursiveMode, Watcher,
|
||||
};
|
||||
use std::{env, path::Path};
|
||||
use std::{env, path::Path, process::Output};
|
||||
|
||||
use crate::app::{ watcher::watcher, build::docki_build};
|
||||
use crate::app::{ watcher::watcher, build::{docki_build, DockiBuildResult}, commands::build::build, log::display_status};
|
||||
|
||||
|
||||
pub async fn serve() {
|
||||
build().await;
|
||||
tokio::join!(watch_and_build(), start_server());
|
||||
}
|
||||
|
||||
@@ -21,7 +22,7 @@ async fn watch_and_build() {
|
||||
}
|
||||
|
||||
async fn start_server() {
|
||||
println!("Serving at http://localhost:8080");
|
||||
println!("\nServing at {} ", "http://localhost:8080".bold());
|
||||
|
||||
let Ok(()) = listen("localhost", 8080, "./dist").await else {
|
||||
panic!("could not start server")
|
||||
@@ -44,14 +45,13 @@ async fn watch(path: &Path) -> notify::Result<()> {
|
||||
fn file_change(event: Event) {
|
||||
match event.kind {
|
||||
EventKind::Modify(ModifyKind::Data(_)) => {
|
||||
build_file(event.paths).expect("building file failed");
|
||||
()
|
||||
build_file(event.paths)
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_file(paths: Vec<std::path::PathBuf>) -> Result<(), String> {
|
||||
fn build_file(paths: Vec<std::path::PathBuf>) {
|
||||
let invalid_path_message = "changed path is invalid";
|
||||
let in_path = paths
|
||||
.first()
|
||||
@@ -61,10 +61,22 @@ fn build_file(paths: Vec<std::path::PathBuf>) -> Result<(), String> {
|
||||
.replace(¤t_dir(), "")
|
||||
.replace("/./", "./");
|
||||
|
||||
println!("{} {}", "[Rebuilding]".green(), in_path);
|
||||
|
||||
docki_build(&in_path);
|
||||
return Ok(())
|
||||
let result = docki_build(&in_path);
|
||||
|
||||
match result {
|
||||
DockiBuildResult::Slide(out_path) => display_rebuilding_status("Slide", &in_path, &out_path),
|
||||
DockiBuildResult::Doc(out_path) => display_rebuilding_status("Doc", &in_path, &out_path),
|
||||
DockiBuildResult::Copy(out_path) => display_rebuilding_status("Copy", &in_path, &out_path),
|
||||
DockiBuildResult::Err(err) => {
|
||||
display_rebuilding_status("Error", &in_path, "");
|
||||
println!("{}", err);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn display_rebuilding_status(context: &str, in_path: &str, out_path: &str) {
|
||||
display_status("Rebuildng", context, in_path, out_path)
|
||||
}
|
||||
|
||||
fn current_dir() -> String {
|
||||
|
||||
20
src/app/log/mod.rs
Normal file
20
src/app/log/mod.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use colored::Colorize;
|
||||
|
||||
pub fn display_status(context1: &str, context2: &str, in_path: &str, out_path: &str) {
|
||||
let colored_context = color_context(context2);
|
||||
println!(
|
||||
"({}) [{}] {} -> {}",
|
||||
context1.bold(),
|
||||
colored_context,
|
||||
in_path,
|
||||
out_path
|
||||
);
|
||||
}
|
||||
|
||||
fn color_context(context: &str) -> colored::ColoredString {
|
||||
if context == "Error" {
|
||||
return context.bright_red()
|
||||
} else {
|
||||
return context.bright_green()
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ mod commands;
|
||||
pub mod build;
|
||||
pub mod fs_util;
|
||||
pub mod watcher;
|
||||
pub mod log;
|
||||
mod args;
|
||||
|
||||
use std::env;
|
||||
|
||||
Reference in New Issue
Block a user