fork/home-manager

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

r-vdpfzf: add mkOrder for fish like we do for other shells84fa81c7

master
5.7 KiB198 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.fzf;
8
9  renderedColors = colors:
10    concatStringsSep ","
11    (mapAttrsToList (name: value: "${name}:${value}") colors);
12
13in {
14  imports = [
15    (mkRemovedOptionModule [ "programs" "fzf" "historyWidgetCommand" ]
16      "This option is no longer supported by fzf.")
17  ];
18
19  options.programs.fzf = {
20    enable = mkEnableOption "fzf - a command-line fuzzy finder";
21
22    package = mkOption {
23      type = types.package;
24      default = pkgs.fzf;
25      defaultText = literalExpression "pkgs.fzf";
26      description = "Package providing the {command}`fzf` tool.";
27    };
28
29    defaultCommand = mkOption {
30      type = types.nullOr types.str;
31      default = null;
32      example = "fd --type f";
33      description = ''
34        The command that gets executed as the default source for fzf
35        when running.
36      '';
37    };
38
39    defaultOptions = mkOption {
40      type = types.listOf types.str;
41      default = [ ];
42      example = [ "--height 40%" "--border" ];
43      description = ''
44        Extra command line options given to fzf by default.
45      '';
46    };
47
48    fileWidgetCommand = mkOption {
49      type = types.nullOr types.str;
50      default = null;
51      example = "fd --type f";
52      description = ''
53        The command that gets executed as the source for fzf for the
54        CTRL-T keybinding.
55      '';
56    };
57
58    fileWidgetOptions = mkOption {
59      type = types.listOf types.str;
60      default = [ ];
61      example = [ "--preview 'head {}'" ];
62      description = ''
63        Command line options for the CTRL-T keybinding.
64      '';
65    };
66
67    changeDirWidgetCommand = mkOption {
68      type = types.nullOr types.str;
69      default = null;
70      example = "fd --type d";
71      description = ''
72        The command that gets executed as the source for fzf for the
73        ALT-C keybinding.
74      '';
75    };
76
77    changeDirWidgetOptions = mkOption {
78      type = types.listOf types.str;
79      default = [ ];
80      example = [ "--preview 'tree -C {} | head -200'" ];
81      description = ''
82        Command line options for the ALT-C keybinding.
83      '';
84    };
85
86    historyWidgetOptions = mkOption {
87      type = types.listOf types.str;
88      default = [ ];
89      example = [ "--sort" "--exact" ];
90      description = ''
91        Command line options for the CTRL-R keybinding.
92      '';
93    };
94
95    colors = mkOption {
96      type = types.attrsOf types.str;
97      default = { };
98      example = literalExpression ''
99        {
100          bg = "#1e1e1e";
101          "bg+" = "#1e1e1e";
102          fg = "#d4d4d4";
103          "fg+" = "#d4d4d4";
104        }
105      '';
106      description = ''
107        Color scheme options added to `FZF_DEFAULT_OPTS`. See
108        <https://github.com/junegunn/fzf/wiki/Color-schemes>
109        for documentation.
110      '';
111    };
112
113    tmux = {
114      enableShellIntegration = mkEnableOption ''
115        setting `FZF_TMUX=1` which causes shell integration to use fzf-tmux
116      '';
117
118      shellIntegrationOptions = mkOption {
119        type = types.listOf types.str;
120        default = [ ];
121        example = literalExpression ''[ "-d 40%" ]'';
122        description = ''
123          If {option}`programs.fzf.tmux.enableShellIntegration` is set to `true`,
124          shell integration will use these options for fzf-tmux.
125          See {command}`fzf-tmux --help` for available options.
126        '';
127      };
128    };
129
130    enableBashIntegration = mkOption {
131      default = true;
132      type = types.bool;
133      description = ''
134        Whether to enable Bash integration.
135      '';
136    };
137
138    enableZshIntegration = mkOption {
139      default = true;
140      type = types.bool;
141      description = ''
142        Whether to enable Zsh integration.
143      '';
144    };
145
146    enableFishIntegration = mkOption {
147      default = true;
148      type = types.bool;
149      description = ''
150        Whether to enable Fish integration.
151      '';
152    };
153  };
154
155  config = mkIf cfg.enable {
156    home.packages = [ cfg.package ];
157
158    home.sessionVariables = mapAttrs (n: v: toString v)
159      (filterAttrs (n: v: v != [ ] && v != null) {
160        FZF_ALT_C_COMMAND = cfg.changeDirWidgetCommand;
161        FZF_ALT_C_OPTS = cfg.changeDirWidgetOptions;
162        FZF_CTRL_R_OPTS = cfg.historyWidgetOptions;
163        FZF_CTRL_T_COMMAND = cfg.fileWidgetCommand;
164        FZF_CTRL_T_OPTS = cfg.fileWidgetOptions;
165        FZF_DEFAULT_COMMAND = cfg.defaultCommand;
166        FZF_DEFAULT_OPTS = cfg.defaultOptions
167          ++ lib.optionals (cfg.colors != { })
168          [ "--color ${renderedColors cfg.colors}" ];
169        FZF_TMUX = if cfg.tmux.enableShellIntegration then "1" else null;
170        FZF_TMUX_OPTS = cfg.tmux.shellIntegrationOptions;
171      });
172
173    # Note, since fzf unconditionally binds C-r we use `mkOrder` to make the
174    # initialization show up a bit earlier. This is to make initialization of
175    # other history managers, like mcfly or atuin, take precedence.
176    programs.bash.initExtra = mkIf cfg.enableBashIntegration (mkOrder 200 ''
177      if [[ :$SHELLOPTS: =~ :(vi|emacs): ]]; then
178        . ${cfg.package}/share/fzf/completion.bash
179        . ${cfg.package}/share/fzf/key-bindings.bash
180      fi
181    '');
182
183    # Note, since fzf unconditionally binds C-r we use `mkOrder` to make the
184    # initialization show up a bit earlier. This is to make initialization of
185    # other history managers, like mcfly or atuin, take precedence.
186    programs.zsh.initExtra = mkIf cfg.enableZshIntegration (mkOrder 200 ''
187      if [[ $options[zle] = on ]]; then
188        . ${cfg.package}/share/fzf/completion.zsh
189        . ${cfg.package}/share/fzf/key-bindings.zsh
190      fi
191    '');
192
193    programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration
194      (mkOrder 200 ''
195        source ${cfg.package}/share/fzf/key-bindings.fish && fzf_key_bindings
196      '');
197  };
198}