Counts of lines of code generated are used or abused for many purposes. There is no standard way of making these counts.
Use one-liner Python functions for counting lines of code in an external file of code. The code has no external dependencies. The code can be in any programming language. The examples below use a file written in Elisp.
The last example is the solution that you want most of the time. The bash function at the bottom can be available everywhere and all of the time.
Python3 one-liner to count number of non-blank lines follows. This code will count the number of non-blank lines:
print(sum(1 for line in open("init.el",'r') if line.strip()))
Python3 one-liner to count the number of lines that are commented out follows. The semicolon is used to comment out lines in Emacs Lisp. This function will miss commented lines that are indented, so this function could be improved.
print(sum(1 for line in open('init.el','r') if (line[0] == ';') ) )
Python3 one-liner to count the number of non-blank lines that are not commented out. This code overlooks block comments and indented comment lines. There is room for improvement.
This is the code that you want to use most of the time:
print(sum(1 for line in open('init.el','r') if ((line.strip()) and (line[0] != ';') ) ) )
You could make this into a bash function that is callable from anywhere on any kind of code. Beware that you may have to escape the comment character if it is part of the Bash syntax. Examples of usage include:
cntloc test.py \#
cntloc init.el \;
cntloc .bashFunctions \#
cntloc rhoxyz.f \*
cntloc rhoxyz.f95 \!
cntloc ancientCode.f C
cntloc learnjuia.jl \#
cntloc my_model.stan \/
cntloc testme.c \/
cntloc test.cjl \;
cntloc test.R \#
This function can be stored in a .bashFunctions
file that is sourced when you open a new bash or zsh shell.
cntloc()
{
echo "Count lines of code in almost any plain text file. Will not handle block comments correctly."
echo "You may have to escape the comment character if it is part of the bash syntax."
if [ $# -lt 2 ]; then
echo 1>&2 "$0: not enough arguments"
s='Supply the filename and the first comment character (e.g., # / % ; C * !)'
echo "${s//;\; //#\# ///\/ //%\% //!\!}"
t='Usage: cntloc init.el \;'
echo "${t//;\; //#\# ///\/ //%\% //!\!}"
return 2
elif [ $# -gt 2 ]; then
echo 1>&2 "$0: too many arguments"
s='Supply the filename and the first comment character (e.g., # / % ; C * !)'
echo "${s//;\; //#\# ///\/ //%\%}"
t='Usage: cntloc init.el \;'
echo "${t//;\; //#\# ///\/ //%\%}"
fi
/opt/local/bin/python3.11 -c "print(sum(1 for line in open('$1','r') if ( (line.strip()) and (line[0] != '$2') ) ))"
}
- Skips comments in blocks (e.g., triple quote blocks in Python).
- Skips comments on indented lines.
- Skips blocked comments in special comment environments (e.g., Python triple quoted blocks, HTML comment blocks, LaTeX comment blocks, comment blocks in C++).
- Skip comments on indented lines
Version | Changes | Date |
---|---|---|
Version 0.2 | Fixed typos in README.md | 2024 April 10 |
- NIH: R01 CA242845
- NIH: R01 AI088011
- NIH: P30 CA225520 (PI: R. Mannel)
- NIH P20GM103640 and P30GM145423 (PI: A. West)