aboutsummaryrefslogtreecommitdiffstats
path: root/modules/colors/base16/default.nix
blob: 982113e6ea5653e2d4e49408089941b9bf6b5411 (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
  availableColorSchemeFiles = {
    "onedark-dark" = ./onedark-dark.nix;
  };

  selectedColorSchemeFile =
    availableColorSchemeFiles.${config.colors.base16}
      or (throw "Unknown color scheme: ${config.colors.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
    );
}