Activate zsh rehash with pacman hook

This commit is contained in:
Marko Korhonen 2021-08-17 20:54:45 +03:00
parent a0d236b56f
commit ed5db34d5b
4 changed files with 49 additions and 7 deletions

View file

@ -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:

View file

@ -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}')"

View file

@ -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

View file

@ -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