summaryrefslogtreecommitdiff
path: root/modules/common/fzfExec.nix
blob: 2de3741133e8bd74cd9fde0ba4e714566d1b2204 (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
{
  tsunamiLib,
  lib,
  pkgs,
  config,
  ...
}: let
  inherit (tsunamiLib) scriptPath;
  cfg = config.tsunami.fzfExec;
in {
  options = {
    tsunami.fzfExec = {
      enable = lib.mkOption {
        type = lib.types.bool;
        default = true;
      };
    };
  };

  config = lib.mkIf cfg.enable {
    tsunami.scripts = {
      # this is buggy af; it only works half the time.
      # But it's the only way I know of to get docs on a specific tmux command
      "tmux-doc" = ''
        usage=$(tmux list-commands -F "#{command_list_name} #{command_list_usage}" "$1")

        man tmux | ${pkgs.gawk}/bin/awk -v usage="$usage" '
          index($0, usage) > 0 { found=1 }
          found {
            print
            if ($0 == "") exit
          }
        '
      '';

      "fzf-exec" = ''
        all_cmds() {
          tmux list-commands -F $'#{command_list_name}#{?command_list_alias,\n#{command_list_alias},}'
        }

        selected_cmd=$(all_cmds | ${pkgs.fzf}/bin/fzf --bind 'enter:accept-or-print-query,tab:replace-query,alt-backspace:clear-query' --prompt : --preview "~/.config/tmux/scripts/tmux-doc.sh $(printf '{}' | cut -d' ' -f1)")
        test -z "$selected_cmd" && exit

        tmux $(echo "$selected_cmd" | sed "s@~@$HOME@g")
      '';
    };

    tsunami.keys.global = [
      {
        key = "M-x";
        exec = "run-shell '${scriptPath "minibuffer"} -h 10 ${scriptPath "fzf-exec"}'";
      }
    ];
  };
}