aboutsummaryrefslogtreecommitdiffstats
path: root/.config/init/funcs/mpd-filetypes
diff options
context:
space:
mode:
Diffstat (limited to '.config/init/funcs/mpd-filetypes')
-rwxr-xr-x.config/init/funcs/mpd-filetypes99
1 files changed, 99 insertions, 0 deletions
diff --git a/.config/init/funcs/mpd-filetypes b/.config/init/funcs/mpd-filetypes
new file mode 100755
index 0000000..0bc93d2
--- /dev/null
+++ b/.config/init/funcs/mpd-filetypes
@@ -0,0 +1,99 @@
+#!/usr/bin/env zsh
+# list stats about mpd library file types, to motivate me
+# to do better!
+
+local pattern
+
+# if args exist, read them as file extensions in a pattern
+if
+if [[ ! -z "$@" ]]; then
+ pattern=$(echo "$@" | sed -e 's/\s/|/g' -e 's/\(.*\)/(\1)/')
+else
+# else hackily yoink recognised types from mpd --version
+ pattern=$(mpd --version \
+ | sed -n '1h; 1!H; ${g; s/.*Decoders plugins:\(.*\)Output.*/\1/g; p}' \
+ | sed -e 's/.*\]//' | tr -d '\n' \
+ | sed -e 's/^ //' -e 's/\s/|/g' -e 's/\(.*\)/(\1)/')
+fi
+
+# find file counts
+local i=1
+local total=0
+local totalstr='total'
+local counts=()
+local types=()
+find ~/music -type f -regextype posix-extended \
+ -regex ".*\.$pattern" | grep -oE "\.$pattern$" \
+ | sort | uniq -c | sed -e 's/^\s*\([0-9]* \)\./\1/' | \
+while read -A line; do
+ counts[$i]=${line[1]}
+ types[$i]=${line[2]}
+ total=$(( $total + ${line[1]} ))
+ i=$(( $i + 1 ))
+done
+
+# calculate percentages
+local twidth=0
+local cwidth=0
+local percentages=()
+for i in {1..${#types}}; do
+ percentages[$i]="%$(calc -p \
+ "a=${counts[$i]} * 100 / $total; round(a, 5-digits(a), 16)" \
+ | tr -d '~')"
+
+done
+
+types=( 'type' "${types[@]}" 'total' )
+counts=( 'count' "${counts[@]}" $total )
+percentages=( 'percent' "${percentages[@]}" ' ')
+
+# calculate padding widths
+for i in {1..${#types}}; do
+ if [[ ${#types[$i]} -gt $twidth ]]; then
+ twidth=${#types[$i]}
+ fi
+
+ if [[ ${#counts[$i]} -gt $cwidth ]]; then
+ cwidth=${#counts[$i]}
+ fi
+done
+
+
+# print results
+local linelen=$(( $twidth + $cwidth + 13 ))
+local j
+echo -n "┌"
+for (( j=0; j < $linelen; j++ )); do
+ echo -n "─"
+done
+echo "┐"
+
+for i in {1..${#types}}; do
+ if [[ $i -eq 2 || $i -eq ${#types} ]]; then
+ echo -n "├"
+ for (( j=0; j < $linelen; j++ )); do
+ echo -n "─"
+ done
+ echo "┤"
+ fi
+
+ if [[ $i -eq 1 ]]; then
+ printf "│ %${twidth}s " "${types[$i]}"
+ else
+ printf "│ %${twidth}s: " "${types[$i]}"
+ fi
+
+ printf "%${cwidth}s" "${counts[$i]}"
+
+ if [[ $i -eq 1 || $i -eq ${#types} ]]; then
+ printf " %-7s │\n" "${percentages[$i]}"
+ else
+ printf ", %-7s │\n" "${percentages[$i]}"
+ fi
+done
+
+echo -n "└"
+for (( j=0; j < $linelen; j++ )); do
+ echo -n "─"
+done
+echo "┘"