aboutsummaryrefslogtreecommitdiffstats
path: root/modules/scripts/fzf-launcher.sh
blob: 79853f1f895674ec6c9a609b3640111cc21b57b7 (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
DIRS=(
  /run/current-system/sw/share/applications
  "$HOME/.nix-profile/share/applications"
  "$HOME/.local/share/applications"
)

entries=""

for dir in "${DIRS[@]}"; do
  [[ -d "$dir" ]] || continue

  for f in "$dir"/*.desktop; do
    [[ -f "$f" ]] || continue

    grep -q "^NoDisplay=true" "$f" && continue
    grep -q "^Hidden=true" "$f" && continue
    grep -q "^Type=Application" "$f" || continue
    grep -q "^Terminal=true" "$f" && continue

    categories=$(grep -m1 "^Categories=" "$f" | cut -d= -f2-)
    if [[ -n "$categories" ]]; then
      echo "$categories" | grep -qE "(Settings|System|Screensaver|DesktopSettings|PackageManager)" && continue
    fi

    name=$(grep -m1 "^Name=" "$f" | cut -d= -f2-)
    exec=$(grep -m1 "^Exec=" "$f" | cut -d= -f2-)

    [[ -z "$name" || -z "$exec" ]] && continue

    # Remove field codes properly
    exec=$(echo "$exec" | sed -E 's/ ?%[a-zA-Z]//g')

    entries+="$name"$'\t'"$exec"$'\n'
  done
done

entries=$(printf '%s' "$entries" | sort -fu -t$'\t' -k1,1)

chosen=$(printf '%s' "$entries" | "$FZF" \
  --prompt='Launch: ' \
  --layout=reverse \
  --info=inline \
  --delimiter=$'\t' \
  --with-nth=1)

[[ -z "$chosen" ]] && exit 0

exec_cmd=$(printf '%s' "$chosen" | cut -d$'\t' -f2)

eval "$exec_cmd" &>/dev/null &
disown