Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Speed up get_packages #1857

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion neofetch
Original file line number Diff line number Diff line change
Expand Up @@ -1502,11 +1502,69 @@ get_packages() {
# to adjust the number of pkgs per pkg manager
pkgs_h=0

# get possible manager names for all os
case $os in
Linux|BSD|"iPhone OS"|Solaris)
manager_candidates=(
kiss cpt pacman dpkg xbps apk opkg pacman
lvu tce pkg_info tazpkg sorcery alps butch
swupd pisi dnf rpm mine brew emerge
Compile eopkg crew pkgtool scratch kagami cave
kpm-pkg guix nix-store pkginfo pkg flatpak
spm puyo snap appimaged
)
;;
"Mac OS X"|"macOS"|MINIX)
manager_candidates=(
port brew pkgin dpkg nix-store
)
;;
AIX|FreeMiNT)
manager_candidates=(
lslpp rpm
)
;;
Windows)
manager_candidates=(
cygcheck pacman scoop
)
;;
Haiku)
manager_candidates=(
pkgman
)
;;
IRIX)
manager_candidates=(
)
;;
esac
# Join manager names as regex to search for executable names by grep
# For example, find brew or apt or scoop by /brew$|/apt$|/scoop$
manager_candidates=$(printf '/%s$|' "${manager_candidates[@]}")
manager_candidates=${manager_candidates%"|"}

# find all the possible files for package managers at once
OLD_PATH=$PATH
PATH+=:EOF # little hack to list last PATH dir
manager_hits=($(printf '%s\n' ${PATH//:/\/* } | grep -E $manager_candidates -o))
PATH=$OLD_PATH

# has: Check if package manager installed.
# dir: Count files or dirs in a glob.
# pac: If packages > 0, log package manager name.
# tot: Count lines in command output.
has() { type -p "$1" >/dev/null && manager=$1; }
has() {
# check if the command is in the possible manager file list
if printf '%s\n' "${manager_hits[@]}" | grep -E "^/"$1"$" > /dev/null; then
# if it is in the possible manager file list, double check the availability of this command
if type -p "$1" >/dev/null; then
manager=$1
return 0
fi
fi
return 1
}
# globbing is intentional here
# shellcheck disable=SC2206
dir() { pkgs=($@); ((packages+=${#pkgs[@]})); pac "$((${#pkgs[@]}-pkgs_h))"; }
Expand Down