From: Nick Bowler Date: Fri, 7 Dec 2012 01:49:31 +0000 (-0500) Subject: Mostly rewrite musicstats.zsh. X-Git-Url: https://git.draconx.ca/gitweb/scripts.git/commitdiff_plain/d180c0bfdd4e3911a18360b3ccacd5271441e240 Mostly rewrite musicstats.zsh. This new version has less hardcoded behaviour, and a somewhat different output format. Add support for Opus files. --- diff --git a/musicstats.zsh b/musicstats.zsh index c5b9a95..17e8e43 100755 --- a/musicstats.zsh +++ b/musicstats.zsh @@ -6,10 +6,10 @@ # 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=0 +sizes=false while getopts 's' opt $@; do case $opt in - s) sizes=1 ;; + s) sizes=true ;; \?) exit 1 ;; esac done @@ -21,41 +21,81 @@ if [[ $# == 0 ]]; then exit 1 fi -alias mfind='find $@ -type f' -flac=`mfind -name '*.flac' | wc -l` -vorbis=`mfind -name '*.ogg' | wc -l` -mp3=`mfind -name '*.mp3' | wc -l` -total=$((flac+vorbis+mp3)) +# 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 tracks: %d (%.1f%%) flac, %d (%.1f%%) vorbis, %d (%.1f%%) mp3.\n' \ - $total \ - $flac $((100.0*flac /total)) \ - $vorbis $((100.0*vorbis/total)) \ - $mp3 $((100.0*mp3 /total)) +printf '%d %s in %d %s' "$total" `ngettext track tracks $total` \ + "$#" `ngettext directory directories $#` +if $sizes; then + printf ' ' -if [[ $sizes == 1 ]]; then - alias getsize="du -mc --files0-from=- | sed -n '\$s/\ttotal$//p'" float totalsize=`mfind -print0 | getsize` - float flacsize=`mfind -name '*.flac' -print0 | getsize` - float vorbissize=`mfind -name '*.ogg' -print0 | getsize` - float mp3size=`mfind -name '*.mp3' -print0 | getsize` - if [[ $totalsize -gt 4000 ]]; then - printf 'size: %.1fG total, %.1fG flac, %.1fG vorbis, %.1fG mp3.\n' \ - $((totalsize/1024)) \ - $((flacsize/1024)) \ - $((vorbissize/1024)) \ - $((mp3size/1024)) + printf '(%.1fGiB total disk usage)' $((totalsize/1024)) else - printf 'size: %.1fM total, %.1fM flac, %.1fM vorbis, %.1fM mp3.\n' \ - $((totalsize)) \ - $((flacsize)) \ - $((vorbissize)) \ - $((mp3size)) + 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