summaryrefslogtreecommitdiff
path: root/dot-config/fish/functions/fish_command_not_found.fish
blob: 5b454ee9d7a50a1a61e08adc54bd9787d1527013 (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
function fish_command_not_found --description "Fish: suggest/install AUR pkg when command not found"
    set -l cmd $argv[1]
    set -l pkgs

    printf '\033[0;31m\033[1m==> ERROR: Command not found:\033[0m %s\n' "$cmd"
    printf '\033[0m'
    printf 'Checking for package.'

    # spinner: print dots until pkgfile finishes
    set -l spinner_pid
    fish -c '
        while true
            sleep 0.1
            printf "."
        end
    ' &
    set spinner_pid $last_pid

    # query pkgfile; -b (binaries), -v (verbose) to include repo/pkg paths
    set -l out (pkgfile -b -v -- "$cmd" ^/dev/null)

    # stop spinner
    kill $spinner_pid 2>/dev/null
    echo

    if test -n "$out"
        # Split lines into array
        set pkgs $out

        printf 'The application %s is not installed. It may be found in the following AUR packages:\n' "$cmd"
        for p in $pkgs
            printf '  %s\n' "$p"
        end

        # Emulate original logic: take second field after splitting first match by "/"
        # Example line format often like: "aur/pkgname usr/bin/cmd"
        set -l first $pkgs[1]
        set -l parts (string split -m1 ' ' -- $first)[1]
        set -l repo_pkg (string split '/' -- $parts)
        set -l pkgname $repo_pkg[2]

        if test -n "$pkgname"
            printf 'Do you want to Install AUR package %s? (y/N) ' "$pkgname"
            read -l -n 1 choice
            echo
            if test (string lower -- "$choice") = y
                echo "Executing command: yay -S $pkgname"
                yay -S "$pkgname" --noconfirm
                echo
                return
            else
                echo
                return
            end
        end
    end 1>&2

    return 127
end