Skip to content

Instantly share code, notes, and snippets.

@rasmusmerzin
Created February 19, 2021 22:23
Show Gist options
  • Save rasmusmerzin/88b50c9c0e35e8461a8a024dcfbd2d7f to your computer and use it in GitHub Desktop.
Save rasmusmerzin/88b50c9c0e35e8461a8a024dcfbd2d7f to your computer and use it in GitHub Desktop.
#!/bin/bash
[ -z "$1" ] && {
echo "error: specify file or directory"
exit 1
}
files=$(find "$@" -type f)
exit_code=$?; [ $exit_code -ne 0 ] && exit $exit_code
name_width=20
for f in $files
do [ ${#f} -gt $name_width ] && name_width=${#f}
done
file_count=$(printf '%s' "$files" | wc -l)
total_sloc=0
total_oks=0
total_prints=0
total_unwraps=0
dismissed=
checked=
log() {
name=$1
sloc=$2
prints=$3
oks=$4
unwraps=$5
printf '%b\n' "$(
printf "%-${name_width}s %6d sloc" "$name" "$sloc"
[ -n "$prints" ] && {
[ "$prints" -gt 0 ] && printf '\33[1;35m'
printf '%4d print\33[m' "$prints"
[ "$oks" -gt 0 ] && printf '\33[1;33m'
printf '%4d ok\33[m' "$oks"
[ "$unwraps" -gt 0 ] && printf '\33[1;31m'
printf '%4d unwrap\33[m' "$unwraps"
}
)"
}
for f in $files
do
sloc=$(grep -vc '^\(\s*$\|//\)' "$f")
total_sloc=$((total_sloc + sloc))
if [ "${f##*.}" = 'rs' ]
then
prints=$(grep -c '\<e\?print\(ln\)\?!(' "$f")
oks=$(grep -c '\<ok();' "$f")
unwraps=$(grep -c '\<unwrap()' "$f")
checked="$checked$(log "$f" "$sloc" "$prints" "$oks" "$unwraps")\n"
total_prints=$((total_prints + prints))
total_oks=$((total_oks + oks))
total_unwraps=$((total_unwraps + unwraps))
else dismissed="$dismissed$(log "$f" "$sloc")\n"
fi
done
printf '\33[2m'
printf '%b' "$dismissed" | sort -n -k2
printf '\33[m\n'
printf '%b' "$checked" | sort -n -k8 -k6 -k4 -k2
echo
log "$file_count files" "$total_sloc" "$total_prints" "$total_oks" "$total_unwraps"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment