#!/usr/bin/env zsh # # Copyright © 2009 Nick Bowler # # License WTFPL2: Do What The Fuck You Want To Public License, version 2. # This is free software: you are free to do what the fuck you want to. # There is NO WARRANTY, to the extent permitted by law. sizes=false while getopts 's' opt $@; do case $opt in s) sizes=true ;; \?) exit 1 ;; esac done shift $((OPTIND-1)) if [[ $# == 0 ]]; then printf 'usage: %s [-s] directory [directory ...]\n' $0 exit 1 fi # Normalize target names. targets=() for arg in $@; do case $arg in /*) targets+=$arg ;; *) targets+=./$arg ;; esac done # Simple wrapper around find to search for ordinary files in the target # directories. mfind() { find $targets -type f $@ } # Simple ngettext wrapper that should work even if ngettext is not installed. ngettext() { command ngettext $@ 2>/dev/null && return 0; while [[ $# -gt 3 ]]; do shift; done case $3 in 1) printf '%s\n' "$1" ;; *) printf '%s\n' "$2" ;; esac } # Get the total size (in mebibytes) of a list of files specified on standard # input. The input is expected to be in the format of find -print0. getsize() { du -mc --files0-from=- | sed -n '$s/\ttotal$//p' (exit ${${pipestatus%0}[1]}) } formats=(flac flac ogg vorbis opus opus mp3 mp3) total=0 for ext name in $formats; do typeset $ext=`mfind -name \*.$ext | wc -l` total=$((total + $ext)) done if [[ $total == 0 ]]; then printf '0 tracks.\n' exit fi printf '%d %s in %d %s' "$total" `ngettext track tracks $total` \ "$#" `ngettext directory directories $#` if $sizes; then printf ' ' float totalsize=`mfind -print0 | getsize` if [[ $totalsize -gt 4000 ]]; then printf '(%.1fGiB total disk usage)' $((totalsize/1024)) else printf '(%.1fMiB total disk usage)' $((totalsize)) fi fi printf ':\n' for ext name in $formats; do printf ' %*d %5.1f%% %s' $#total $(($ext)) \ $((100.0*$ext/total)) $name if $sizes; then printf '%*s' $((8-$#name)) float size=`mfind -name \*.$ext -print0 | getsize` if [[ $totalsize -gt 4000 ]]; then integer t=$((totalsize/1024)) printf '(%*.1fGiB)' $(($#t+2)) $((size/1024)) else integer t=$((totalsize)) printf '(%*.1fMiB)' $(($#t+2)) $((size)) fi fi printf '\n' done