< 返回版块

黑豆腐 发表于 2019-12-13 09:00

Tags:rust

Tokei是一个按语言统计代码行数等统计信息的工具。其实这些信息还是非常有用的,首先程序猿可以用它来估计别人的实力或者观察自己实力成长的速度(神马,行数论英雄么?)。其次,管理人员可以用它来估计工程的进度和预算(据说一个程序猿一天写不出100行代码?)。最后,其实在看新代码的时候先看下这些数据,也有助于对代码的理解(说不定最长的文件就是核心逻辑的所在处呢?)。所以,大家以后没事多tokei一下呀。

好了不废话,首先安装的话直接用cargo就好了:

$ cargo install tokei

我们下面以cpython的源码为例,看下tokei具体如何使用

直接使用tokei+目录名就可以得到关于目录下按语言分类的文件数、代码行数(包括代码和注释)和空行数。

$ tokei cpython/
-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 Assembly                6         1978         1723           26          229
 Autoconf               13         4493         2708          939          846
 Batch                  32         2120         1762            7          351
 C                     317       421274       320386        48459        52429
 C Header              384       181963       156295        10240        15428
 C Shell                 1           25           11            5            9
 C++                     5         4223         3241          256          726
 CSS                     1            6            0            4            2
 D                       5           83           74            1            8
 Fish                    1           64           38           13           13
 HTML                   10         2045         1926           11          108
 INI                     1          171          102           27           42
 JavaScript              2          209          177           14           18
 JSON                    6           68           68            0            0
 Lisp                    1          692          502           81          109
 Makefile                4          339          242           40           57
 Markdown                2           47           47            0            0
 Module-Definition       8         1400         1363           14           23
 MSBuild                10          995          824           80           91
 Objective-C             7          794          635           61           98
 PowerShell              6          600          345          171           84
 Prolog                  1           24           24            0            0
 Python               1943       814052       636398        58030       119624
 ReStructuredText      645       322625       322625            0            0
 Shell                   5          797          487          191          119
 SVG                     8            8            8            0            0
 Plain Text            149        89499        89499            0            0
 TOML                    1            4            4            0            0
 VBScript                1            1            0            1            0
 Visual Studio Pro|     48         6276         6223            8           45
 Visual Studio Sol|      2         1472         1471            0            1
 XSL                     1            5            5            0            0
 XML                    58          417          338            7           72
-------------------------------------------------------------------------------
 Total                3684      1858769      1549551       118686       190532
-------------------------------------------------------------------------------

使用-s可以针对某个列排序,比如如果我们要针对代码行数排序

$ tokei cpython/ -s code
-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 Python               1943       814052       636398        58030       119624
 ReStructuredText      645       322625       322625            0            0
 C                     317       421274       320386        48459        52429
 C Header              384       181963       156295        10240        15428
 Plain Text            149        89499        89499            0            0
 Visual Studio Pro|     48         6276         6223            8           45
 C++                     5         4223         3241          256          726
 Autoconf               13         4493         2708          939          846
 HTML                   10         2045         1926           11          108
 Batch                  32         2120         1762            7          351
 Assembly                6         1978         1723           26          229
 Visual Studio Sol|      2         1472         1471            0            1
 Module-Definition       8         1400         1363           14           23
 MSBuild                10          995          824           80           91
 Objective-C             7          794          635           61           98
 Lisp                    1          692          502           81          109
 Shell                   5          797          487          191          119
 PowerShell              6          600          345          171           84
 XML                    58          417          338            7           72
 Makefile                4          339          242           40           57
 JavaScript              2          209          177           14           18
 INI                     1          171          102           27           42
 D                       5           83           74            1            8
 JSON                    6           68           68            0            0
 Markdown                2           47           47            0            0
 Fish                    1           64           38           13           13
 Prolog                  1           24           24            0            0
 C Shell                 1           25           11            5            9
 SVG                     8            8            8            0            0
 XSL                     1            5            5            0            0
 TOML                    1            4            4            0            0
 CSS                     1            6            0            4            2
 VBScript                1            1            0            1            0
-------------------------------------------------------------------------------
 Total                3684      1858769      1549551       118686       190532
-------------------------------------------------------------------------------

使用-t可以过选择想要的语言

$ tokei cpython/ -t=Python
-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 Python               1943       814052       636398        58030       119624
-------------------------------------------------------------------------------
 Total                1943       814052       636398        58030       119624
-------------------------------------------------------------------------------

也可以同时选择多个语言,使用逗号分开

$ tokei cpython/ -t=Python,C++
-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 C++                     5         4223         3241          256          726
 Python               1943       814052       636398        58030       119624
-------------------------------------------------------------------------------
 Total                1948       818275       639639        58286       120350
-------------------------------------------------------------------------------

使用-e可以过滤掉不想要进入统计的文件名类型,比如说我们过滤掉所有拓展名是c的文件

$ tokei cpython/ -e *.c
-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 Assembly                6         1978         1723           26          229
 Autoconf               13         4493         2708          939          846
 Batch                  32         2120         1762            7          351
 C Header              384       181963       156295        10240        15428
 C Shell                 1           25           11            5            9
 C++                     5         4223         3241          256          726
 CSS                     1            6            0            4            2
 D                       5           83           74            1            8
 Fish                    1           64           38           13           13
 HTML                   10         2045         1926           11          108
 INI                     1          171          102           27           42
 JavaScript              2          209          177           14           18
 JSON                    6           68           68            0            0
 Lisp                    1          692          502           81          109
 Makefile                4          339          242           40           57
 Markdown                2           47           47            0            0
 Module-Definition       8         1400         1363           14           23
 MSBuild                10          995          824           80           91
 Objective-C             7          794          635           61           98
 PowerShell              6          600          345          171           84
 Prolog                  1           24           24            0            0
 Python               1943       814052       636398        58030       119624
 ReStructuredText      645       322625       322625            0            0
 Shell                   5          797          487          191          119
 SVG                     8            8            8            0            0
 Plain Text            149        89499        89499            0            0
 TOML                    1            4            4            0            0
 VBScript                1            1            0            1            0
 Visual Studio Pro|     48         6276         6223            8           45
 Visual Studio Sol|      2         1472         1471            0            1
 XSL                     1            5            5            0            0
 XML                    58          417          338            7           72
-------------------------------------------------------------------------------
 Total                3367      1437495      1229165        70227       138103
-------------------------------------------------------------------------------

也可以使用--files使得统计按照文件显示而不是合并显示。因为cpython的文件太多,我们选取一个子目录看看:

$ tokei cpython/Parser/ --files
-------------------------------------------------------------------------------------------------------------------------
 Language                                                      Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------------------------------------------------
 C                                                                 9         3860         3158          347          355
-------------------------------------------------------------------------------------------------------------------------
 cpython/Parser/listnode.c                                                     70           60            2            8
 cpython/Parser/token.c                                                       243          233            3            7
 cpython/Parser/myreadline.c                                                  380          295           37           48
 cpython/Parser/parsetok.c                                                    495          414           25           56
 cpython/Parser/grammar1.c                                                     47           38            3            6
 cpython/Parser/parser.c                                                      462          336           61           65
 cpython/Parser/acceler.c                                                     123          105           10            8
 cpython/Parser/tokenizer.c                                                  1852         1550          163          139
 cpython/Parser/node.c                                                        188          127           43           18
-------------------------------------------------------------------------------------------------------------------------
 C Header                                                          2          137          101           15           21
-------------------------------------------------------------------------------------------------------------------------
 cpython/Parser/parser.h                                                       49           37            2           10
 cpython/Parser/tokenizer.h                                                    88           64           13           11
-------------------------------------------------------------------------------------------------------------------------
 Python                                                           10         2898         2369          111          418
-------------------------------------------------------------------------------------------------------------------------
 cpython/Parser/asdl.py                                                       376          281           41           54
 cpython/Parser/asdl_c.py                                                    1417         1224           17          176
 cpython/Parser/pgen/automata.py                                              371          280           20           71
 cpython/Parser/pgen/token.py                                                  38           22            4           12
 cpython/Parser/pgen/pgen.py                                                  305          255            8           42
 cpython/Parser/pgen/metaparser.py                                            152          115           21           16
 cpython/Parser/pgen/__init__.py                                                0            0            0            0
 cpython/Parser/pgen/grammar.py                                               147          124            0           23
 cpython/Parser/pgen/keywordgen.py                                             59           42            0           17
 cpython/Parser/pgen/__main__.py                                               33           26            0            7
-------------------------------------------------------------------------------------------------------------------------
 Total                                                            21         6895         5628          473          794
-------------------------------------------------------------------------------------------------------------------------

最后也可以把刚才这些组合起来,比如

$ tokei cpython/Parser/ --files -t=Python -s code -e __init__.py
-------------------------------------------------------------------------------------------------------------------------
 Language                                                      Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------------------------------------------------
 Python                                                            9         2898         2369          111          418
-------------------------------------------------------------------------------------------------------------------------
 cpython/Parser/asdl_c.py                                                    1417         1224           17          176
 cpython/Parser/asdl.py                                                       376          281           41           54
 cpython/Parser/pgen/automata.py                                              371          280           20           71
 cpython/Parser/pgen/pgen.py                                                  305          255            8           42
 cpython/Parser/pgen/grammar.py                                               147          124            0           23
 cpython/Parser/pgen/metaparser.py                                            152          115           21           16
 cpython/Parser/pgen/keywordgen.py                                             59           42            0           17
 cpython/Parser/pgen/__main__.py                                               33           26            0            7
 cpython/Parser/pgen/token.py                                                  38           22            4           12
-------------------------------------------------------------------------------------------------------------------------
 Total                                                             9         2898         2369          111          418
-------------------------------------------------------------------------------------------------------------------------

最后,其实tokei是个日语词,意思是钟表,也许作者是在暗示这款工具运行地速度飞快吧。实际上在github中有和其他类似工具(Go的scc以及同样用Rust写的loc)的比较。但比较的版本有些老了。

我们就自己跑一下看看统计当前Rust官方代码库的成绩(CPU时间)吧:

tokei 1.5±0.1s

scc 2.3±1.0s

loc 3.8±0.4s

tokei是不是很优秀?不过也别太当真啦,反正都是一眨眼的事情。没事多tokei一下你的代码吧~

评论区

写评论

还没有评论

1 共 0 条评论, 1 页