From 20da7322f72fc8d40f3266c5fdad72915396af62 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 17 Aug 2023 17:19:34 -0400 Subject: [PATCH] added colors to operation outputs and normalized indents --- src/operation.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/operation.rs b/src/operation.rs index f635fd1..d9daf67 100644 --- a/src/operation.rs +++ b/src/operation.rs @@ -14,6 +14,11 @@ use termion::{ }; use toml::{ map::Map, Value }; +static FAIL: Fg = Fg(color::LightRed); +static SUCCESS: Fg = Fg(color::LightGreen); +static WARN: Fg = Fg(color::Yellow); +static RESET: Fg = Fg(color::Reset); + pub fn apply(targets: &Vec>) { let home = var("HOME").unwrap(); println!("{}running apply{}", style::Bold, style::Reset); @@ -26,18 +31,18 @@ pub fn apply(targets: &Vec>) { if path.is_none() { if i_name.is_some() { let name = i_name.unwrap().as_str().unwrap(); - println!(" \"{name}\" is missing its path property; skipping"); - } else { println!(" skipping empty target"); } + println!(" {WARN}\"{name}\" is missing its path property; skipping{RESET}"); + } else { println!(" {WARN}skipping empty target{RESET}"); } continue; } if i_name.is_none() { - println!(" target missing name; skipping"); + println!(" {WARN}target missing name; skipping{RESET}"); continue; } // print applying text let name = i_name.unwrap().as_str().unwrap(); - println!(" applying \"{name}\":"); + println!(" applying \"{name}\":"); // set up paths let destination = Path::new(path.unwrap().as_str().unwrap()); @@ -46,8 +51,8 @@ pub fn apply(targets: &Vec>) { // copy and print let result = fs::copy(source, destination); - if result.is_err() { println!(" failed to copy!"); } - else { println!(" completed successfully"); } + if result.is_err() { println!(" {FAIL}failed to copy!{RESET}"); } + else { println!(" {SUCCESS}completed successfully{RESET}"); } } } @@ -59,13 +64,13 @@ pub fn build(targets: &Vec>, tera: &mut Tera, context: &Conte let i_name = target.get("name"); // handle empty names gracefully if i_name.is_none() { - println!(" {}target missing name; skipping{}", Fg(color::Yellow), Fg(color::Reset)); + println!(" {WARN}target missing name; skipping{RESET}"); continue; } // print building text let name = i_name.unwrap().as_str().unwrap(); - println!(" building \"{name}\":"); + println!(" building \"{name}\":"); // render template let render = tera.render(name, context); @@ -74,7 +79,7 @@ pub fn build(targets: &Vec>, tera: &mut Tera, context: &Conte let error = render.err().unwrap(); let message = error.source().unwrap(); - println!(" failed to render template:\n {message}"); + println!(" {FAIL}failed to render template:\n {message}{RESET}"); continue; } @@ -84,14 +89,14 @@ pub fn build(targets: &Vec>, tera: &mut Tera, context: &Conte let path = Path::new(&destination); let i_file = File::create(path); if i_file.is_err() { - println!(" failed to create destination file at {path:?}"); + println!(" {FAIL}failed to create destination file at {path:?}{RESET}"); continue; } let mut file = i_file.unwrap(); // write to destination file let written = write!(&mut file, "{output}"); - if written.is_err() { println!(" failed to write to destination file at {path:?}"); } - else { println!(" completed successfully"); } + if written.is_err() { println!(" {FAIL}failed to write to destination file at {path:?}{RESET}"); } + else { println!(" {SUCCESS}completed successfully{RESET}"); } } }