-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions
64 lines (59 loc) · 1.96 KB
/
functions
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
pman() {
man -t "${1}" | open -f -a /Applications/Preview.app
}
__sizeup_build_query () {
local bool="and"
local query=""
for t in $@; do
query="$query -$bool -iname \"*.$t\""
bool="or"
done
echo -n "$query"
}
__sizeup_humanize () {
local size=$1
if [ $size -ge 1073741824 ]; then
printf '%6.2f%s' $(echo "scale=2;$size/1073741824"| bc) G
elif [ $size -ge 1048576 ]; then
printf '%6.2f%s' $(echo "scale=2;$size/1048576"| bc) M
elif [ $size -ge 1024 ]; then
printf '%6.2f%s' $(echo "scale=2;$size/1024"| bc) K
else
printf '%6.2f%s' ${size} b
fi
}
sizeup () {
local helpstring="Show file sizes for all files with totals\n-r\treverse sort\n-[0-3]\tlimit depth (default 4 levels, 0=unlimited)\nAdditional arguments limit by file extension\n\nUsage: sizeup [-r[0123]] ext [,ext]"
local totalb=0
local size output reverse OPT
local depth="-maxdepth 4"
OPTIND=1
while getopts "hr0123" opt; do
case $opt in
r) reverse="-r " ;;
0) depth="" ;;
1) depth="-maxdepth 1" ;;
2) depth="-maxdepth 2" ;;
3) depth="-maxdepth 3" ;;
h) echo -e $helpstring; return;;
\?) echo "Invalid option: -$OPTARG" >&2; return 1;;
esac
done
shift $((OPTIND-1))
local cmd="find . -type f ${depth}$(__sizeup_build_query $@)"
local counter=0
while read -r file; do
counter=$(( $counter+1 ))
size=$(stat -f '%z' "$file")
totalb=$(( $totalb+$size ))
>&2 echo -ne $'\E[K\e[1;32m'"${counter}:"$'\e[1;31m'" $file "$'\e[0m'"("$'\e[1;31m'$size$'\e[0m'")"$'\r'
# >&2 echo -n "$(__sizeup_humanize $totalb): $file ($size)"
# >&2 echo -n $'\r'
output="${output}${file#*/}*$size*$(__sizeup_humanize $size)\n"
done < <(eval $cmd)
>&2 echo -ne $'\r\E[K\e[0m'
echo -e "$output"| sort -t '*' ${reverse}-nk 2 | cut -d '*' -f 1,3 | column -s '*' -t
echo $'\e[1;33;40m'"Total: "$'\e[1;32;40m'"$(__sizeup_humanize $totalb)"$'\e[1;33;40m'" in $counter files"$'\e[0m'
}
# vim: ai ts=2 sw=2 et sts=2 ft=sh
# vim: autoindent tabstop=2 shiftwidth=2 expandtab softtabstop=2 filetype=sh