about summary refs log tree commit diff
path: root/modules/programs/foot/default.nix
blob: 5e10f7f89b2cb42310ad5a8578f182d9b6fce847 (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{ config, lib, pkgs, ... }:

let
  inherit (pkgs.vuizvui.sternenseemann.lib)
    mapAttrsByAttrs
    ;

  inherit (lib.generators)
    toINI
    toKeyValue
    ;

  cfg = config.vuizvui.programs.foot;

  # We don't allow null, since we use null as
  # a “fall back to foot's defaults” value for defined
  # options in the freeform module so no null may be
  # present in the resulting ini file.
  iniAtom = with lib.types; (oneOf [
    bool
    int
    float
    str
  ]) // {
    description = "INI atom (bool, int, float or string)";
  };

  # pkgs.formats.ini doesn't allow top-level bindings
  # without a section, so we have to wrap it a bit
  # TODO(sterni): multiple binds
  format = {
    type = with lib.types;
      (attrsOf (either iniAtom (attrsOf iniAtom))) // {
        description = ''
          attribute set of either top-level INI atoms (bool, int, float or string) or attribute sets (sections) of INI atoms
        '';
      };
    generate = name: value:
      let
        isSection = builtins.isAttrs;
        topLevel = lib.filterAttrs (_: v: !(isSection v)) value;
        sections = lib.filterAttrs (_: v: isSection v) value;
      in
        pkgs.writeText name ''
          ${toKeyValue {} topLevel}
          ${toINI {} sections}
        '';
  };

  prettyPrint = lib.generators.toPretty {};

  fontOptions = [
    "font"
    "font-bold"
    "font-italic"
    "font-bold-italic"
  ];

  exampleFontSet = {
    font = "Dina";
    options = {
      slant = "italic";
      weight = "bold";
    };
  };

  # check if the given set is formed like we expect it to be
  # and print a nice error message if not.
  wellFormedFontSet = set:
    let
      attrCount = builtins.length (builtins.attrNames set);
    in
      lib.assertMsg (set ? font && attrCount <= 2
          && (attrCount > 1 -> set ? options))
        "font set must be of the form ${prettyPrint exampleFontSet} where the options attr is optional.";

  # Convert a mixed list of font sets and strings into a comma
  # separated string list of properly rendered fontconfig font strings
  buildIniFontList = fonts:
    let
      formatOptions = opts: lib.concatStrings
        (lib.mapAttrsToList (o: v: ":${o}=${builtins.toString v}") opts);
      fontconfigFont = font:
        if builtins.isString font
        then font
        else assert (wellFormedFontSet font);
          "${font.font}${formatOptions (font.options or {})}";
    in
      lib.concatMapStringsSep "," fontconfigFont fonts;

  mkFontOption = name: lib.mkOption {
    type = with lib.types; nullOr (nonEmptyListOf (either str attrs));
    description = ''
      The first font specified is used as foot's
      primary ${name}, all further fonts are
      used as fallbacks in the specified order.
      Fonts must be either specified as strings
      in fontconfig syntax or using a special
      record syntax (see example).
    '';
    example = lib.literalExample (prettyPrint [
      {
        font = "Courier New";
        options = {
          size = 12;
        };
      }
      exampleFontSet
      "monospace"
    ]);
    default = null;
  };

  commandBindOptions = [
    "pipe-visible"
    "pipe-scrollback"
    "pipe-selected"
  ];

  exampleCommandBindSet = {
    cmd = "sh -c 'xurls | bemenu | xargs -r $BROWSER'";
    bind = "Control+Print";
  };

  wellformedCommandBindSet = set:
    lib.assertMsg (set ? cmd && set ? bind)
      "command bind set must contain a cmd and a bind attr: ${prettyPrint exampleCommandBindSet}";

  buildIniCommandBind = bind:
    if builtins.isString bind
    then bind
    else assert wellformedCommandBindSet bind;
      "[${bind.cmd}] ${bind.bind}";

  mkCommandBindOption = name:
    lib.mkOption {
      type = with lib.types; nullOr (either str (attrsOf str));
      description = ''
        Bind a key which pipes the ${lib.removePrefix "pipe-" name}
        region into the given command.
      '';
      example = lib.literalExample (prettyPrint exampleCommandBindSet);
      default = null;
    };

  # convert some fancy options we support to a format formats.ini can deal
  # with and remove all optional options (in this case options which default
  # to null), so we don't have to track upstreams defaults, but foot can
  # decide for itself while we still can treat some options specially.
  iniReady = settings:
    let
      withoutNulls =
        lib.filterAttrsRecursive (_: x: x != null) settings;
      attrTransforms =
        (lib.genAttrs fontOptions (n: (_: buildIniFontList))) // {
          key-bindings =
            lib.genAttrs commandBindOptions (n: (_: buildIniCommandBind));
        };
    in
      mapAttrsByAttrs withoutNulls attrTransforms;

in {
  options.vuizvui.programs.foot = {
    enable = lib.mkEnableOption "foot";

    settings = lib.mkOption {
      type = lib.types.submodule {
        freeformType = format.type;
        options = (lib.genAttrs fontOptions mkFontOption) // {
          key-bindings =
            lib.genAttrs commandBindOptions mkCommandBindOption;
        };
      };
      default = {};
      description = ''
        Configuration for foot. A list of all available
        options can be found in
        <citerefentry>
          <refentrytitle>foot</refentrytitle>
          <manvolnum>5</manvolnum>
        </citerefentry>
        or at <link xlink:href="https://codeberg.org/dnkl/foot/src/tag/${pkgs.foot.version}/foot.ini" />.
      '';
      example = lib.literalExample (prettyPrint {
        key-bindings = {
          scrollback-up-page = "Control+Shift+Page_Up";
          scrollback-down-page = "Control+Shift+Page_Down";
          search-start = "Control+Shift+F";
        };

        mouse-bindings = {
          primary-paste = "none";
        };
      });
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [ pkgs.foot pkgs.foot.terminfo ];

    environment.etc."xdg/foot/foot.ini".source =
      format.generate "foot.ini" (iniReady cfg.settings);
  };
}