fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes Altmanningerfish_command_not_found: move non-interactive logicd601ceb55

master
3.2 KiB80 linesraw
1### Command-not-found handlers
2# This can be overridden by defining a new fish_command_not_found function
3
4function fish_command_not_found
5    __fish_default_command_not_found_handler $argv
6end
7
8if not status is-interactive
9    exit
10end
11
12# Read the OS/Distro from /etc/os-release.
13# This has a "ID=" line that defines the exact distribution,
14# and an "ID_LIKE=" line that defines what it is derived from or otherwise like.
15# For our purposes, we use both.
16set -l os
17if test -r /etc/os-release
18    set os (string replace -rf '^ID(?:_LIKE)?\s*=(.*)' '$1' < /etc/os-release |
19        string trim -c '\'"' | string split " ")
20end
21
22# If an old handler already exists, defer to that.
23if functions -q __fish_command_not_found_handler
24    function fish_command_not_found
25        # The fish_command_not_found event was removed in fish 3.2.0,
26        # and future versions of fish will just call a function called "fish_command_not_found".
27        # You have defined a custom handler, we suggest renaming it to "fish_command_not_found".
28        __fish_command_not_found_handler $argv
29    end
30    # First check if we are on OpenSUSE since SUSE's handler has no options
31    # but the same name and path as Ubuntu's.
32else if contains -- suse $os || contains -- sles $os && type -q command-not-found
33    function fish_command_not_found
34        command-not-found $argv[1]
35    end
36    # Check for Fedora's handler
37else if test -f /usr/libexec/pk-command-not-found
38    function fish_command_not_found
39        /usr/libexec/pk-command-not-found $argv
40    end
41    # Check in /usr/lib, where Ubuntu places this command
42else if test -f /usr/lib/command-not-found
43    function fish_command_not_found
44        /usr/lib/command-not-found -- $argv[1]
45    end
46    # Check for NixOS handler
47else if test -f /run/current-system/sw/bin/command-not-found
48    function fish_command_not_found
49        /run/current-system/sw/bin/command-not-found $argv
50    end
51    # Ubuntu Feisty places this command in the regular path instead
52else if type -q command-not-found
53    function fish_command_not_found
54        command-not-found -- $argv[1]
55    end
56    # pkgfile is an optional, but official, package on Arch Linux
57    # it ships with example handlers for bash and zsh, so we'll follow that format
58else if type -q pkgfile
59    function fish_command_not_found
60        set -l __packages (pkgfile --binaries --verbose -- $argv[1] 2>/dev/null)
61        if test $status -eq 0
62            printf "%s may be found in the following packages:\n" "$argv[1]"
63            printf "  %s\n" $__packages
64        else
65            __fish_default_command_not_found_handler $argv[1]
66        end
67    end
68    # pacman is too slow, see #7841.
69    # else if type -q pacman
70    #     function fish_command_not_found
71    #         set -l paths $argv[1]
72    #         # If we've not been given an absolute path, try $PATH as the starting point,
73    #         # otherwise pacman will try *every path*, and e.g. bash-completion
74    #         # isn't helpful.
75    #         string match -q '/*' -- $argv[1]; or set paths $PATH/$argv[1]
76    #         # Pacman only prints the path, so we still need to print the error.
77    #         __fish_default_command_not_found_handler $argv[1]
78    #         pacman -F $paths
79    #     end
80end