ambevar-dotfiles/.scripts/pacman-size

85 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
_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
OPT_SORTN="cat"
# SORT="sort -u -k3"
# SORT_SIZE="cat"
while getopts ":hnq" opt; do
case $opt in
h)
_printhelp "$0"
exit 1 ;;
n)
OPT_SORTN="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
if [ -f "${0%/*}/.pacman-init" ];then
. "${0%/*}/.pacman-init"
else
echo "Missing init file" >&2 && exit 1
fi
if [ $(pacman -Qq "$@" 2>/dev/null | wc -l) -eq 0 ]; then
echo "No valid package given." >&2
exit
fi
## We use external variable for awk to fit current locales.
## We use "eval" to be compatible with non-POSIX wordsplitting (e.g. zsh).
RESULT="$(${CMD} $@ | 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 -uk3 | eval $OPT_SORTN)"
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.
# ${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)}')