Skip to content

Commit

Permalink
feat: update catppuccin to 2.x (stable) and handlebars to 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy committed Mar 5, 2024
1 parent f071aab commit 093f2b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

14 changes: 7 additions & 7 deletions whiskers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ name = "whiskers"
path = "src/main.rs"

[dependencies]
base64 = "0.21"
catppuccin = { version = "2.0.0-beta.3", features = ["css-colors"] }
indexmap = { version = "2.2.3", features = ["serde"] }
base64 = "0.22"
catppuccin = { version = "2.0", features = ["css-colors"] }
indexmap = { version = "2.2", features = ["serde"] }
clap = { version = "4.5", features = ["derive"] }
clap-stdin = "0.4.0"
clap-stdin = "0.4"
color-eyre = { version = "0.6", default-features = false }
css-colors = "1.0"
handlebars = "4.5"
handlebars = "5.1"
regex = "1.10"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
serde_yaml = "0.9"
tempfile = "3.10"
thiserror = "1.0"
titlecase = "2.2"
json-patch = "1.2.0"
json-patch = "1.2"

[dev-dependencies]
assert_cmd = "2.0"
predicates = "3.1.0"
predicates = "3.1"
60 changes: 33 additions & 27 deletions whiskers/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ use base64::Engine;
use css_colors::{Color, Ratio, HSLA, RGBA};
use handlebars::{
handlebars_helper, Context, Handlebars, Helper, HelperResult, Output, RenderContext,
RenderError,
RenderError, RenderErrorReason,
};

use ::titlecase::titlecase as titlecase_ext;
use serde_json::Value;

use crate::parse::ColorExt;

