From 1d09b15b4fa63b16e2a63c47c835c87ecc78ee38 Mon Sep 17 00:00:00 2001 From: InioX Date: Mon, 22 Jul 2024 20:09:08 +0200 Subject: [PATCH] fix: make hooks not depend on `colors_to_compare` (#93) --- src/util/template.rs | 36 ++++++++++++++++++++---------------- src/util/variables.rs | 9 ++++----- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/util/template.rs b/src/util/template.rs index 970e030..cae13ba 100644 --- a/src/util/template.rs +++ b/src/util/template.rs @@ -325,25 +325,29 @@ fn format_hook( } else { None }; - Ok( - if template.colors_to_compare.is_some() && template.compare_to.is_some() { - let t = engine.compile(template.hook.as_ref().unwrap())?; - let res = format_hook_text(render_data, closest_color.as_ref(), t); - let mut command = shell(&res); - command.stdout(Stdio::inherit()); + let t = engine.compile(template.hook.as_ref().unwrap())?; + let res = if template.colors_to_compare.is_some() && template.compare_to.is_some() { + format_hook_text(render_data, closest_color, t) + } else { + format_hook_text(render_data, None, t) + }; + + let mut command = shell(&res); - let output = command.execute_output()?; + command.stdout(Stdio::inherit()); - if let Some(exit_code) = output.status.code() { - if exit_code != 0 { - error!("Failed executing command: {:?}", &res) - } - } else { - eprintln!("Interrupted!"); - } - }, - ) + 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!"); + } + + Ok(()) } fn generate_colors( diff --git a/src/util/variables.rs b/src/util/variables.rs index 53bfc9f..3efe64f 100644 --- a/src/util/variables.rs +++ b/src/util/variables.rs @@ -73,17 +73,16 @@ use upon::{Engine, Syntax, Template, Value}; // return result.to_string(); // } -pub fn format_hook_text(render_data: &mut Value, closest_color: Option<&String>, template: Template<'_>) -> String { +pub fn format_hook_text(render_data: &mut Value, closest_color: Option, 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 closest_color.is_some() { - map.insert("closest_color".to_string(), Value::from(closest_color.unwrap().as_str())); - } + map.insert("closest_color".to_string(), Value::from(closest_color)); }, _ => { - println!("not") + debug!("not map") } }