diff --git a/Cargo.lock b/Cargo.lock index 3ced01f..4f8fcf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -639,6 +639,43 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "execute" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a82608ee96ce76aeab659e9b8d3c2b787bffd223199af88c674923d861ada10" +dependencies = [ + "execute-command-macro", + "execute-command-tokens", + "generic-array", +] + +[[package]] +name = "execute-command-macro" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90dec53d547564e911dc4ff3ecb726a64cf41a6fa01a2370ebc0d95175dd08bd" +dependencies = [ + "execute-command-macro-impl", +] + +[[package]] +name = "execute-command-macro-impl" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce8cd46a041ad005ab9c71263f9a0ff5b529eac0fe4cc9b4a20f4f0765d8cf4b" +dependencies = [ + "execute-command-tokens", + "quote", + "syn", +] + +[[package]] +name = "execute-command-tokens" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69dc321eb6be977f44674620ca3aa21703cb20ffbe560e1ae97da08401ffbcad" + [[package]] name = "exr" version = "1.71.0" @@ -765,6 +802,15 @@ dependencies = [ "slab", ] +[[package]] +name = "generic-array" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe739944a5406424e080edccb6add95685130b9f160d5407c639c7df0c5836b0" +dependencies = [ + "typenum", +] + [[package]] name = "getrandom" version = "0.2.11" @@ -1204,6 +1250,7 @@ dependencies = [ "colorsys", "directories", "enquote", + "execute", "image 0.24.9", "indexmap", "log", @@ -2195,6 +2242,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-bidi" version = "0.3.14" diff --git a/Cargo.toml b/Cargo.toml index 08138f6..8a90e03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,3 +47,4 @@ serde_json = "1.0.107" toml = "0.8.8" material-colors = { version = "0.3.2", features = ["image"] } regex = "1.10.5" +execute = "0.2.13" diff --git a/example/config.toml b/example/config.toml index 9e54ab5..545a7bf 100644 --- a/example/config.toml +++ b/example/config.toml @@ -7,7 +7,6 @@ swww_options = [ "--transition-type", "center", ] -default_value = "-" [config.custom_keywords] test = "aaaa" @@ -31,7 +30,7 @@ colors_to_compare = [ { name = "purple", color = "#800080" }, ] compare_to = "#aefbd5" -hook = "source color {source_color}, source image {source_image}, compared color {compared_color}" +hook = 'echo "source color {{colors.source_color.default.hex}}, source image {{image}}, compared color {{compared_color}}"' [config.custom_colors] green = "#00FF00" diff --git a/src/main.rs b/src/main.rs index 537fa54..332af75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -142,7 +142,6 @@ fn main() -> Result<(), Report> { &default_scheme, &config.config.custom_keywords, &args.prefix, - &config.config.default_value, )?; if config.config.reload_apps == Some(true) { diff --git a/src/util/config.rs b/src/util/config.rs index 3970a49..3fe1e16 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -59,7 +59,6 @@ pub struct Config { pub prefix: Option, pub custom_keywords: Option>, pub custom_colors: Option>, - pub default_value: Option } #[derive(Deserialize, Serialize, Debug)] diff --git a/src/util/template.rs b/src/util/template.rs index 2f9ffe1..ec4e430 100644 --- a/src/util/template.rs +++ b/src/util/template.rs @@ -13,10 +13,14 @@ use upon::Value; use crate::util::color; use crate::util::color::color_to_string; use crate::util::filters::set_lightness; -use crate::util::variables::replace_hook_keywords; +use crate::util::variables::format_hook_text; use std::str; +use std::process::{Command, Stdio}; + +use execute::{Execute, shell}; + use std::collections::HashMap; use std::fs::create_dir_all; use std::fs::read_to_string; @@ -110,7 +114,6 @@ impl Template { default_scheme: &SchemesEnum, custom_keywords: &Option>, path_prefix: &Option, - default_fill_value: &Option, ) -> Result<(), Report> { let default_prefix = "@".to_string(); @@ -147,13 +150,12 @@ impl Template { } } - let render_data = upon::value! { + let mut render_data = upon::value! { colors: &colors, image: image, custom: &custom, }; - let fill = String::from("-"); - let default_fill_value = default_fill_value.as_ref().unwrap_or(&fill); - + + // let default_fill_value = String::from("-"); // debug!("render_data: {:#?}", &render_data); for (i, (name, template)) in templates.iter().enumerate() { @@ -171,21 +173,36 @@ impl Template { None }; - let parsed = replace_hook_keywords( - &template.hook.as_ref().unwrap(), - &default_fill_value, - image, - compared_color.as_ref(), - source_color, - ); - println!("{}", parsed); - } + // let parsed = replace_hook_keywords( + // &template.hook.as_ref().unwrap(), + // &default_fill_value, + // image, + // compared_color.as_ref(), + // source_color, + // ); + + if template.colors_to_compare.is_some() && template.compare_to.is_some() { + color::color_to_string( + &template.colors_to_compare.as_ref().unwrap(), + &template.compare_to.as_ref().unwrap(), + ); + let t = engine.compile(template.hook.as_ref().unwrap())?; + let res = format_hook_text(&mut render_data, compared_color.as_ref(), t); + let mut command = shell(&res); - if template.colors_to_compare.is_some() && template.compare_to.is_some() { - color::color_to_string( - &template.colors_to_compare.as_ref().unwrap(), - &template.compare_to.as_ref().unwrap(), - ); + command.stdout(Stdio::inherit()); + + let output = command.execute_output()?; + + + if let Some(exit_code) = output.status.code() { + if exit_code != 0 { + error!("Failed executing command: {:?}", &res) + } + } else { + eprintln!("Interrupted!"); + } + } } if !input_path_absolute.exists() { diff --git a/src/util/variables.rs b/src/util/variables.rs index e7f02fe..fe8d718 100644 --- a/src/util/variables.rs +++ b/src/util/variables.rs @@ -1,76 +1,95 @@ -use material_colors::color::Argb; -use regex::{Captures, Regex}; -use upon::Engine; +use upon::{Engine, Syntax, Template, Value}; -use super::color::{format_hex, rgb_from_argb}; +// use regex::{Captures, Regex}; +// use material_colors::color::Argb; +// use super::color::{format_hex, rgb_from_argb}; -pub enum Variables { - Invalid, - ComparedColor, - SourceImage, - SourceColor, -} +// pub enum Variables { +// Invalid, +// ComparedColor, +// SourceImage, +// SourceColor, +// } -impl Variables { - fn from(mut input: &str) -> Self { - if input.starts_with("{") && input.ends_with("}") { - input = input.remove_first_char().remove_last_char(); - } +// impl Variables { +// fn from(mut input: &str) -> Self { +// if input.starts_with("{") && input.ends_with("}") { +// input = input.remove_first_char().remove_last_char(); +// } - match input { - "compared_color" => Variables::ComparedColor, - "source_image" => Variables::SourceImage, - "source_color" => Variables::SourceColor, - _ => { - error!("Invalid variable: {{{}}}", input); - Variables::Invalid - } - } - } -} +// match input { +// "compared_color" => Variables::ComparedColor, +// "source_image" => Variables::SourceImage, +// "source_color" => Variables::SourceColor, +// _ => { +// error!("Invalid variable: {{{}}}", input); +// Variables::Invalid +// } +// } +// } +// } -trait StrExt { - fn remove_last_char(&self) -> &str; - fn remove_first_char(&self) -> &str; -} +// trait StrExt { +// fn remove_last_char(&self) -> &str; +// fn remove_first_char(&self) -> &str; +// } -impl StrExt for str { - fn remove_last_char(&self) -> &str { - match self.char_indices().next_back() { - Some((i, _)) => &self[..i], - None => self, - } - } - fn remove_first_char(&self) -> &str { - self.chars() - .next() - .map(|c| &self[c.len_utf8()..]) - .unwrap_or("") - } -} +// impl StrExt for str { +// fn remove_last_char(&self) -> &str { +// match self.char_indices().next_back() { +// Some((i, _)) => &self[..i], +// None => self, +// } +// } +// fn remove_first_char(&self) -> &str { +// self.chars() +// .next() +// .map(|c| &self[c.len_utf8()..]) +// .unwrap_or("") +// } +// } + +// pub fn replace_hook_keywords( +// input: &str, +// default_value: &String, +// src_img: Option<&String>, +// compared_color: Option<&String>, +// source_color: &Argb, +// ) -> String { +// let re = Regex::new(r"\{.*?\}").unwrap(); + +// let source_formatted = format_hex(&rgb_from_argb(*source_color)); -pub fn replace_hook_keywords( - input: &str, - default_value: &String, - src_img: Option<&String>, - compared_color: Option<&String>, - source_color: &Argb, -) -> String { - let re = Regex::new(r"\{.*?\}").unwrap(); +// let result = re.replace_all(input, |cap: &Captures| { +// match Variables::from(&cap[0]) { +// Variables::Invalid => &cap[0], +// Variables::ComparedColor => compared_color.unwrap_or(default_value), +// Variables::SourceImage => src_img.unwrap_or(default_value), +// Variables::SourceColor => &source_formatted, +// } +// .to_string() +// }); - let source_formatted = format_hex(&rgb_from_argb(*source_color)); +// return result.to_string(); +// } - let result = re.replace_all(input, |cap: &Captures| { - match Variables::from(&cap[0]) { - Variables::Invalid => &cap[0], - Variables::ComparedColor => compared_color.unwrap_or(default_value), - Variables::SourceImage => src_img.unwrap_or(default_value), - Variables::SourceColor => &source_formatted, +pub fn format_hook_text(render_data: &mut Value, compared_color: Option<&String>, template: Template<'_>) -> String { + let syntax = Syntax::builder().expr("{{", "}}").block("<*", "*>").build(); + let mut engine = Engine::with_syntax(syntax); + match render_data { + Value::Map(ref mut map) => { + if compared_color.is_some() { + map.insert("compared_color".to_string(), Value::from(compared_color.unwrap().as_str())); + } + }, + _ => { + println!("not") } - .to_string() - }); + } - return result.to_string(); -} + let data = template + .render(&engine,&render_data) + .to_string().unwrap(); -pub fn format_hook_text(mut engine: Engine) {} + return data +}