summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/fzfmenu/default.nix
blob: 1ee55a5ab379d7370a21320b9d54d88c635d2d2a (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
with import <stockholm/lib>;
{ pkgs, ... }@args:

let
  # config cannot be declared in the input attribute set because that would
  # cause callPackage to inject the wrong config.  Instead, get it from ...
  # via args.
  config = args.config or {};

  cfg = eval.config;

  eval = evalModules {
    modules = singleton {
      _file = toString ./profile.nix;
      imports = singleton config;
      options = {
        appName = mkOption {
          default = "fzfmenu";
          type = types.label;
        };
        windowTitle = mkOption {
          default = "fzfmenu";
          type = types.str;
        };
      };
    };
  };
in

pkgs.writeDashBin "fzfmenu" ''
  set -efu
  PROMPT=">"
  for i in "$@"
  do
  case $i in
      -p)
      PROMPT="$2"
      shift
      shift
      break
      ;;
      -l)
      # no reason to filter number of lines
      LINES="$2"
      shift
      shift
      break
      ;;
      -i)
      # we do this anyway
      shift
      break
      ;;
      *)
      echo "Unknown option $1" >&2
      shift
      ;;
  esac
  done
  INPUT=$(${pkgs.coreutils}/bin/cat)
  OUTPUT="$(${pkgs.coreutils}/bin/mktemp)"
  if [ -z ''${TERM+x} ]; then #check if we can print fzf in the shell
    ${pkgs.rxvt_unicode}/bin/urxvt \
      -name ${cfg.appName} \
      -title ${shell.escape cfg.windowTitle} \
      -e ${pkgs.dash}/bin/dash -c \
        "echo \"$INPUT\" | ${pkgs.fzf}/bin/fzf \
          --history=/dev/null \
          --print-query \
          --prompt=\"$PROMPT\" \
          --reverse \
          > \"$OUTPUT\"" 2>/dev/null
  else
    echo "$INPUT" | ${pkgs.fzf}/bin/fzf \
      --history=/dev/null \
      --print-query \
      --prompt="$PROMPT" \
      --reverse \
      > "$OUTPUT"
  fi
  ${pkgs.coreutils}/bin/tail -1 "$OUTPUT"
  ${pkgs.coreutils}/bin/rm "$OUTPUT"
''