Skip to content

Commit

Permalink
feat: decided to use "tera" at the end as template engine
Browse files Browse the repository at this point in the history
- Change dependency from `handlebars` to `tera` in Cargo.toml
- Remove unused shell variables and FZF colors configuration in `colors.sh`
- Replace `Handlebars` with `Tera` for template rendering in `templ.rs`
- Replace `BTreeMap` with `Context` for template data in `templ.rs`
- Use `tera.add_raw_template` instead of `handlebars.register_template_string` in `templ.rs`
- Use `tera.render` instead of `handlebars.render` to render the template in `templ.rs`
  • Loading branch information
bresilla committed Feb 7, 2024
1 parent 448e332 commit e022a22
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ notify = "4.0.15"
nix = "0.19.1"
tokio = { version= "1.0.1", features = ["full"] }
futures = "0.3.8"
handlebars = "3"
tera = "1.0"
fork = "0.1.18"
nom = "5"
29 changes: 13 additions & 16 deletions src/gen/templ.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
use crate::fun::text;
use crate::scheme::*;
use anyhow::Result;
use std::collections::BTreeMap;
use std::path::PathBuf;
use handlebars::Handlebars;
use tera::{Context, Tera};

fn generate_template(original: PathBuf, replaced: PathBuf, scheme: &Scheme) -> Result<()> {

let mut content = String::new();
if let Ok(cont) = text::file_to_string(original) {
content = cont;
}

let mut handlebars = Handlebars::new();
handlebars.register_template_string("template", &content)?;
let mut tera = Tera::default();
tera.add_raw_template("template", &content)?;

let mut data = BTreeMap::new();
let mut context = Context::new();

if let Some(colors) = scheme.colors() {
for (i, color) in colors.iter().enumerate() {
let name = format!("color{}", i);
data.insert(name, color.to_rgb_hex_string(false));
context.insert(name, &color.to_rgb_hex_string(false));
}
data.insert("background".to_string(), colors[0].to_rgb_hex_string(false));
data.insert("foreground".to_string(), colors[15].to_rgb_hex_string(false));
data.insert("cursor".to_string(), colors[1].to_rgb_hex_string(false));
data.insert("accent".to_string(), colors[1].to_rgb_hex_string(false));
context.insert("background", &colors[0].to_rgb_hex_string(false));
context.insert("foreground", &colors[15].to_rgb_hex_string(false));
context.insert("cursor", &colors[1].to_rgb_hex_string(false));
context.insert("accent", &colors[1].to_rgb_hex_string(false));
}

if let Some(wallpaper) = scheme.image() {
data.insert("wallpaper".to_string(), wallpaper.clone());
context.insert("wallpaper", &wallpaper);
}
if let Some(theme) = scheme.theme() {
data.insert("theme".to_string(), theme.clone());
context.insert("theme", &theme);
}

let new_content = handlebars.render("template", &data)?;

let new_content = tera.render("template", &context)?;
text::write_to_file(replaced, new_content.as_bytes());

Ok(())
Expand All @@ -49,7 +46,7 @@ pub fn pattern_generation(scheme: &mut Scheme) -> Result<()> {
generate_template(PathBuf::from(&p.0), PathBuf::from(&p.1), scheme)?;
println!("generating :{} into: {}", p.0, p.1)
} else {
//TODO: better error handle
// TODO: better error handle
println!("{} or {} is not a valid file", p.0, p.1)
}
}
Expand Down
10 changes: 1 addition & 9 deletions templates/colors.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/sh
# Shell variables
# Generated by 'wal'

wallpaper="#{{ wallpaper }}"

# Special
Expand All @@ -25,10 +24,3 @@ color12='#{{ color12 }}'
color13='#{{ color13 }}'
color14='#{{ color14 }}'
color15='#{{ color15 }}'

# FZF colors
export FZF_DEFAULT_OPTS="
$FZF_DEFAULT_OPTS
--color fg:7,bg:0,hl:1,fg+:232,bg+:1,hl+:255
--color info:7,prompt:2,spinner:1,pointer:232,marker:1
"

0 comments on commit e022a22

Please sign in to comment.