Chrysostomus / manjaro-zsh-config

Zsh configuration package for manjaro
MIT License
124 stars 52 forks source link

use pkgfile #26

Open papajoker opened 3 years ago

papajoker commented 3 years ago

why use pkgfile ? with pamac we have *.files so this package is not usefull

test code (write in bash NOT zsh !!!!!) for command-not-found

cmd="lsd -l"    # my test
if [ -f "/var/lib/pacman/sync/core.files" ]; then
    cmd=($cmd)
    echo "\"${cmd[0]}\" command not found"
    # pkgs=$(pacman -Fxq "usr/bin/.*/${cmd[0]}$")   # -Fx is slow ? 
    # find first command , after gui app
    found=$(pacman -Fl | grep -E "usr/bin/${cmd[0]}$|usr/share/applications/${cmd[0]}.desktop$" -m 1)
    if [ -n "$found" ]; then
        echo "\"${found% *}\" package to install for command \"/${found##* }\" ? [y/N]"
    fi
fi
Chrysostomus commented 3 years ago

When I tried implement this pamac originally, it ended being significantly slower than pkgfile. I'll check if the situation has changed...

papajoker commented 3 years ago

here, i use grep -m1 so, if found , this command not search in all database

other way (for me best speed) is to read in ".files", example (search only in /usr/bin lower case command)

#!/usr/bin/env bash
# command-not-found
cmd="$@"
for repo in $(pacman-conf -l); do
    echo "find in $repo ..."
    pkgs=$(zcat "/var/lib/pacman/sync/${repo}.files"|awk '/%NAME%/ {getline;name=$0} /usr\/bin\/'"${1,,}"'$/ {printf "%s %s\n",name,$1}')
    if [ -n "$pkgs" ]; then
        printf "package: %s command: /%s exists, install it ? [y/N]\n" ${pkgs[@]}
        exit 0  # not search in other
    fi
    exit 127    # not found :(
done

result

./test.sh vlc -x
find in core ...
find in extra ...
package: vlc command: /usr/bin/vlc
./test.sh zstd xx
find in core ...
package: zstd command: /usr/bin/zstd

EDIT: as @yochananmarqos package, display more info (url)

pkgs=$(zcat "/var/lib/pacman/sync/${repo}.files"|awk '/^%NAME%$/ {getline;name=$0} /^%URL%$/ {getline;url=$0} /usr\/bin\/'"${1,,}"'$/ {printf "%s %s %s\n",name,$1,url}')
...
printf "package: %s command: /%s exists, %s, install it ? [y/N]\n" ${pkgs[@]}

result:

./test.sh Firefox
find in core ...
find in extra ...
package: firefox command: /usr/bin/firefox exists, https://www.mozilla.org/firefox/, install it ? [y/N]
yochananmarqos commented 3 years ago

There's also command-not-found which uses the existing Pacman database. However, it's not in active development. Neat thing is, it has some options pkgfile does not:

❯ yarn
find-the-command: "yarn" is not found locally, searching in repositories...

"yarn" may be found in package "community/yarn"

What would you like to do? 
1) install             2) info                3) list files          4) list files (paged)  
Action (0 to abort): 
papajoker commented 3 years ago

yes good, but only not use pacman sync datatase ? and us we have .files with pamac my first idea is to re-use pamac database

papajoker commented 3 years ago

gist created for test speed 4 solutions, zsh code
results at file end : best is pacman -Fl and grep
ps: pamac : always read all, so always same time (if result in core or community change nothing)

alexiswa commented 2 years ago

i know this hasnt been posted on for a long time, but this has been troubling many users for quite some time. Not sure if there are any alternatives.