aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/programs/lf.nix
blob: ee4e9b5bfce24a209d770fb6d6c8ec8edf9b89ea (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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.programs.lf;

  knownSettings = {
    anchorfind = types.bool;
    color256 = types.bool;
    dircounts = types.bool;
    dirfirst = types.bool;
    drawbox = types.bool;
    globsearch = types.bool;
    icons = types.bool;
    hidden = types.bool;
    ignorecase = types.bool;
    ignoredia = types.bool;
    incsearch = types.bool;
    preview = types.bool;
    reverse = types.bool;
    smartcase = types.bool;
    smartdia = types.bool;
    wrapscan = types.bool;
    wrapscroll = types.bool;
    number = types.bool;
    relativenumber = types.bool;
    findlen = types.int;
    period = types.int;
    scrolloff = types.int;
    tabstop = types.int;
    errorfmt = types.str;
    filesep = types.str;
    ifs = types.str;
    promptfmt = types.str;
    shell = types.str;
    sortby = types.str;
    timefmt = types.str;
    ratios = types.str;
    info = types.str;
    shellopts = types.str;
  };

  lfSettingsType = types.submodule {
    options = let
      opt = name: type:
        mkOption {
          type = types.nullOr type;
          default = null;
          visible = false;
        };
    in mapAttrs opt knownSettings;
  };
in {
  meta.maintainers = [ hm.maintainers.owm111 ];

  options = {
    programs.lf = {
      enable = mkEnableOption "lf";

      settings = mkOption {
        type = lfSettingsType;
        default = { };
        example = {
          tabstop = 4;
          number = true;
          ratios = "1:1:2";
        };
        description = ''
          An attribute set of lf settings. The attribute names and corresponding
          values must be among the following supported options.

          <informaltable frame="none"><tgroup cols="1"><tbody>
          ${concatStringsSep "\n" (mapAttrsToList (n: v: ''
            <row>
              <entry><varname>${n}</varname></entry>
              <entry>${v.description}</entry>
            </row>
          '') knownSettings)}
          </tbody></tgroup></informaltable>

          See the lf documentation for detailed descriptions of these options.
          Note, use <varname>previewer</varname> to set lf's
          <varname>previewer</varname> option, and
          <varname>extraConfig</varname> for any other option not listed above.
          All string options are quoted with double quotes.
        '';
      };

      commands = mkOption {
        type = with types; attrsOf (nullOr str);
        default = { };
        example = {
          get-mime-type = ''%xdg-mime query filetype "$f"'';
          open = "$$OPENER $f";
        };
        description = ''
          Commands to declare. Commands set to null or an empty string are
          deleted.
        '';
      };

      keybindings = mkOption {
        type = with types; attrsOf (nullOr str);
        default = { };
        example = {
          gh = "cd ~";
          D = "trash";
          i = "$less $f";
          U = "!du -sh";
          gg = null;
        };
        description =
          "Keys to bind. Keys set to null or an empty string are deleted.";
      };

      cmdKeybindings = mkOption {
        type = with types; attrsOf (nullOr str);
        default = { };
        example = literalExample ''{ "<c-g>" = "cmd-escape"; }'';
        description = ''
          Keys to bind to command line commands which can only be one of the
          builtin commands. Keys set to null or an empty string are deleted.
        '';
      };

      previewer.source = mkOption {
        type = with types; nullOr path;
        default = null;
        example = literalExample ''
          pkgs.writeShellScript "pv.sh" '''
            #!/bin/sh

            case "$1" in
                *.tar*) tar tf "$1";;
                *.zip) unzip -l "$1";;
                *.rar) unrar l "$1";;
                *.7z) 7z l "$1";;
                *.pdf) pdftotext "$1" -;;
                *) highlight -O ansi "$1" || cat "$1";;
            esac
          '''
        '';
        description = ''
          Script or executable to use to preview files. Sets lf's
          <varname>previewer</varname> option.
        '';
      };

      previewer.keybinding = mkOption {
        type = with types; nullOr str;
        default = null;
        example = "i";
        description = ''
          Key to bind to the script at <varname>previewer.source</varname> and
          pipe through less. Setting to null will not bind any key.
        '';
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        example = ''
          $mkdir -p ~/.trash
        '';
        description = "Custom lfrc lines.";
      };
    };
  };

  config = mkIf cfg.enable {
    home.packages = [ pkgs.lf ];

    xdg.configFile."lf/lfrc".text = let
      fmtSetting = k: v:
        optionalString (v != null) "set ${
          if isBool v then
            "${optionalString (!v) "no"}${k}"
          else
            "${k} ${if isInt v then toString v else ''"${v}"''}"
        }";

      settingsStr = concatStringsSep "\n" (filter (x: x != "")
        (mapAttrsToList fmtSetting
          (builtins.intersectAttrs knownSettings cfg.settings)));

      fmtCmdMap = before: k: v:
        "${before} ${k}${optionalString (v != null && v != "") " ${v}"}";
      fmtCmd = fmtCmdMap "cmd";
      fmtMap = fmtCmdMap "map";
      fmtCmap = fmtCmdMap "cmap";

      commandsStr = concatStringsSep "\n" (mapAttrsToList fmtCmd cfg.commands);
      keybindingsStr =
        concatStringsSep "\n" (mapAttrsToList fmtMap cfg.keybindings);
      cmdKeybindingsStr =
        concatStringsSep "\n" (mapAttrsToList fmtCmap cfg.cmdKeybindings);

      previewerStr = optionalString (cfg.previewer.source != null) ''
        set previewer ${cfg.previewer.source}
        ${optionalString (cfg.previewer.keybinding != null) ''
          map ${cfg.previewer.keybinding} ''$${cfg.previewer.source} "$f" | less -R
        ''}
      '';
    in ''
      ${settingsStr}

      ${commandsStr}

      ${keybindingsStr}

      ${cmdKeybindingsStr}

      ${previewerStr}

      ${cfg.extraConfig}
    '';
  };
}