Skyler Grey | 92fb413 | 2022-10-30 22:31:42 +0000 | [diff] [blame] | 1 | From 5c53c16ed4634acca31c637c077363abacc7e3b4 Mon Sep 17 00:00:00 2001 |
| 2 | From: Skyler <skyler3665@gmail.com> |
| 3 | Date: Thu, 15 Sep 2022 14:08:26 +0100 |
| 4 | Subject: [PATCH] Update command-not-found.sh |
| 5 | |
| 6 | --- |
| 7 | command-not-found.sh | 129 +++++-------------------------------------- |
| 8 | 1 file changed, 14 insertions(+), 115 deletions(-) |
| 9 | |
| 10 | diff --git a/command-not-found.sh b/command-not-found.sh |
| 11 | index e542c49..ad87a91 100755 |
| 12 | --- a/command-not-found.sh |
| 13 | +++ b/command-not-found.sh |
| 14 | @@ -1,134 +1,33 @@ |
| 15 | #!/bin/sh |
| 16 | +# From https://gist.github.com/InternetUnexplorer/58e979642102d66f57188764bbf11701 |
| 17 | |
| 18 | -# for bash 4 |
| 19 | -# this will be called when a command is entered |
| 20 | -# but not found in the user’s path + environment |
| 21 | command_not_found_handle () { |
| 22 | - |
| 23 | - # TODO: use "command not found" gettext translations |
| 24 | - |
| 25 | - # taken from http://www.linuxjournal.com/content/bash-command-not-found |
| 26 | - # - do not run when inside Midnight Commander or within a Pipe |
| 27 | if [ -n "${MC_SID-}" ] || ! [ -t 1 ]; then |
| 28 | >&2 echo "$1: command not found" |
| 29 | return 127 |
| 30 | fi |
| 31 | |
| 32 | - toplevel=nixpkgs # nixpkgs should always be available even in NixOS |
| 33 | - cmd=$1 |
| 34 | - attrs=$(@out@/bin/nix-locate --minimal --no-group --type x --type s --top-level --whole-name --at-root "/bin/$cmd") |
| 35 | - len=$(echo -n "$attrs" | grep -c "^") |
| 36 | + echo -n "searching nix-index..." |
| 37 | + ATTRS=$(@nix-locate@ --minimal --no-group --type x --type s --top-level --whole-name --at-root "/bin/$1") |
| 38 | |
| 39 | - case $len in |
| 40 | + case `echo -n "$ATTRS" | grep -c "^"` in |
| 41 | 0) |
| 42 | - >&2 echo "$cmd: command not found" |
| 43 | - ;; |
| 44 | - 1) |
| 45 | - # if only 1 package provides this, then we can invoke it |
| 46 | - # without asking the users if they have opted in with one |
| 47 | - # of 2 environment variables |
| 48 | - |
| 49 | - # they are based on the ones found in |
| 50 | - # command-not-found.sh: |
| 51 | - |
| 52 | - # NIX_AUTO_INSTALL : install the missing command into the |
| 53 | - # user’s environment |
| 54 | - # NIX_AUTO_RUN : run the command transparently inside of |
| 55 | - # nix shell |
| 56 | - |
| 57 | - # these will not return 127 if they worked correctly |
| 58 | - |
| 59 | - if ! [ -z "${NIX_AUTO_INSTALL-}" ]; then |
| 60 | - >&2 cat <<EOF |
| 61 | -The program '$cmd' is currently not installed. It is provided by |
| 62 | -the package '$toplevel.$attrs', which I will now install for you. |
| 63 | -EOF |
| 64 | - if [ -e "$HOME/.nix-profile/manifest.json" ]; then |
| 65 | - nix profile install $toplevel#$attrs |
| 66 | - else |
| 67 | - nix-env -iA $toplevel.$attrs |
| 68 | - fi |
| 69 | - if [ "$?" -eq 0 ]; then |
| 70 | - $@ # TODO: handle pipes correctly if AUTO_RUN/INSTALL is possible |
| 71 | - return $? |
| 72 | - else |
| 73 | - >&2 cat <<EOF |
| 74 | -Failed to install $toplevel.attrs. |
| 75 | -$cmd: command not found |
| 76 | -EOF |
| 77 | - fi |
| 78 | - elif ! [ -z "${NIX_AUTO_RUN-}" ]; then |
| 79 | - nix-build --no-out-link -A $attrs "<$toplevel>" |
| 80 | - if [ "$?" -eq 0 ]; then |
| 81 | - # how nix-shell handles commands is weird |
| 82 | - # $(echo $@) is need to handle this |
| 83 | - nix-shell -p $attrs --run "$(echo $@)" |
| 84 | - return $? |
| 85 | - else |
| 86 | - >&2 cat <<EOF |
| 87 | -Failed to install $toplevel.attrs. |
| 88 | -$cmd: command not found |
| 89 | -EOF |
| 90 | - fi |
| 91 | - else |
| 92 | - if [ -e "$HOME/.nix-profile/manifest.json" ]; then |
| 93 | - >&2 cat <<EOF |
| 94 | -The program '$cmd' is currently not installed. You can install it |
| 95 | -by typing: |
| 96 | - nix profile install $toplevel#$attrs |
| 97 | - |
| 98 | -Or run it once with: |
| 99 | - nix run $toplevel#$attrs --command ... |
| 100 | -EOF |
| 101 | - else |
| 102 | - >&2 cat <<EOF |
| 103 | -The program '$cmd' is currently not installed. You can install it |
| 104 | -by typing: |
| 105 | - nix-env -iA $toplevel.$attrs |
| 106 | - |
| 107 | -Or run it once with: |
| 108 | - nix-shell -p $attrs --run ... |
| 109 | -EOF |
| 110 | - fi |
| 111 | - fi |
| 112 | + >&2 echo -ne "$(@tput@ el1)\r" |
| 113 | + >&2 echo "$1: command not found" |
| 114 | ;; |
| 115 | *) |
| 116 | - >&2 cat <<EOF |
| 117 | -The program '$cmd' is currently not installed. It is provided by |
| 118 | -several packages. You can install it by typing one of the following: |
| 119 | -EOF |
| 120 | - |
| 121 | - # ensure we get each element of attrs |
| 122 | - # in a cross platform way |
| 123 | - while read attr; do |
| 124 | - if [ -e "$HOME/.nix-profile/manifest.json" ]; then |
| 125 | - >&2 echo " nix profile install $toplevel#$attr" |
| 126 | - else |
| 127 | - >&2 echo " nix-env -iA $toplevel.$attr" |
| 128 | - fi |
| 129 | - done <<< "$attrs" |
| 130 | - |
| 131 | - >&2 cat <<EOF |
| 132 | - |
| 133 | -Or run it once with: |
| 134 | -EOF |
| 135 | - |
| 136 | - while read attr; do |
| 137 | - if [ -e "$HOME/.nix-profile/manifest.json" ]; then |
| 138 | - >&2 echo " nix run $toplevel#$attr --command ..." |
| 139 | - else |
| 140 | - >&2 echo " nix-shell -p $attr --run ..." |
| 141 | - fi |
| 142 | - done <<< "$attrs" |
| 143 | - ;; |
| 144 | + >&2 echo -ne "$(@tput@ el1)\r" |
| 145 | + >&2 echo "The program ‘$(@tput@ setaf 4)$1$(@tput@ sgr0)’ is currently not installed." |
| 146 | + >&2 echo "You can use one of the following commands to open it in an ephemeral shell:" |
| 147 | + while read ATTR; do |
| 148 | + ATTR=$(echo "$ATTR" | sed 's|\.out$||') # Strip trailing '.out' |
| 149 | + >&2 echo " nix shell $(@tput@ setaf 12)nixpkgs#$(@tput@ setaf 4)$ATTR$(@tput@ sgr0)" |
| 150 | + done <<< "$ATTRS" |
| 151 | esac |
| 152 | |
| 153 | - return 127 # command not found should always exit with 127 |
| 154 | + return 127 |
| 155 | } |
| 156 | |
| 157 | -# for zsh... |
| 158 | -# we just pass it to the bash handler above |
| 159 | -# apparently they work identically |
| 160 | command_not_found_handler () { |
| 161 | command_not_found_handle $@ |
| 162 | return $? |