Skip to content

Instantly share code, notes, and snippets.

@yougg
Created August 17, 2018 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yougg/48d98866bb149def022d4ed115b803dd to your computer and use it in GitHub Desktop.
Save yougg/48d98866bb149def022d4ed115b803dd to your computer and use it in GitHub Desktop.
shell count code lines in the directory
#!/bin/bash
if [ -n "${1}" ]; then
WD=${1}
else
WD=$(cd `dirname $0`/..;pwd)
fi
# find all text file extensions which not in the dir: .idea/.git/vendor/thirdparty/sdk/swagger
exts=$(find ${WD} -type f -not -path "*.idea*" -not -path "*.git*" -not -path "*/sdk/*" -not -path "*rd*arty*" -not -path "*vendor*" -not -path "*swagger*" -exec grep -Iq . {} \; -and -print 2> /dev/null | awk -F '.' '{if ($NF!~/\//){print $NF}}' | sort | uniq)
printf "\e[1;4;33;40m| %-10s|%10s |%10s |%10s |%12s |\e[0m\n" "File type" "Files" "Comments" "Lines" "Third party"
bgcolor=40
mold=1
for ext in $(echo ${exts}); do
# count lines for each type source code without empty lines
files=$(find ${WD} -name "*.${ext}" -type f -not -path "*.idea*" -not -path "*.git*" -not -path "*/sdk/*" -not -path "*rd*arty*" -not -path "*vendor*" -not -path "*swagger*" 2> /dev/null | wc -l)
if [ ${files} -le 2 ]; then
# ignore ext which files count less equal 2
continue
fi
lines=$(find ${WD} -name "*.${ext}" -type f -not -path "*.idea*" -not -path "*.git*" -not -path "*/sdk/*" -not -path "*rd*arty*" -not -path "*vendor*" -not -path "*swagger*" 2> /dev/null | xargs -d '\n' cat | sed '/^\s*$/d' | wc -l)
comment=$(find ${WD} -name "*.${ext}" -type f -not -path "*.idea*" -not -path "*.git*" -not -path "*/sdk/*" -not -path "*rd*arty*" -not -path "*vendor*" -not -path "*swagger*" 2> /dev/null | xargs -d '\n' grep -E '^[ \t]*(#|//|--|::|REM|/\*|<!--)' | wc -l)
others=$(find ${WD} -name "*.${ext}" -type f \( -path "*/sdk/*" -o -path "*rd*arty*" -o -path "*vendor*" -o -path "*swagger*" \) 2> /dev/null | xargs -d '\n' cat | sed '/^\s*$/d' | wc -l)
printf "\e[33;%dm| %-10s|%10d |%10s |%10d |%12d |\e[0m\n" $(($mold%2*4+$bgcolor)) ${ext} ${files} ${comment} ${lines} ${others}
mold=$(($mold+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment