Browse Source

refactor(command-not-found): clean up and reorganize logic

Marc Cornellà 3 years ago
parent
commit
b54a6dab9c
1 changed files with 47 additions and 47 deletions
  1. 47 47
      plugins/command-not-found/command-not-found.plugin.zsh

+ 47 - 47
plugins/command-not-found/command-not-found.plugin.zsh

@@ -1,55 +1,55 @@
-# Uses the command-not-found package zsh support
-# as seen in https://www.porcheron.info/command-not-found-for-zsh/
-# this is installed in Ubuntu
+## Platforms with a built-in command-not-found handler init file
+
+for file (
+  # Arch Linux. Must have pkgfile installed: https://wiki.archlinux.org/index.php/Pkgfile#Command_not_found
+  /usr/share/doc/pkgfile/command-not-found.zsh
+  # macOS (M1 and classic Homebrew): https://github.com/Homebrew/homebrew-command-not-found
+  /opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh
+  /usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh
+); do
+  if [[ -r "$file" ]]; then
+    source "$file"
+    unset file
+    return 0
+  fi
+done
+unset file
 
-if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
-    function command_not_found_handler {
-        # check because c-n-f 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 "zsh: command not found: %s\n" "$1" >&2
-            return 127
-        fi
-        return 0
-    }
-fi
 
-# Arch Linux command-not-found support, you must have package pkgfile installed
-# https://wiki.archlinux.org/index.php/Pkgfile#.22Command_not_found.22_hook
-[[ -e /usr/share/doc/pkgfile/command-not-found.zsh ]] && source /usr/share/doc/pkgfile/command-not-found.zsh
+## Platforms with manual command_not_found_handler() setup
 
-# Fedora command-not-found support
-if [ -f /usr/libexec/pk-command-not-found ]; then
-    command_not_found_handler() {
-        runcnf=1
-        retval=127
-        [ ! -S /var/run/dbus/system_bus_socket ] && runcnf=0
-        [ ! -x /usr/libexec/packagekitd ] && runcnf=0
-        if [ $runcnf -eq 1 ]; then
-            /usr/libexec/pk-command-not-found $@
-            retval=$?
-        fi
-        return $retval
-    }
+# Debian and derivatives: https://launchpad.net/ubuntu/+source/command-not-found
+if [[ -x /usr/lib/command-not-found || -x /usr/share/command-not-found/command-not-found ]]; then
+  command_not_found_handler() {
+    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 "zsh: command not found: %s\n" "$1" >&2
+      return 127
+    fi
+  }
 fi
 
-# macOS command-not-found support
-# https://github.com/Homebrew/homebrew-command-not-found
-HB_CNF_HANDLER_SUFFIX="Library/Taps/homebrew/homebrew-command-not-found/handler.sh"
-if [[ -s "/opt/homebrew/$HB_CNF_HANDLER_SUFFIX" ]]; then
-      source "/opt/homebrew/$HB_CNF_HANDLER_SUFFIX"
-elif [[ -s "/usr/local/Homebrew/$HB_CNF_HANDLER_SUFFIX" ]]; then
-      source "/usr/local/Homebrew/$HB_CNF_HANDLER_SUFFIX"
+# Fedora: https://fedoraproject.org/wiki/Features/PackageKitCommandNotFound
+if [[ -x /usr/libexec/pk-command-not-found ]]; then
+  command_not_found_handler() {
+    if [[ -S /var/run/dbus/system_bus_socket && -x /usr/libexec/packagekitd ]]; then
+      /usr/libexec/pk-command-not-found -- "$@"
+      return $?
+    fi
+
+    printf "zsh: command not found: %s\n" "$1" >&2
+    return 127
+  }
 fi
 
-# NixOS command-not-found support
-if [ -x /run/current-system/sw/bin/command-not-found ]; then
-    command_not_found_handler() {
-        /run/current-system/sw/bin/command-not-found $@
-    }
+# NixOS: https://github.com/NixOS/nixpkgs/tree/master/nixos/modules/programs/command-not-found
+if [[ -x /run/current-system/sw/bin/command-not-found ]]; then
+  command_not_found_handler() {
+    /run/current-system/sw/bin/command-not-found -- "$@"
+  }
 fi