当前位置:网站首页>WC statistics are out of date, and every line of cloc code is valid

WC statistics are out of date, and every line of cloc code is valid

2022-06-23 11:46:00 Li XXXI

Context

We usually judge a programmer's skill or the size of a project by the amount of code , When reading the source code of a new project , You also need to know the amount of code , I have an expectation .

How to count the amount of code ?

Traditional practice

adopt find+wc Command complete

$ find . -name "*.go" | xargs wc -l

60 ./etcd/tutorial/distributed_lock.go
...
3491 total

Above , The statistics command displays all files , Altogether 3491 Line code . But the statistics here are too rough ,wc The statistical process will note 、 Blank lines and so on are counted as code statistics .

We should try more effective statistical methods .

Effective statistics cloc

cloc(Count Lines of Code[0]) It's a Perl Statistical tools of open source code for language development . Support for multiple platforms 、 Multilingual 、 Category counts the number of source code files in the target file or folder 、 Number of blank lines 、 Comment lines and code lines .

install

Support multiple installation methods , It includes almost all common installation methods .

npm install -g cloc              # https://www.npmjs.com/package/cloc
sudo apt install cloc            # Debian, Ubuntu
sudo yum install cloc            # Red Hat, Fedora
sudo dnf install cloc            # Fedora 22 or later
sudo pacman -S cloc              # Arch
sudo emerge -av dev-util/cloc    # Gentoo https://packages.gentoo.org/packages/dev-util/cloc
sudo apk add cloc                # Alpine Linux
doas pkg_add cloc                # OpenBSD
sudo pkg install cloc            # FreeBSD
sudo port install cloc           # macOS with MacPorts
brew install cloc                # macOS with Homebrew
choco install cloc               # Windows with Chocolatey
scoop install cloc               # Windows with Scoop

cloc advantage ?

cloc Easy to use 、 Scalable and portable features :

  • Easy to install , Just download the package and run it .
  • Support category statistics for multiple languages :Java、Go、Python、C etc. .
  • Support category statistics of results in multiple formats : Pure text 、SQL、JSON、XML、YAML、 Comma separated values, etc .
  • The number of files counted by category is supported 、 Number of blank lines 、 Comment lines and code lines .

Basic use

$ cloc ./     #  Statistical table of contents 
$ cloc perl-5.22.0.tar.gz #  You can also directly count compressed packets

For front-end students , Statistical code often needs to ignore the largest node_modules And packaged dist file , Support filtering logic , Easy to use .

$ cloc $(git ls-files) 
#  or
$ cloc --vcs git    
#  Or filtration  svn 
$ cloc --vcs svn

Advanced usage

It's easy to use , The statistical results will count the blank lines of the source code according to the language classification 、 Comment lines and physical lines . As follows :

Count individual files

#$ cloc docker-compose-devcontainer.yml

       1 text file.
       1 unique file.
       0 files ignored.

github.com/AlDanial/cloc v 1.92  T=0.01 s (140.6 files/s, 4919.9 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
YAML                             1              3              1             31
-------------------------------------------------------------------------------

Statistics folder

$ cloc .

      54 text files.
      42 unique files.
      14 files ignored.

github.com/AlDanial/cloc v 1.92  T=0.14 s (290.8 files/s, 22637.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Go                              29            381            216           2468
Markdown                         6             28              0             55
...
-------------------------------------------------------------------------------
SUM:                            42            431            220           2618
-------------------------------------------------------------------------------

It also supports the adoption of --exclude-dir Parameter to filter out paths that are not counted in statistics

$ cloc . --exclude-dir=build

      52 text files.
      40 unique files.
      14 files ignored.

github.com/AlDanial/cloc v 1.92  T=0.07 s (605.9 files/s, 48999.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Go                              29            381            216           2468
Markdown                         5             27              0             54
...
-------------------------------------------------------------------------------
SUM:                            40            423            217           2595
-------------------------------------------------------------------------------

Press is also supported --by-file Parameter to filter the statistical results .

$ cloc . --by-file | head -n 10

      54 text files.
      42 unique files.
      14 files ignored.

github.com/AlDanial/cloc v 1.92  T=0.03 s (1380.3 files/s, 107434.2 lines/s)
-----------------------------------------------------------------------------------------
File                                                  blank        comment           code
-----------------------------------------------------------------------------------------
./etcd/tutorial/demo.go                                  53              0            291
./etcd/simple_raft/raft_log.go                           29             93            263

Count multiple subdirectories under the folder

$ for d in ./*/ ; do (cd "$d" && echo "$d" && cloc .); done
./go-bootcamp/
    1474 text files.
    1226 unique files.
     540 files ignored.

github.com/AlDanial/cloc v 1.92  T=7.16 s (171.2 files/s, 118482.4 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                     421          10606          84539         584311
...
-------------------------------------------------------------------------------
SUM:                          1226          18182         100323         729998
-------------------------------------------------------------------------------
./html-bootcamp/
      18 text files.
      15 unique files.
       5 files ignored.

github.com/AlDanial/cloc v 1.92  T=0.02 s (635.5 files/s, 9744.9 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
HTML                             4             28              8             89
...
-------------------------------------------------------------------------------
SUM:                            15             51             15            164
-------------------------------------------------------------------------------
./java-bootcamp/
       3 text files.
       1 unique file.
       3 files ignored.

github.com/AlDanial/cloc v 1.92  T=0.01 s (104.8 files/s, 524.2 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Markdown                         1              2              0              3
-------------------------------------------------------------------------------

More orders

$ cloc --help

Supported languages

$ cloc --show-lang

adopt docker function

$ docker run --rm -v $PWD:/tmp aldanial/cloc ./

above , That's all for today , Contains cloc Installation and use . Of course , Important content tends to be small , Very recommended docker Operation mode .

️️️ Every reader's love is the driving force of the author's progress ! I am a Thirty-one [1], Thank you for : Please thumb up 、 Ask for comment 、 Please forward , See you next time !

References

  • [0] Count Lines of Code: https://github.com/AlDanial/cloc
  • [1] Thirty-one : http://www.lee31.cn/assets/image/ThirtyOneLee.jpeg

The articles

  • Introduction : from etcd The origin of the name , To install, deploy and use gestures
  • How to see Python(PyScript) Can do browser front-end development language ?
  • The depth of the summary , Take you around NVIDIA GPU
  • Still living in the last era ,Etcd 3.0 Implementing distributed locks is so simple
  • from 0 To 1, How to roll one with your bare hands Python Plug in system ?
  • One stop machine learning business platform MLflow What about? ?
原网站

版权声明
本文为[Li XXXI]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231131469296.html