fork/home-manager

git clone https://git.julia.blue/fork/home-manager

musjjlf: simplify option validation (#4334)406d34d9

master
4.6 KiB169 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let cfg = config.programs.lf;
6in {
7  meta.maintainers = [ hm.maintainers.owm111 ];
8
9  options = {
10    programs.lf = {
11      enable = mkEnableOption "lf";
12
13      package = mkOption {
14        type = types.package;
15        default = pkgs.lf;
16        defaultText = literalExpression "pkgs.lf";
17        description = ''
18          lf package to use.
19        '';
20      };
21
22      settings = mkOption {
23        type = with types;
24          attrsOf (oneOf [ str int (listOf (either str int)) bool ]);
25        default = { };
26        example = {
27          tabstop = 4;
28          number = true;
29          ratios = [ 1 1 2 ];
30        };
31        description = ''
32          An attribute set of lf settings. See the lf documentation for
33          detailed descriptions of these options. Prefer
34          {option}`programs.lf.previewer.*` for setting lf's {var}`previewer`
35          option. All string options are quoted with double quotes.
36        '';
37      };
38
39      commands = mkOption {
40        type = with types; attrsOf (nullOr str);
41        default = { };
42        example = {
43          get-mime-type = ''%xdg-mime query filetype "$f"'';
44          open = "$$OPENER $f";
45        };
46        description = ''
47          Commands to declare. Commands set to null or an empty string are
48          deleted.
49        '';
50      };
51
52      keybindings = mkOption {
53        type = with types; attrsOf (nullOr str);
54        default = { };
55        example = {
56          gh = "cd ~";
57          D = "trash";
58          i = "$less $f";
59          U = "!du -sh";
60          gg = null;
61        };
62        description =
63          "Keys to bind. Keys set to null or an empty string are deleted.";
64      };
65
66      cmdKeybindings = mkOption {
67        type = with types; attrsOf (nullOr str);
68        default = { };
69        example = literalExpression ''{ "<c-g>" = "cmd-escape"; }'';
70        description = ''
71          Keys to bind to command line commands which can only be one of the
72          builtin commands. Keys set to null or an empty string are deleted.
73        '';
74      };
75
76      previewer.source = mkOption {
77        type = with types; nullOr path;
78        default = null;
79        example = literalExpression ''
80          pkgs.writeShellScript "pv.sh" '''
81            #!/bin/sh
82
83            case "$1" in
84                *.tar*) tar tf "$1";;
85                *.zip) unzip -l "$1";;
86                *.rar) unrar l "$1";;
87                *.7z) 7z l "$1";;
88                *.pdf) pdftotext "$1" -;;
89                *) highlight -O ansi "$1" || cat "$1";;
90            esac
91          '''
92        '';
93        description = ''
94          Script or executable to use to preview files. Sets lf's
95          {var}`previewer` option.
96        '';
97      };
98
99      previewer.keybinding = mkOption {
100        type = with types; nullOr str;
101        default = null;
102        example = "i";
103        description = ''
104          Key to bind to the script at {var}`previewer.source` and
105          pipe through less. Setting to null will not bind any key.
106        '';
107      };
108
109      extraConfig = mkOption {
110        type = types.lines;
111        default = "";
112        example = ''
113          $mkdir -p ~/.trash
114        '';
115        description = "Custom lfrc lines.";
116      };
117    };
118  };
119
120  config = mkIf cfg.enable {
121    home.packages = [ cfg.package ];
122
123    xdg.configFile."lf/lfrc".text = let
124      fmtSetting = k: v:
125        optionalString (v != null) "set ${
126          if isBool v then
127            "${optionalString (!v) "no"}${k}"
128          else if isList v then
129            ''${k} "${concatStringsSep ":" (map (w: toString w) v)}"''
130          else
131            "${k} ${if isInt v then toString v else ''"${v}"''}"
132        }";
133
134      settingsStr = concatStringsSep "\n"
135        (remove "" (mapAttrsToList fmtSetting cfg.settings));
136
137      fmtCmdMap = before: k: v:
138        "${before} ${k}${optionalString (v != null && v != "") " ${v}"}";
139      fmtCmd = fmtCmdMap "cmd";
140      fmtMap = fmtCmdMap "map";
141      fmtCmap = fmtCmdMap "cmap";
142
143      commandsStr = concatStringsSep "\n" (mapAttrsToList fmtCmd cfg.commands);
144      keybindingsStr =
145        concatStringsSep "\n" (mapAttrsToList fmtMap cfg.keybindings);
146      cmdKeybindingsStr =
147        concatStringsSep "\n" (mapAttrsToList fmtCmap cfg.cmdKeybindings);
148
149      previewerStr = optionalString (cfg.previewer.source != null) ''
150        set previewer ${cfg.previewer.source}
151        ${optionalString (cfg.previewer.keybinding != null) ''
152          map ${cfg.previewer.keybinding} ''$${cfg.previewer.source} "$f" | less -R
153        ''}
154      '';
155    in ''
156      ${settingsStr}
157
158      ${commandsStr}
159
160      ${keybindingsStr}
161
162      ${cmdKeybindingsStr}
163
164      ${previewerStr}
165
166      ${cfg.extraConfig}
167    '';
168  };
169}