Skip to content

Instantly share code, notes, and snippets.

@dhaigh
Last active July 19, 2020 09:25
Show Gist options
  • Save dhaigh/148117df0deddee5d824e3cf21fd4bc8 to your computer and use it in GitHub Desktop.
Save dhaigh/148117df0deddee5d824e3cf21fd4bc8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Usage:
# ./crunch.sh
FILE_LIST=/tmp/dirless_file_list
rg --files | grep -v '.lock$' > $FILE_LIST
TOTAL_FILES=$(cat $FILE_LIST | wc -l)
TOTAL_LINES=$(cat $FILE_LIST | tr '\n' '\0' | xargs -0 cat | wc -l)
file_percentage () { echo "$(echo "scale=1; 100 * $1 / $TOTAL_FILES" | bc)%"; }
line_percentage () { echo "$(echo "scale=1; 100 * $1 / $TOTAL_LINES" | bc)%"; }
divide () { echo "scale=1; $1 / $2" | bc; }
format_number () { printf "%'d" $1; }
crunch () {
files=$(cat $FILE_LIST | grep $1 $2 | wc -l)
echo ' files:' $(format_number $files) "($(file_percentage $files))"
lines=$(cat $FILE_LIST | grep $1 $2 | tr '\n' '\0' | xargs -0 cat | wc -l)
echo -n ' lines:' $(format_number $lines) "($(line_percentage $lines))"
echo " (avg $(divide $lines $files) per file)"
}
#######################################################################################################################
# CONFIGURE THE DIFFERENT SECTIONS YOU WANT HERE
sections=('\.css$' '\.html$' '\.js$' '\.py$' '\.csv$' '\.json$' '\.txt$' '\.sql')
#######################################################################################################################
for pattern in ${sections[@]}; do
echo ___________________________________________
echo $pattern
crunch $pattern
done
echo ===========================================
echo NONE OF THE ABOVE PATTERNS
for i in ${!sections[@]}; do
# modify the array to wrap each pattern in parens (a bit dodgy)
sections[$i]="\(${sections[$i]}\)"
done
join_by () { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }
crunch $(join_by '\|' ${sections[@]}) -v
# print all the files that didn't match any of the section patterns with this
# cat $FILE_LIST | grep -v $(join_by '\|' ${sections[@]})
echo ===========================================
echo OVERALL
crunch '.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment