[FUN] Get some stats of your project/s

Heya

Ever wanted to have some basic stats of your projects?
Like:

./stats.sh 
########################################
	Project stats for "tui"
########################################
260 kb	in bin
24 kb	in conf.etc
12 kb	in conf.home
32 kb	in docs/samples
176 kb	in docs/wiki
280 kb	in docs
192 kb	in man
1984 kb	in screenshots
12 kb	in templates/manpage
32 kb	in templates/scripts
12 kb	in templates/usr
64 kb	in templates
36 kb	in themes
13 folders with a total of 3116 kbytes
########################################
Spread across 190 files, there are:
Lines Total: 		 14542
Comment lines: 		 2409
Blank lines: 		 201
Avrg lines p. file: 	 76

Was created using these few lines:

#!/bin/bash
#
#	Vars
#
	LINER="########################################"
	COMMENTS=0
	LINES=0
	BLANKS=0
#
#	Action
#
	echo "$LINER"
	echo -e "\tProject stats for \"$(basename $PWD)\""
# Size
	echo "$LINER"
	for DIR in * ; do [ -d "$DIR" ] && LIST+=" $DIR" ; done
	du $LIST  | awk '{print $1" kb\tin "$2;SUM=SUM+$1} END {print NR" folders with a total of "SUM" kbytes"}'
# Files
	echo "$LINER"
	FILES=$(find|grep -ve ".git" -ve ".jpg"| wc -l)
	echo "Spread across $FILES files, there are:"
# Lines
	for F in $(find|grep -ve ".git" -ve ".jpg")
	do	[ -f "$F" ] && \
			COMMENTS=$(( $COMMENTS + $(grep ^"#" "$F" | wc -l) )) && \
			LINES=$(( $LINES + $(cat "$F" | wc -l) )) && \
			BLANKS=$(( $BLANKS + $(grep ^[[:space:]]$ "$F" | wc -l) ))
	done
# Summary
	echo -e "Lines Total: \t\t $LINES"
	echo -e "Comment lines: \t\t $COMMENTS"
	echo -e "Blank lines: \t\t $BLANKS"
	echo -e "Avrg lines p. file: \t $(( $LINES / $FILES ))"
	

I wanted to share it when i wrote it, but figured i didnt.

Have fun

You could replace your Lines section with the below for more speed:

# Lines
vals=( $( awk -v q="^#,.*,^[[:space:]]*$" '
       BEGIN{cc=split(q,crit,",")}
       { for(i in crit) t+=($0 ~ crit) }
       END { for(i=1; i<=cc;i++) printf "%d ",t }' \
       $(find . -type f -name "st*") )
)
((COMMENTS=vals[0]))
((LINES=vals[1]))
((BLANKS=vals[2]))
1 Like

Limiting the files found to only files starting with st* is alot faster, due to a smaller list size.

After changing the find line to:

 $(find . -type f -name "*"|grep -ve jpg$ -ve png$ -ve svg$ -ve \.git) )

Its printing (almost) the same stats in quite near range (imo, regarding the task done)....
Org:

real	0m0.581s
user	0m0.195s
sys	0m0.742s

New:

real	0m0.402s
user	0m0.385s
sys	0m0.026s

Only value changed is 'blank lines', raised from ~200 to ~1800. :confused:
Eventhough i've (theoreticly) removed more image files - which shouldnt contain empty lines anyway....

I wonder if you want to skip .git directories.
Then a -prune in find is more efficient.
Also it is more efficient to run the find once, and count the FILES in the loop.

FILES=0
for F in $(find . -type d -name ".git" -prune -o -type f \! -name ".jpg" -print)
do
    ((FILES+=1))
    ... # only files, dont need to test again
done
 

NB the -print is needed: by explicitly printing the files it will not print the pruned directories.

1 Like

Just asking, are you aware that i created this thread with solved status?
Hence the [FUN] tag.

Anyway, for some reason the lines found jumped from 14541 to 22469, probably cause by the fact that the jpg files are still listed.

So to try it:

for F in $(find . -type d -name ".git" -prune -o -type f \! -name ".jpg" \! -name ".png" -print)
do	echo "$F"
	COMMENTS=$(( $COMMENTS + $(grep ^"#" "$F" | wc -l) ))
	LINES=$(( $LINES + $(cat "$F" | wc -l) ))
	BLANKS=$(( $BLANKS + $(grep ^[[:space:]]$ "$F" | wc -l) ))
	((FILES+=1))
done

Even with escaped "\.jpg" and/or unescaped "!", the screenshots are shown and their lines counted.
Ironicly, it reports only 2 files more... :confused:

Full output:

time ./stats.sh
########################################
	Project stats for "tui"
########################################
264 kb	in bin
24 kb	in conf.etc
12 kb	in conf.home
32 kb	in docs/samples
176 kb	in docs/wiki
280 kb	in docs
192 kb	in man
1984 kb	in screenshots
12 kb	in templates/manpage
32 kb	in templates/scripts
12 kb	in templates/usr
64 kb	in templates
36 kb	in themes
13 folders with a total of 3120 kbytes
########################################
./install.sh
./conf.etc/apps.conf
./conf.etc/status.conf
./conf.etc/commands.conf
./conf.etc/tui.conf
./conf.etc/colors.conf
./screenshots/examples-interface_ds_make_kickstart.jpg
./screenshots/tui-list.jpg
./screenshots/compare-themes-xterm.jpg
./screenshots/compare-themes-lxterminal.jpg
./screenshots/compare_with_root_mode.jpg
./screenshots/mini-demo-tui.jpg
./screenshots/tui-browser_textmode_root.jpg
./screenshots/tui-psm.jpg
./screenshots/demo_working_with_conf_files.jpg
./screenshots/tui-log.jpg
./screenshots/examlpes_tui-browser.jpg
./screenshots/tui-status.jpg
./screenshots/tui-printf_splitting.jpg
./screenshots/tui-browser_long.jpg
./screenshots/tui-select.jpg
./screenshots/demo-interface.jpg
./screenshots/tui-progress-examples.jpg
./docs/README.md
./docs/CONFIG.md
./docs/samples/demo-compare-with-bash.sh
./docs/samples/sample_tui-conf-get_and_set.sh
./docs/samples/sample_tui-progress.sh
./docs/samples/demo-interface-top-bottom.sh
./docs/samples/demo-interface-select-loop.sh
./docs/samples/sample_tui-status.sh
./docs/samples/sample_tui-log.sh
./docs/LICENSE
./docs/README-new.md
./docs/INSTALL.md
./docs/wiki/tui-conf-set.md
...
./man/tui-log.1
./man/tui-new-browser.1
./man/tui-new-manpage.1
./man/tui-str-usb.1
./man/tui-press.1
./man/tui-read.1
./man/tui-conf-get.1
...
./bin/tui-bol-gui
./bin/tui-install
./bin/tui-str-extension
./bin/tui-str-genfilename
./bin/tui-str-usb
./bin/tui-indi
Spread across 192 files, there are:
Lines Total: 		 22470
Comment lines: 		 2426
Blank lines: 		 206
Avrg lines p. file: 	 117

real	0m0.751s
user	0m0.290s
sys	0m0.848s

:confused:

Sorry for the confusion. It must be
\! -name "*.jpg" \! -name "*.png" .

1 Like