From 15d9d2e8946a235c2c17ebb2048c20658a44cd68 Mon Sep 17 00:00:00 2001 From: Saiyed Shizain Date: Fri, 11 Sep 2026 18:42:23 +0530 Subject: feat: add matugen/noctalia v5 color template support - Add MatugenColors struct reading from ~/.cache/pomoru/colors.json using flat snake_case keys rendered by noctalia v5's template engine - Rename old struct to LegacyNoctaliaColors (kept for backward compat) - Implement priority chain: matugen cache > legacy colors.json > defaults - Fix parse_hex_color to use trim_start_matches instead of fixed-length check, accepting both '#RRGGBB' and 'RRGGBB' formats - Add assets/noctalia/colors-template.json: noctalia template file using {{colors..default.hex}} token syntax - Add assets/noctalia/templates.toml: drop-in user-template config snippet for ~/.config/noctalia/templates.toml --- assets/noctalia/colors-template.json | 6 ++++ assets/noctalia/templates.toml | 28 +++++++++++++++++ src/pomo/state.rs | 60 +++++++++++++++++++++++++++++++----- 3 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 assets/noctalia/colors-template.json create mode 100644 assets/noctalia/templates.toml diff --git a/assets/noctalia/colors-template.json b/assets/noctalia/colors-template.json new file mode 100644 index 0000000..d70e6ff --- /dev/null +++ b/assets/noctalia/colors-template.json @@ -0,0 +1,6 @@ +{ + "primary": "{{colors.primary.default.hex}}", + "on_surface_variant": "{{colors.on_surface_variant.default.hex}}", + "surface_container": "{{colors.surface_container.default.hex}}", + "on_surface": "{{colors.on_surface.default.hex}}" +} diff --git a/assets/noctalia/templates.toml b/assets/noctalia/templates.toml new file mode 100644 index 0000000..51d3f41 --- /dev/null +++ b/assets/noctalia/templates.toml @@ -0,0 +1,28 @@ +# Noctalia v5 user-template for pomoru +# +# This template tells noctalia to render pomoru's theme colors whenever +# the palette changes, so pomoru always matches your wallpaper-derived scheme. +# +# SETUP +# ----- +# Option A – Drop this file into your noctalia config directory: +# +# cp ~/.config/noctalia/templates.toml ~/.config/noctalia/templates.toml.bak # optional backup +# cat assets/noctalia/templates.toml >> ~/.config/noctalia/templates.toml +# +# Option B – Add the block below to ~/.config/noctalia/templates.toml manually. +# +# After adding it, noctalia will render the colors on the next palette change, +# or immediately via: noctalia theme --render-templates + +[theme.templates.user.pomoru] +input_path = "$XDG_CONFIG_HOME/noctalia/templates/pomoru-colors.json" +output_path = "$XDG_CACHE_HOME/pomoru/colors.json" + +# TEMPLATE FILE +# ------------- +# Copy the template to the input_path above: +# +# mkdir -p ~/.config/noctalia/templates +# cp assets/noctalia/colors-template.json \ +# ~/.config/noctalia/templates/pomoru-colors.json diff --git a/src/pomo/state.rs b/src/pomo/state.rs index 83e2038..bf7daef 100644 --- a/src/pomo/state.rs +++ b/src/pomo/state.rs @@ -12,9 +12,21 @@ pub struct Theme { pub text: Color, } +/// Color format rendered by noctalia v5's matugen template system. +/// Pomoru reads this from `~/.cache/pomoru/colors.json` when available. +#[derive(Deserialize)] +struct MatugenColors { + primary: String, + on_surface_variant: String, + surface_container: String, + on_surface: String, +} + +/// Legacy color format from `~/.config/noctalia/colors.json` (noctalia < v5). +/// Kept for backward compatibility. #[derive(Deserialize)] #[serde(rename_all = "camelCase")] -struct NoctaliaColors { +struct LegacyNoctaliaColors { m_primary: String, m_on_surface_variant: String, m_surface_variant: String, @@ -22,10 +34,11 @@ struct NoctaliaColors { } fn parse_hex_color(hex: &str) -> Option { - if hex.len() == 7 && hex.starts_with('#') { - let r = u8::from_str_radix(&hex[1..3], 16).ok()?; - let g = u8::from_str_radix(&hex[3..5], 16).ok()?; - let b = u8::from_str_radix(&hex[5..7], 16).ok()?; + let hex = hex.trim_start_matches('#'); + if hex.len() == 6 { + let r = u8::from_str_radix(&hex[0..2], 16).ok()?; + let g = u8::from_str_radix(&hex[2..4], 16).ok()?; + let b = u8::from_str_radix(&hex[4..6], 16).ok()?; Some(Color::Rgb(r, g, b)) } else { None @@ -34,6 +47,7 @@ fn parse_hex_color(hex: &str) -> Option { impl Default for Theme { fn default() -> Self { + // Catppuccin Mocha defaults — used when no theme file is present. let mut theme = Self { primary: Color::Rgb(180, 190, 254), overlay0: Color::Rgb(108, 112, 134), @@ -42,9 +56,38 @@ impl Default for Theme { }; if let Some(base_dirs) = BaseDirs::new() { - let path = base_dirs.home_dir().join(".config/noctalia/colors.json"); - if let Ok(content) = fs::read_to_string(&path) - && let Ok(colors) = serde_json::from_str::(&content) + // --- Priority 1: matugen / noctalia v5 rendered colors --- + // Noctalia renders `~/.cache/pomoru/colors.json` via a user template. + // See `assets/noctalia/` for the template and setup instructions. + let matugen_path = base_dirs + .home_dir() + .join(".cache/pomoru/colors.json"); + + if let Ok(content) = fs::read_to_string(&matugen_path) + && let Ok(colors) = serde_json::from_str::(&content) + { + if let Some(c) = parse_hex_color(&colors.primary) { + theme.primary = c; + } + if let Some(c) = parse_hex_color(&colors.on_surface_variant) { + theme.overlay0 = c; + } + if let Some(c) = parse_hex_color(&colors.surface_container) { + theme.surface0 = c; + } + if let Some(c) = parse_hex_color(&colors.on_surface) { + theme.text = c; + } + return theme; + } + + // --- Priority 2: legacy noctalia colors.json (noctalia < v5) --- + let legacy_path = base_dirs + .home_dir() + .join(".config/noctalia/colors.json"); + + if let Ok(content) = fs::read_to_string(&legacy_path) + && let Ok(colors) = serde_json::from_str::(&content) { if let Some(c) = parse_hex_color(&colors.m_primary) { theme.primary = c; @@ -60,6 +103,7 @@ impl Default for Theme { } } } + theme } } -- cgit v1.2.3