diff --git a/config-root.yaml b/config-root.yaml index a7264f7c..f4b22ec4 100644 --- a/config-root.yaml +++ b/config-root.yaml @@ -59,6 +59,9 @@ dotfiles: f_pacman.conf: dst: /etc/pacman.conf src: etc/pacman.conf + f_pacman_zsh_hook: + dst: /etc/pacman.d/hooks/zsh.hook + src: etc/pacman.d/hooks/zsh.hook f_sshd_config: dst: /etc/ssh/sshd_config src: etc/ssh/sshd_config @@ -113,6 +116,7 @@ profiles: Pacman: dotfiles: - f_pacman.conf + - f_pacman_zsh_hook - f_pacserve.service.conf Mirkwood: dotfiles: diff --git a/home/.config/zsh/03-aliases.zsh b/home/.config/zsh/03-aliases.zsh index 1190315d..82a98df6 100644 --- a/home/.config/zsh/03-aliases.zsh +++ b/home/.config/zsh/03-aliases.zsh @@ -21,6 +21,34 @@ _fzf_compgen_path() { _skim_compgen_path } +# Command not found handler +# source https://wiki.archlinux.org/title/Zsh#pacman_-F_%22command_not_found%22_handler +function command_not_found_handler { + local purple='\e[1;35m' bright='\e[0;1m' green='\e[1;32m' reset='\e[0m' + printf 'zsh: command not found: %s\n' "$1" + local entries=( + ${(f)"$(/usr/bin/pacman -F --machinereadable -- "/usr/bin/$1")"} + ) + if (( ${#entries[@]} )) + then + printf "${bright}$1${reset} may be found in the following packages:\n" + local pkg + for entry in "${entries[@]}" + do + # (repo package version file) + local fields=( + ${(0)entry} + ) + if [[ "$pkg" != "${fields[2]}" ]] + then + printf "${purple}%s/${bright}%s ${green}%s${reset}\n" "${fields[1]}" "${fields[2]}" "${fields[3]}" + fi + printf ' /%s\n' "${fields[4]}" + pkg="${fields[2]}" + done + fi +} + # search and install packages with skim pi() { SELECTED_PKGS="$(paru -Slq | sk --header='Install packages' -m --preview 'paru -Si {1}')" diff --git a/home/.config/zsh/06-pacman.zsh b/home/.config/zsh/06-pacman.zsh index 56ebbcea..0398c378 100644 --- a/home/.config/zsh/06-pacman.zsh +++ b/home/.config/zsh/06-pacman.zsh @@ -1,6 +1,17 @@ # from https://wiki.archlinux.org/index.php/Zsh#On-demand_rehash -catch_signal_usr1() { - trap catch_signal_usr1 USR1 - rehash + +zshcache_time="$(date +%s%N)" + +autoload -Uz add-zsh-hook + +rehash_precmd() { + if [[ -a /var/cache/zsh/pacman ]]; then + local paccache_time="$(date -r /var/cache/zsh/pacman +%s%N)" + if (( zshcache_time < paccache_time )); then + rehash + zshcache_time="$paccache_time" + fi + fi } -trap catch_signal_usr1 USR1 + +add-zsh-hook -Uz precmd rehash_precmd diff --git a/root/etc/pacman.d/hooks/zsh.hook b/root/etc/pacman.d/hooks/zsh.hook index 4c3881c0..567e8b83 100644 --- a/root/etc/pacman.d/hooks/zsh.hook +++ b/root/etc/pacman.d/hooks/zsh.hook @@ -2,11 +2,10 @@ Operation = Install Operation = Upgrade Operation = Remove -Type = Package +Type = Path Target = usr/bin/* [Action] Depends = zsh -Depends = procps-ng When = PostTransaction -Exec = /usr/bin/pkill zsh --signal=USR1 +Exec = /usr/bin/install -Dm644 /dev/null /var/cache/zsh/pacman