Skip to content

Commit

Permalink
feat: add custom keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Dec 12, 2023
1 parent 80a5d77 commit 1b6f265
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use material_color_utilities_rs::{
scheme::{scheme::Scheme, scheme_android::SchemeAndroid},
};

use std::collections::HashMap;

use clap::{Parser, ValueEnum};
use color_eyre::{eyre::Result, Report};
use log::LevelFilter;
Expand Down Expand Up @@ -54,17 +56,17 @@ fn main() -> Result<(), Report> {
} else {
LevelFilter::Warn
};

setup_logging(log_level)?;

check_version();

let source_color = get_source_color(&args.source)?;

let mut palette: CorePalette = generate_palette(&args.palette.unwrap(), source_color)?;

let config: ConfigFile = ConfigFile::read(&args)?;

let default_scheme = args
.mode
.expect("Something went wrong while parsing the mode");
Expand All @@ -79,7 +81,7 @@ fn main() -> Result<(), Report> {
};

if args.dry_run == Some(false) {
Template::generate(&schemes, &config.templates, &args.source, &config.config.prefix, &source_color, &default_scheme)?;
Template::generate(&schemes, &config.templates, &args.source, &config.config.prefix, &source_color, &default_scheme, config.config.custom_keywords)?;

if config.config.reload_apps == Some(true) {
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
Expand Down
7 changes: 7 additions & 0 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ pub struct Config {
pub swww_options: Option<Vec<String>>,
pub feh_options: Option<Vec<String>>,
pub prefix: Option<String>,
pub custom_keywords: Option<HashMap<String, CustomKeyword>>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct CustomKeyword {
pub find: String,
pub replace: String,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down
12 changes: 11 additions & 1 deletion src/util/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::collections::HashMap;
use crate::util::arguments::Source;
use crate::util::color::SchemeExt;
use crate::Scheme;
use crate::util::config::CustomKeyword;

use crate::util::color::SchemeAndroidExt;
use crate::SchemeAndroid;
Expand Down Expand Up @@ -144,6 +145,7 @@ impl Template {
prefix: &Option<String>,
source_color: &[u8; 4],
default_scheme: &SchemesEnum,
custom_keywords: Option<HashMap<String, CustomKeyword>>,
) -> Result<(), Report> {
let default_prefix = "@".to_string();

Expand All @@ -164,6 +166,14 @@ impl Template {

let colors: Colors = generate_colors(schemes, source_color, default_scheme)?;

let mut custom: HashMap<String, String> = Default::default();

for entry in custom_keywords.iter() {
for (_name, values) in entry {
custom.insert(values.find.to_string(), values.replace.to_string());
}
}

for (i, (name, template)) in templates.iter().enumerate() {
let input_path_absolute = template.input_path.try_resolve()?;
let output_path_absolute = template.output_path.try_resolve()?;
Expand Down Expand Up @@ -215,7 +225,7 @@ impl Template {
}

data = engine.template(name)
.render(upon::value!{ colors: &colors, image: image })
.render(upon::value!{ colors: &colors, image: image, custom: &custom, })
.to_string()?;

output_file.write_all(data.as_bytes())?;
Expand Down

0 comments on commit 1b6f265

Please sign in to comment.