From 0eeb9623b20354d6e07a2bc83d142c88e041d69f Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Sat, 19 Jul 2025 20:20:10 +0200 Subject: [PATCH] Modules/Color: Add a helper script to output the current colorscheme to the terminal --- home/modules/color/default.nix | 76 +++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/home/modules/color/default.nix b/home/modules/color/default.nix index 043e973c..fcb3e0db 100644 --- a/home/modules/color/default.nix +++ b/home/modules/color/default.nix @@ -9,67 +9,87 @@ in { options.modules.color = import ./options.nix {inherit lib mylib;}; - config = let - colorDefs = import ./schemes/${color.scheme}.nix; - - mkColorAssignment = defs: key: {${key} = defs.${key};}; - mkStringColorAssignment = defs: key: {${key} = "#${defs.${key}}";}; - mkRgbColorAssignment = defs: key: {${key} = mylib.color.hexToRGB defs.${key};}; - mkRgbStringColorAssignment = defs: key: {${key} = mylib.color.hexToRGBString "," defs.${key};}; - in { - # Helper script that processes a visual mode selection and replaces - # referenced colors in-place with their counterparts in this module. - # Usage: ":'<,'>!applyColors" + config = { home.packages = let mkPythonColorDef = name: value: " '${name}': '${value}',"; + # Helper script that processes a visual mode selection and replaces + # referenced colors in-place with their counterparts in this module. + # Usage: ":'<,'>!applyColors" applyColors = pkgs.writers.writePython3Bin "applyColors" { doCheck = false; } - ( - builtins.concatStringsSep "\n" [ - "colors: dict[str, str] = {" - (config.modules.color.hex - |> builtins.mapAttrs mkPythonColorDef - |> builtins.attrValues - |> builtins.concatStringsSep "\n") - "}" - (builtins.readFile ./applyColors.py) - ] - ); - in [applyColors]; + (builtins.concatStringsSep "\n" [ + "colors: dict[str, str] = {" + (color.hex + |> builtins.mapAttrs mkPythonColorDef + |> builtins.attrValues + |> builtins.concatStringsSep "\n") + "}" + (builtins.readFile ./applyColors.py) + ]); + + mkBashColorEcho = let + pastel = "${pkgs.pastel}/bin/pastel"; + in + name: value: ''printf "%-12s" " ${name}:" && ${pastel} color "${value}" | ${pastel} format hex''; + + # Helper script that prints the color scheme to the terminal + printNixColors = + pkgs.writeShellScriptBin + "printNixColors" + (builtins.concatStringsSep "\n" [ + ''echo " ${color.scheme}:"'' + ''echo ${lib.concatStrings (lib.replicate 20 "=")}'' + (color.hexS + |> builtins.mapAttrs mkBashColorEcho + |> builtins.attrValues + |> builtins.concatStringsSep "\n") + ''echo ${lib.concatStrings (lib.replicate 20 "=")}'' + ]); + in [ + applyColors + printNixColors + ]; # This module sets its own options to the values specified in a colorscheme file. - modules.color = { + modules.color = let + colorDefs = import ./schemes/${color.scheme}.nix; + + mkColorAssignment = key: {${key} = colorDefs.${key};}; + mkStringColorAssignment = key: {${key} = "#${colorDefs.${key}}";}; + mkRgbColorAssignment = key: {${key} = mylib.color.hexToRGB colorDefs.${key};}; + mkRgbStringColorAssignment = key: {${key} = mylib.color.hexToRGBString "," colorDefs.${key};}; + in { # RRGGBB (0-F) hex = colorDefs |> builtins.attrNames - |> builtins.map (mkColorAssignment colorDefs) + |> builtins.map mkColorAssignment |> lib.mergeAttrsList; # #RRGGBB (0-F) hexS = colorDefs |> builtins.attrNames - |> builtins.map (mkStringColorAssignment colorDefs) + |> builtins.map mkStringColorAssignment |> lib.mergeAttrsList; # [RR GG BB] (0-255) rgb = colorDefs |> builtins.attrNames - |> builtins.map (mkRgbColorAssignment colorDefs) + |> builtins.map mkRgbColorAssignment |> lib.mergeAttrsList; # RR,GG,BB (0-255) rgbS = colorDefs |> builtins.attrNames - |> builtins.map (mkRgbStringColorAssignment colorDefs) + |> builtins.map mkRgbStringColorAssignment |> lib.mergeAttrsList; }; };