aboutsummaryrefslogtreecommitdiffstats
path: root/.config/init/funcs/mpd-filetypes
blob: 0bc93d2d9242ce088b494beaa835f59c98c9b37c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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 "┘"