aboutsummaryrefslogtreecommitdiffstats
path: root/modules/colors/base16/default.nix
blob: 3102821def9c3fd59981090b7042c713f69a1a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Shared base16 color scheme helpers. Not a module — pass the module arguments
# to it (`import <path> { inherit config lib pkgs; }`) to get
# `colorsWithHashPrefix` and a functor that renders a color template into a Nix
# store file.
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.colors;
  availableColorSchemeFiles = {
    "onedark-dark" = ./onedark-dark.nix;
  };

  selectedColorSchemeFile =
    availableColorSchemeFiles.${cfg.base16} or (throw "Unknown color scheme: ${cfg.base16}");

  selectedColorScheme = import selectedColorSchemeFile { };
  selectedColorSchemeColorNames = builtins.attrNames selectedColorScheme;

  renderColorSchemeTemplate =
    templateFileOrContent:
    let
      templateContent =
        if builtins.typeOf templateFileOrContent == "path" then
          builtins.readFile templateFileOrContent
        else
          templateFileOrContent;
    in
    builtins.replaceStrings (map (colorName: "@@${colorName}@@") selectedColorSchemeColorNames) (map (
      colorName: selectedColorScheme.${colorName}
    ) selectedColorSchemeColorNames) templateContent;
in
{
  colorsWithHashPrefix = lib.mapAttrs (_colorName: colorValue: "#${colorValue}") selectedColorScheme;

  __functor =
    _self:
    {
      templateFileOrContent,
      fileExtension ? "",
    }:
    pkgs.writeText "color-scheme-themed${fileExtension}" (
      renderColorSchemeTemplate templateFileOrContent
    );
}