operations now show elapsed times
This commit is contained in:
parent
772411da73
commit
6b3d12e7f9
2 changed files with 41 additions and 8 deletions
|
@ -3,13 +3,15 @@ use std::{
|
|||
env::var,
|
||||
fs::{self, read_to_string, File },
|
||||
io::Write,
|
||||
path::{ Path, PathBuf }
|
||||
path::{ Path, PathBuf },
|
||||
time::SystemTime
|
||||
};
|
||||
|
||||
use termion::{
|
||||
color::{ self, Fg },
|
||||
style::{
|
||||
Bold as BOLD,
|
||||
Faint as FAINT,
|
||||
Italic as ITALIC,
|
||||
Reset as RESET
|
||||
}
|
||||
|
@ -19,7 +21,8 @@ use upon::Engine;
|
|||
|
||||
use crate::{
|
||||
config::Config,
|
||||
filter
|
||||
filter,
|
||||
util::time
|
||||
};
|
||||
|
||||
static SUCCESS: Fg<color::Green> = Fg(color::Green);
|
||||
|
@ -27,9 +30,12 @@ static WARNING: Fg<color::Yellow> = Fg(color::Yellow);
|
|||
static FAILURE: Fg<color::Red> = Fg(color::Red);
|
||||
|
||||
pub fn apply(targets: &Vec<Map<String, Value>>) {
|
||||
let start = SystemTime::now();
|
||||
|
||||
let home = var("HOME").unwrap();
|
||||
println!("running apply:");
|
||||
for target in targets {
|
||||
let start = SystemTime::now();
|
||||
// get path and name
|
||||
let path = target.get("path");
|
||||
let i_name = target.get("name");
|
||||
|
@ -62,12 +68,17 @@ pub fn apply(targets: &Vec<Map<String, Value>>) {
|
|||
println!(" {BOLD}{FAILURE}failed to copy{RESET}");
|
||||
}
|
||||
else {
|
||||
println!(" {BOLD}{SUCCESS}completed{RESET}");
|
||||
let time = time(start);
|
||||
println!(" {BOLD}{SUCCESS}completed{RESET} {FAINT}({time}){RESET}");
|
||||
}
|
||||
}
|
||||
let time = time(start);
|
||||
println!("{FAINT}(apply: {time}){RESET}");
|
||||
}
|
||||
|
||||
pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &Config) {
|
||||
let start = SystemTime::now();
|
||||
|
||||
let home = var("HOME").unwrap();
|
||||
println!("running build:");
|
||||
|
||||
|
@ -75,6 +86,7 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
|
|||
engine.add_filter("has", filter::has);
|
||||
|
||||
for target in targets {
|
||||
let start = SystemTime::now();
|
||||
let context = config.context(target);
|
||||
// get name property
|
||||
let i_name = target.get("name");
|
||||
|
@ -89,7 +101,8 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
|
|||
println!(" building \"{name}\":");
|
||||
|
||||
// compile
|
||||
println!(" {ITALIC}compiling{RESET}");
|
||||
let compile_start = SystemTime::now();
|
||||
print!(" {ITALIC}compiling{RESET}");
|
||||
let mut path = PathBuf::from(&template_dir);
|
||||
if let Some(Value::String(base)) = target.get("base") { path.push(base); }
|
||||
else { path.push(name); }
|
||||
|
@ -97,17 +110,22 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
|
|||
let content = read_to_string(path).unwrap();
|
||||
let template = engine.compile(&content);
|
||||
if let Err(error) = template {
|
||||
println!(" {BOLD}{FAILURE}failed to compile template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}");
|
||||
println!("\n {BOLD}{FAILURE}failed to compile template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}");
|
||||
continue;
|
||||
}
|
||||
let compile_time = time(compile_start);
|
||||
println!(" {FAINT}({compile_time}){RESET}");
|
||||
|
||||
// render
|
||||
println!(" {ITALIC}rendering{RESET}");
|
||||
let render_start = SystemTime::now();
|
||||
print!(" {ITALIC}rendering{RESET}");
|
||||
let render = template.unwrap().render(&engine, &context).to_string();
|
||||
if let Err(error) = render {
|
||||
println!(" {BOLD}{FAILURE}failed to render template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}");
|
||||
println!("\n {BOLD}{FAILURE}failed to render template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}");
|
||||
continue;
|
||||
}
|
||||
let render_time = time(render_start);
|
||||
println!(" {FAINT}({render_time}){RESET}");
|
||||
|
||||
// get rendered text and open destination file
|
||||
let output = render.unwrap();
|
||||
|
@ -122,7 +140,12 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
|
|||
// write to destination file
|
||||
let written = write!(&mut file, "{output}");
|
||||
if written.is_err() { println!(" {FAILURE}failed to write to destination file at {path:?}{RESET}"); }
|
||||
else { println!(" {BOLD}{SUCCESS}completed{RESET}"); }
|
||||
else {
|
||||
let time = time(start);
|
||||
println!(" {BOLD}{SUCCESS}completed{RESET} {FAINT}({time}){RESET}");
|
||||
}
|
||||
}
|
||||
let time = time(start);
|
||||
println!("{FAINT}(build: {time}){RESET}");
|
||||
}
|
||||
|
||||
|
|
10
src/util.rs
10
src/util.rs
|
@ -1,3 +1,4 @@
|
|||
use std::time::SystemTime;
|
||||
|
||||
use upon::Value as ContextValue;
|
||||
use toml::{ value::Array, Value };
|
||||
|
@ -23,3 +24,12 @@ pub fn convert(value: &Value) -> Option<ContextValue> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn time(start: SystemTime) -> String {
|
||||
let now = SystemTime::now();
|
||||
if let Ok(duration) = now.duration_since(start) {
|
||||
let ms = duration.as_millis();
|
||||
if ms > 0 { format!("{ms} ms") }
|
||||
else { "< 1 ms".to_owned() }
|
||||
} else { String::new() }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue