Cloc - 快速計算程式碼行數

最近剛好一個閒來無事

想看看專案規模成長到多大

雖然以程式碼行數來去看是不太準

但起碼依然是指標的一種

 

Cloc不但計算快速

且除了程式碼行數,還會列出檔案總數、空行數和註解行數

 

[icon name=“github” class="" unprefixed_class=""]GitHub: https://github.com/AlDanial/cloc


安裝

Cloc可以透過多種Package manager安裝:

1
2
3
4
5
6
7
8
9
npm install -g cloc                    # https://www.npmjs.com/package/cloc
sudo apt-get 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 pkg install cloc                  # FreeBSD
sudo port install cloc                 # Mac OS X with MacPorts
brew install cloc                      # Mac OS X with Homebrew
choco install cloc                     # Windows with Chocolatey

也可以直接下載Released package進行安裝 (下載頁面)

 


操作

指令:cloc [options] <file(s)/dir(s)> | <set 1> <set 2> |

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
Calos@Workstation:~/project/my-project$ cloc .
     538 text files.
     520 unique files.
     154 files ignored.

https://github.com/AlDanial/cloc v 1.66  T=2.61 s (182.4 files/s, 38389.0 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
PHP                            299           3141           1457          35339
JavaScript                      97           5996           5308          30628
CSS                             44            386            207          11262
SQL                              2             25             54           2732
HTML                            14            423             98           2699
JSON                            10              1              0            282
Bourne Shell                     8             21              2             77
make                             1             12             13             28
YAML                             1              0              0             10
-------------------------------------------------------------------------------
SUM:                           476          10005           7139          83057
-------------------------------------------------------------------------------

個人常用的參數有三種:

  • --exclude-dir: 排除特定目錄,以逗號隔開
  • --exclude-lang: 排除特定語言,以逗號隔開
  • --exclude-ext: 排除特定副檔名,以逗號隔開

完整參數說明請參考官方文件

 

以下範例為:

  1. 排除plugins、makefiles、storage三個特定目錄下所有檔案
  2. 排除make語言的所有檔案
  3. 排除副檔名為html的所有檔案
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Calos@Workstation:~/project/my-project$ cloc . --exclude-dir=plugins,makefiles,storage --exclude-lang=make --exclude-ext=html
     372 text files.
     364 unique files.
      63 files ignored.

https://github.com/AlDanial/cloc v 1.66  T=0.44 s (808.6 files/s, 102809.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
PHP                            286           2986           1390          34250
JavaScript                      39            604            286           4056
CSS                             28            230             23           1055
-------------------------------------------------------------------------------
SUM:                           353           3820           1699          39361
-------------------------------------------------------------------------------
Licensed under CC BY-NC-SA 3.0 TW
comments powered by Disqus