Skip to content

Instantly share code, notes, and snippets.

@rsms
Last active March 11, 2023 23:00
Show Gist options
  • Save rsms/36bda3b5c8ab83d951e45ed788a184f4 to your computer and use it in GitHub Desktop.
Save rsms/36bda3b5c8ab83d951e45ed788a184f4 to your computer and use it in GitHub Desktop.
source line-length histogram script
./linelen_hist.sh src '*.c'
COLS COUNT
2 1317 ████████████████████████████████████████████████████████████▌
4 583 ██████████████████████████▏
6 500 ██████████████████████▎
8 253 ███████████▊
10 264 ████████████▊
12 448 ████████████████████▋
14 417 ███████████████████▌
16 476 █████████████████████▍
18 445 ████████████████████▎
20 493 ██████████████████████▏
22 509 ███████████████████████▍
24 518 ███████████████████████▌
26 477 █████████████████████▊
28 549 █████████████████████████▋
30 489 ██████████████████████▎
32 426 ███████████████████▋
34 417 ███████████████████▌
36 397 ██████████████████▍
38 456 ████████████████████▎
40 388 █████████████████▎
42 363 ████████████████▉
44 376 █████████████████▊
46 369 ████████████████▋
48 320 ██████████████▎
50 279 ████████████▋
52 286 █████████████▊
54 257 ███████████▋
56 265 ████████████▎
58 237 ██████████▌
60 235 ██████████▌
62 236 ██████████
64 204 █████████▌
66 225 ██████████
68 225 ██████████
70 229 ██████████▉
72 213 █████████▌
74 211 █████████▋
76 222 ██████████▋
78 315 ██████████████
80 209 █████████▊
82 161 ███████▉
84 144 ██████▏
86 138 ██████▍
88 97 ████▊
90 77 ███▋
92 102 █████
94 52 ██▎
96 27 █▊
98 15 ▍
100 28 █▎
average columns per line: 30.7
#!/bin/bash
DIR=${1:-$(dirname "$0")/src}
PAT=${2:-'*.c'}
for f in $(find "$DIR" -type f -name $PAT); do
for e in $(awk '/^[[:space:]]*[^\/ \t]/ {print length}' $f |
sort -n | uniq -c | awk '{print $1 ":" $2}')
do
occurances=${e%:*}
width=${e#*:}
[ "$width" = "0" -o $width -gt 100 ] && continue # ignore
((histogram[$width]+=$occurances))
done
done
for i in $(seq 2 2 100); do
occurances1=${histogram[$(($i - 1))]} ; [ -z "$occurances" ] && occurances1=0
occurances=${histogram[$i]} ; [ -z "$occurances" ] && occurances=0
occurances=$(( occurances + occurances1 ))
((hist2[$i]+=$occurances))
done
nmax=0
nmin=100
for w in "${!hist2[@]}"; do
n=${hist2[$w]}
[ $n -lt $nmin ] && nmin=$n
[ $n -gt $nmax ] && nmax=$n
done
step=$(echo "scale=4; $nmax/100.0" | bc)
cols=60
avg=
# echo $step
printf "%-4s %-4s\n" "COLS" "COUNT"
for w in "${!hist2[@]}"; do
n=${hist2[$w]}
if [ -z "$avg" ]; then
avg=$(printf "%s\n" "scale=20; $n" | bc)
else
avg=$(printf "%s\n" "scale=20; ($avg + $n) / 2" | bc)
fi
t=$(printf "%s\n" "scale=8; $n/$nmax * $cols * 10" | bc)
u=$(printf "%.0f" "$t")
m=$(( $u / 10 ))
c=$(printf "%.0f" $(echo "scale=8; ($t - ($u - 0.5)) * 8" | bc))
printf "%-4s %-4s " "$w" "$n"
for (( i = 1; i <= $m; i++ )); do
printf "█"
done
if [ $c = 1 ]; then printf "▏"
elif [ $c = 2 ]; then printf "▎"
elif [ $c = 3 ]; then printf "▍"
elif [ $c = 4 ]; then printf "▌"
elif [ $c = 5 ]; then printf "▋"
elif [ $c = 6 ]; then printf "▊"
elif [ $c = 7 ]; then printf "▉"
elif [ $c = 8 ]; then printf "█"
fi
printf "\n"
done
printf "average columns per line: %.1f\n" "$avg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment