ambevar-dotfiles/.scripts/pacman-size

78 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
if [ -f "${0%/*}/.pacman-init" ];then
. "${0%/*}/.pacman-init"
else
echo "Missing init file" && exit 1
fi
_printhelp()
{
cat <<EOF
Usage: ${1##*/} [OPTIONS] PACKAGES
Display package size. Output contains no double and is alphabetically sorted. A
grand total is printed at the end. It will only work for repos packages by
default.
-h: Show this help.
-n: Output is sorted by size.
-q: Uses installed packages database instead of repos database. It speeds up queries and allows displaying size of local packages not available in repos.
EOF
}
CMD="pacman -Si"
SEP=": "
TOTAL_SIZE=0
SORT="sort -u -k3"
SORT_SIZE="cat"
while getopts ":hnq" opt; do
case $opt in
h)
_printhelp "$0"
exit 1
;;
n)
SORT_SIZE="sort -n"
;;
q)
CMD="pacman -Qi"
;;
?)
_printhelp "$0"
exit 1
;;
:)
echo "Missing argument."
_printhelp "$0"
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
echo "Missing argument."
_printhelp "$0"
exit 1
fi
## We use external variable for awk to fit current locales.
RESULT="$(eval "${CMD} $@" 2>/dev/null | awk -F "$SEP" -v filter="$pacman_size" -v pkg="$pacman_name" \
'$0 ~ pkg {pkgname=$2} $0 ~ filter {gsub(/\..*/,"") ; printf("%6s KiB %s\n", $2, pkgname)}' | eval "$SORT" | eval "$SORT_SIZE")"
echo "$RESULT"
## Print total size.
echo "$RESULT" | awk '{TOTAL=$1+TOTAL} END {printf("Total: %d KiB\n",TOTAL)}'
## One line version. Approximately same performance. Note the combination of
## 'tee' and >(awk ...) to write the detailed list to standard output and to
## pass it to awk.
# eval "${CMD} $@ 2>/dev/null" | awk -F "$SEP" -v filter="$pacman_size" -v pkg="$pacman_name" \
# '$0 ~ pkg {pkgname=$2} $0 ~ filter {gsub(/\..*/,"") ; printf("%6s KiB %s\n", $2, pkgname)}' | sort -u -k3 \
# | tee >(awk '{TOTAL=$1+TOTAL} END {printf("Total: %d KiB\n",TOTAL)}')