added ANSI formatting for better output readability

This commit is contained in:
Valerie Wolfe 2023-08-26 00:00:55 -04:00
parent 4f61060109
commit 3f6e996c64
3 changed files with 32 additions and 15 deletions

View file

@ -1,13 +1,13 @@
[package] [package]
name = "oink" name = "oink"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
#copy_dir = "0.1.3"
pico-args = "0.5.0" pico-args = "0.5.0"
termion = "2.0.1"
toml = "0.7.6" toml = "0.7.6"
upon = "0.7.1" upon = "0.7.1"

View file

@ -41,5 +41,6 @@ path = "/home/test/.config/oink/oink.toml"
## Libraries ## Libraries
- [pico-args](https://crates.io/crates/pico-args) — argument parsing - [pico-args](https://crates.io/crates/pico-args) — argument parsing
- [termion](https://crates.io/crates/termion) — ANSI formatting
- [toml](https://crates.io/crates/toml) — configuration parsing - [toml](https://crates.io/crates/toml) — configuration parsing
- [upon](https://crates.io/crates/upon) — template engine - [upon](https://crates.io/crates/upon) — template engine

View file

@ -6,11 +6,23 @@ use std::{
path::{ Path, PathBuf } path::{ Path, PathBuf }
}; };
use termion::{
color::{ self, Fg },
style::{
Bold as BOLD,
Italic as ITALIC,
Reset as RESET
}
};
use toml::{ map::Map, Value }; use toml::{ map::Map, Value };
use upon::Engine; use upon::Engine;
use crate::config::Config; use crate::config::Config;
static SUCCESS: Fg<color::Green> = Fg(color::Green);
static WARNING: Fg<color::Yellow> = Fg(color::Yellow);
static FAILURE: Fg<color::Red> = Fg(color::Red);
pub fn apply(targets: &Vec<Map<String, Value>>) { pub fn apply(targets: &Vec<Map<String, Value>>) {
let home = var("HOME").unwrap(); let home = var("HOME").unwrap();
println!("running apply:"); println!("running apply:");
@ -23,12 +35,12 @@ pub fn apply(targets: &Vec<Map<String, Value>>) {
if path.is_none() { if path.is_none() {
if i_name.is_some() { if i_name.is_some() {
let name = i_name.unwrap().as_str().unwrap(); let name = i_name.unwrap().as_str().unwrap();
println!(" \"{name}\" is missing its path property; skipping"); println!(" {WARNING}\"{name}\" is missing its path property; skipping{RESET}");
} else { println!(" skipping empty target"); } } else { println!(" {WARNING}skipping empty target{RESET}"); }
continue; continue;
} }
if i_name.is_none() { if i_name.is_none() {
println!(" target missing name; skipping"); println!(" {WARNING}target missing name; skipping{RESET}");
continue; continue;
} }
@ -43,8 +55,12 @@ pub fn apply(targets: &Vec<Map<String, Value>>) {
// copy and print // copy and print
let result = fs::copy(source, destination); let result = fs::copy(source, destination);
if result.is_err() { println!(" failed to copy!"); } if result.is_err() {
else { println!(" completed successfully"); } println!(" {BOLD}{FAILURE}failed to copy{RESET}");
}
else {
println!(" {BOLD}{SUCCESS}completed{RESET}");
}
} }
} }
@ -60,7 +76,7 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
let i_name = target.get("name"); let i_name = target.get("name");
// handle empty names gracefully // handle empty names gracefully
if i_name.is_none() { if i_name.is_none() {
println!(" target missing name; skipping"); println!(" {WARNING}target missing name; skipping{RESET}");
continue; continue;
} }
@ -69,23 +85,23 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
println!(" building \"{name}\":"); println!(" building \"{name}\":");
// compile // compile
println!(" compiling"); println!(" {ITALIC}compiling{RESET}");
let mut path = PathBuf::from(&template_dir); let mut path = PathBuf::from(&template_dir);
path.push(name); path.push(name);
let content = read_to_string(path).unwrap(); let content = read_to_string(path).unwrap();
let template = engine.compile(&content); let template = engine.compile(&content);
if template.is_err() { if template.is_err() {
let error = template.err().unwrap(); let error = template.err().unwrap();
println!(" failed to compile template:\n {error}\n skipping"); println!(" {BOLD}{FAILURE}failed to compile template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}");
continue; continue;
} }
// render // render
println!(" rendering"); println!(" {ITALIC}rendering{RESET}");
let render = template.unwrap().render(&context).to_string(); let render = template.unwrap().render(&context).to_string();
if render.is_err() { if render.is_err() {
let error = render.err().unwrap(); let error = render.err().unwrap();
println!(" failed to render template:\n {error}\n skipping"); println!(" {BOLD}{FAILURE}failed to render template:{RESET}\n {FAILURE}{error}\n {BOLD}skipping{RESET}");
continue; continue;
} }
@ -95,14 +111,14 @@ pub fn build(targets: &Vec<Map<String, Value>>, template_dir: String, config: &C
let path = Path::new(&destination); let path = Path::new(&destination);
let i_file = File::create(path); let i_file = File::create(path);
if i_file.is_err() { if i_file.is_err() {
println!(" failed to create destination file at {path:?}"); println!(" {BOLD}{FAILURE}failed to create destination file at {path:?}{RESET}");
continue; continue;
} }
let mut file = i_file.unwrap(); let mut file = i_file.unwrap();
// write to destination file // write to destination file
let written = write!(&mut file, "{output}"); let written = write!(&mut file, "{output}");
if written.is_err() { println!(" failed to write to destination file at {path:?}"); } if written.is_err() { println!(" {FAILURE}failed to write to destination file at {path:?}{RESET}"); }
else { println!(" completed"); } else { println!(" {BOLD}{SUCCESS}completed{RESET}"); }
} }
} }