summaryrefslogtreecommitdiffstats
path: root/lass/5pkgs/fzfmenu/default.nix
blob: a158eeaac033a54dd522589c4c4599a949d2c6d2 (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
{ pkgs, ... }:

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/urxvtc \
      -name fzfmenu -title fzfmenu \
      -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"
''