impl From<crate::parse::Error> for RenderError {
fn from(value: crate::parse::Error) -> Self {
Self::from_error("Failed to parse hex string", value)
fn hex_error(
helper: &'static str,
param: &'static str,
) -> impl FnOnce(crate::parse::Error) -> RenderError {
move |_| {
RenderErrorReason::ParamTypeMismatchForName(helper, param.to_string(), "hex".to_string())
.into()
}
}

Expand All @@ -21,64 +25,66 @@ handlebars_helper!(lowercase: |s: String| s.to_lowercase());
handlebars_helper!(titlecase: |s: String| titlecase_ext(&s));
handlebars_helper!(trunc: |number: f32, places: usize| format!("{number:.places$}"));
handlebars_helper!(lighten: |color: String, weight: f32| {
HSLA::from_hex(&color)?.lighten(Ratio::from_f32(weight)).to_hex()
HSLA::from_hex(&color).map_err(hex_error("lighten", "0"))?.lighten(Ratio::from_f32(weight)).to_hex()
});
handlebars_helper!(darken: |color: String, weight: f32| {
HSLA::from_hex(&color)?.darken(Ratio::from_f32(weight)).to_hex()
HSLA::from_hex(&color).map_err(hex_error("darken", "0"))?.darken(Ratio::from_f32(weight)).to_hex()
});
handlebars_helper!(mix: |color_a: String, color_b: String, t: f32| {
HSLA::from_hex(&color_a)?.mix(HSLA::from_hex(&color_b)?, Ratio::from_f32(t)).to_hex()
let a = HSLA::from_hex(&color_a).map_err(hex_error("mix", "0"))?;
let b = HSLA::from_hex(&color_b).map_err(hex_error("mix", "1"))?;
a.mix(b, Ratio::from_f32(t)).to_hex()
});
handlebars_helper!(opacity: |color: String, amount: f32| {
HSLA::from_hex(&color)?.fade(Ratio::from_f32(amount)).to_hex()
HSLA::from_hex(&color).map_err(hex_error("opacity", "0"))?.fade(Ratio::from_f32(amount)).to_hex()
});
handlebars_helper!(rgb: |color: String| {
RGBA::from_hex(&color)?.to_rgb().to_string()
RGBA::from_hex(&color).map_err(hex_error("rgb", "0"))?.to_rgb().to_string()
});
handlebars_helper!(rgba: |color: String| {
RGBA::from_hex(&color)?.to_string()
RGBA::from_hex(&color).map_err(hex_error("rgba", "0"))?.to_string()
});
handlebars_helper!(hsl: |color: String| {
HSLA::from_hex(&color)?.to_hsl().to_string()
HSLA::from_hex(&color).map_err(hex_error("hsl", "0"))?.to_hsl().to_string()
});
handlebars_helper!(hsla: |color: String| {
HSLA::from_hex(&color)?.to_string()
HSLA::from_hex(&color).map_err(hex_error("hsla", "0"))?.to_string()
});
handlebars_helper!(red_i: |color: String| {
RGBA::from_hex(&color)?.r.as_u8()
RGBA::from_hex(&color).map_err(hex_error("red_i", "0"))?.r.as_u8()
});
handlebars_helper!(green_i: |color: String| {
RGBA::from_hex(&color)?.g.as_u8()
RGBA::from_hex(&color).map_err(hex_error("green_i", "0"))?.g.as_u8()
});
handlebars_helper!(blue_i: |color: String| {
RGBA::from_hex(&color)?.b.as_u8()
RGBA::from_hex(&color).map_err(hex_error("blue_i", "0"))?.b.as_u8()
});
handlebars_helper!(alpha_i: |color: String| {
RGBA::from_hex(&color)?.a.as_u8()
RGBA::from_hex(&color).map_err(hex_error("alpha_i", "0"))?.a.as_u8()
});
handlebars_helper!(red_f: |color: String| {
RGBA::from_hex(&color)?.r.as_f32()
RGBA::from_hex(&color).map_err(hex_error("red_f", "0"))?.r.as_f32()
});
handlebars_helper!(green_f: |color: String| {
RGBA::from_hex(&color)?.g.as_f32()
RGBA::from_hex(&color).map_err(hex_error("green_f", "0"))?.g.as_f32()
});
handlebars_helper!(blue_f: |color: String| {
RGBA::from_hex(&color)?.b.as_f32()
RGBA::from_hex(&color).map_err(hex_error("blue_f", "0"))?.b.as_f32()
});
handlebars_helper!(alpha_f: |color: String| {
RGBA::from_hex(&color)?.a.as_f32()
RGBA::from_hex(&color).map_err(hex_error("alpha_f", "0"))?.a.as_f32()
});
handlebars_helper!(red_h: |color: String| {
format!("{:02x}", RGBA::from_hex(&color)?.r.as_u8())
format!("{:02x}", RGBA::from_hex(&color).map_err(hex_error("red_h", "0"))?.r.as_u8())
});
handlebars_helper!(green_h: |color: String| {
format!("{:02x}", RGBA::from_hex(&color)?.g.as_u8())
format!("{:02x}", RGBA::from_hex(&color).map_err(hex_error("green_h", "0"))?.g.as_u8())
});
handlebars_helper!(blue_h: |color: String| {
format!("{:02x}", RGBA::from_hex(&color)?.b.as_u8())
format!("{:02x}", RGBA::from_hex(&color).map_err(hex_error("blue_h", "0"))?.b.as_u8())
});
handlebars_helper!(alpha_h: |color: String| {
format!("{:02x}", RGBA::from_hex(&color)?.a.as_u8())
format!("{:02x}", RGBA::from_hex(&color).map_err(hex_error("alpha_h", "0"))?.a.as_u8())
});

pub fn darklight(
Expand All @@ -90,10 +96,10 @@ pub fn darklight(
) -> HelperResult {
let dark = h
.param(0)
.ok_or_else(|| RenderError::new("Missing parameter `dark` in position 0"))?;
.ok_or_else(|| RenderErrorReason::ParamNotFoundForIndex("darklight", 0))?;
let light = h
.param(1)
.ok_or_else(|| RenderError::new("Missing parameter `light` in position 1"))?;
.ok_or_else(|| RenderErrorReason::ParamNotFoundForIndex("darklight", 1))?;

if ctx.data()["flavor"] == "latte" {
out.write(&light.render())?;
Expand All @@ -105,7 +111,7 @@ pub fn darklight(
}

handlebars_helper!(unquote: |value: Value| {
let content = serde_json::to_string(&value)?;
let content = serde_json::to_string(&value).map_err(RenderErrorReason::SerdeError)?;
let content = base64::engine::general_purpose::STANDARD_NO_PAD.encode(content);
format!("{{WHISKERS:UNQUOTE:{content}}}")
});

0 comments on commit 093f2b6

Please sign in to comment.