Add support for ubuntu command-not-found handler

This commit is contained in:
ItsDrike 2021-01-28 12:46:58 +01:00
parent 7544525472
commit edc7a37a1e
No known key found for this signature in database
GPG key ID: 252D306F545351FC

View file

@ -1,24 +1,43 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Command not found hook that uses `pkgfile` package if command -v pkgfile > /dev/null; then
# to search through the package database in order to # Command not found hook that uses `pkgfile` package
# find a package which includes given command, which # to search through the package index in order to find
# was resolved as not found, if there are no such packages # a package which includes given command, which was resolved
# only print command not found message # and not found, if there are no such packages, only print
command_not_found_handler() { # command not found message
local pkgs cmd="$1" files=() command_not_found_handler() {
printf 'zsh: command not found: %s' "$cmd" # print command not found asap, then search for packages local pkgs cmd="$1" files=()
files=(${(f)"$(pkgfile ${cmd})"}) printf 'zsh: command not found: %s' "$cmd" # print command not found asap, then search for packages
if (( ${#files[@]} )); then files=(${(f)"$(pkgfile ${cmd})"})
printf '\r%s may be found in the following packages:\n' "$cmd" if (( ${#files[@]} )); then
local res=() repo package version file printf '\r%s may be found in the following packages:\n' "$cmd"
for file in "$files[@]"; do local res=() repo package version file
res=("${(0)file}") for file in "$files[@]"; do
repo="$res[1]" res=("${(0)file}")
printf ' %s\n' "$repo" repo="$res[1]"
done printf ' %s\n' "$repo"
else done
printf '\n' else
fi printf '\n'
return 127 fi
} return 127
}
elif [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
# Ubuntu handle for bash default command-not-found
# it works similarely to the above arch alternative,
# this is a clone of the original bash implementation
function command_not_found_handle {
# check because cmd not found could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1"
return $?
elif [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
return 127
fi
}
fi