Tokei是一个按语言统计代码行数等统计信息的工具。其实这些信息还是非常有用的,首先程序猿可以用它来估计别人的实力或者观察自己实力成长的速度(神马,行数论英雄么?)。其次,管理人员可以用它来估计工程的进度和预算(据说一个程序猿一天写不出100行代码?)。最后,其实在看新代码的时候先看下这些数据,也有助于对代码的理解(说不定最长的文件就是核心逻辑的所在处呢?)。所以,大家以后没事多tokei一下呀。
好了不废话,首先安装的话直接用cargo就好了:
$ cargo install tokei
我们下面以cpython的源码为例,看下tokei具体如何使用
直接使用tokei+目录名就可以得到关于目录下按语言分类的文件数、代码行数(包括代码和注释)和空行数。
------------------------------------------------------------------------------- Language Files Lines CodeComments Blanks ------------------------------------------------------------------------------- Assembly6 1978 1723 26229 Autoconf13 4493 2708939846 Batch 32 2120 1762 7351 C 317 421274 320386 48459 52429 C Header 384 181963 156295 10240 15428 C Shell 1 25 11 5 9 C++ 5 4223 3241256726 CSS 1 6 0 4 2 D 5 83 74 1 8 Fish1 64 38 13 13 HTML 10 2045 1926 11108 INI 1171102 27 42 JavaScript 2209177 14 18 JSON6 68 68 0 0 Lisp1692502 81109 Makefile4339242 40 57 Markdown2 47 47 0 0 Module-Definition 8 1400 1363 14 23 MSBuild10995824 80 91 Objective-C 7794635 61 98 PowerShell 6600345171 84 Prolog 1 24 24 0 0 Python1943 814052 636398 58030 119624 ReStructuredText645 322625 322625 0 0 Shell 5797487191119 SVG 8 8 8 0 0 Plain Text 149 89499 89499 0 0 TOML1 4 4 0 0 VBScript1 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 XML58417338 7 72 ------------------------------------------------------------------------------- Total368418587691549551 118686 190532 -------------------------------------------------------------------------------$ tokei cpython/
使用-s可以针对某个列排序,比如如果我们要针对代码行数排序
------------------------------------------------------------------------------- Language Files Lines CodeComments Blanks ------------------------------------------------------------------------------- Python1943 814052 636398 58030 119624 ReStructuredText645 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 3241256726 Autoconf13 4493 2708939846 HTML 10 2045 1926 11108 Batch 32 2120 1762 7351 Assembly6 1978 1723 26229 Visual Studio Sol|2 1472 1471 0 1 Module-Definition 8 1400 1363 14 23 MSBuild10995824 80 91 Objective-C 7794635 61 98 Lisp1692502 81109 Shell 5797487191119 PowerShell 6600345171 84 XML58417338 7 72 Makefile4339242 40 57 JavaScript 2209177 14 18 INI 1171102 27 42 D 5 83 74 1 8 JSON6 68 68 0 0 Markdown2 47 47 0 0 Fish1 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 TOML1 4 4 0 0 CSS 1 6 0 4 2 VBScript1 1 0 1 0 ------------------------------------------------------------------------------- Total368418587691549551 118686 190532 -------------------------------------------------------------------------------$ tokei cpython/ -s code
使用-t可以过选择想要的语言
------------------------------------------------------------------------------- Language Files Lines CodeComments Blanks ------------------------------------------------------------------------------- Python1943 814052 636398 58030 119624 ------------------------------------------------------------------------------- Total1943 814052 636398 58030 119624 -------------------------------------------------------------------------------$ tokei cpython/ -t=Python
也可以同时选择多个语言,使用逗号分开
------------------------------------------------------------------------------- Language Files Lines CodeComments Blanks ------------------------------------------------------------------------------- C++ 5 4223 3241256726 Python1943 814052 636398 58030 119624 ------------------------------------------------------------------------------- Total1948 818275 639639 58286 120350 -------------------------------------------------------------------------------$ tokei cpython/ -t=Python,C++
使用-e可以过滤掉不想要进入统计的文件名类型,比如说我们过滤掉所有拓展名是c的文件
------------------------------------------------------------------------------- Language Files Lines CodeComments Blanks ------------------------------------------------------------------------------- Assembly6 1978 1723 26229 Autoconf13 4493 2708939846 Batch 32 2120 1762 7351 C Header 384 181963 156295 10240 15428 C Shell 1 25 11 5 9 C++ 5 4223 3241256726 CSS 1 6 0 4 2 D 5 83 74 1 8 Fish1 64 38 13 13 HTML 10 2045 1926 11108 INI 1171102 27 42 JavaScript 2209177 14 18 JSON6 68 68 0 0 Lisp1692502 81109 Makefile4339242 40 57 Markdown2 47 47 0 0 Module-Definition 8 1400 1363 14 23 MSBuild10995824 80 91 Objective-C 7794635 61 98 PowerShell 6600345171 84 Prolog 1 24 24 0 0 Python1943 814052 636398 58030 119624 ReStructuredText645 322625 322625 0 0 Shell 5797487191119 SVG 8 8 8 0 0 Plain Text 149 89499 89499 0 0 TOML1 4 4 0 0 VBScript1 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 XML58417338 7 72 ------------------------------------------------------------------------------- Total336714374951229165 70227 138103 -------------------------------------------------------------------------------$ tokei cpython/ -e *.c
也可以使用--files使得统计按照文件显示而不是合并显示。因为cpython的文件太多,我们选取一个子目录看看:
------------------------------------------------------------------------------------------------------------------------- Language Files Lines CodeComments Blanks ------------------------------------------------------------------------------------------------------------------------- C9 3860 3158347355 ------------------------------------------------------------------------------------------------------------------------- cpython/Parser/listnode.c 70 60 2 8 cpython/Parser/token.c243233 3 7 cpython/Parser/myreadline.c380295 37 48 cpython/Parser/parsetok.c 495414 25 56 cpython/Parser/grammar1.c 47 38 3 6 cpython/Parser/parser.c 462336 61 65 cpython/Parser/acceler.c 123105 10 8 cpython/Parser/tokenizer.c1852 1550163139 cpython/Parser/node.c188127 43 18 ------------------------------------------------------------------------------------------------------------------------- C Header 2137101 15 21 ------------------------------------------------------------------------------------------------------------------------- cpython/Parser/parser.h49 37 2 10 cpython/Parser/tokenizer.h 88 64 13 11 ------------------------------------------------------------------------------------------------------------------------- Python 10 2898 2369111418 ------------------------------------------------------------------------------------------------------------------------- cpython/Parser/asdl.py376281 41 54 cpython/Parser/asdl_c.py 1417 1224 17176 cpython/Parser/pgen/automata.py371280 20 71 cpython/Parser/pgen/token.py38 22 4 12 cpython/Parser/pgen/pgen.py305255 8 42 cpython/Parser/pgen/metaparser.py 152115 21 16 cpython/Parser/pgen/__init__.py 0 0 0 0 cpython/Parser/pgen/grammar.py 147124 0 23 cpython/Parser/pgen/keywordgen.py59 42 0 17 cpython/Parser/pgen/__main__.py 33 26 0 7 ------------------------------------------------------------------------------------------------------------------------- Total21 6895 5628473794 -------------------------------------------------------------------------------------------------------------------------$ tokei cpython/Parser/ --files
最后也可以把刚才这些组合起来,比如
------------------------------------------------------------------------------------------------------------------------- Language Files Lines CodeComments Blanks ------------------------------------------------------------------------------------------------------------------------- Python9 2898 2369111418 ------------------------------------------------------------------------------------------------------------------------- cpython/Parser/asdl_c.py 1417 1224 17176 cpython/Parser/asdl.py376281 41 54 cpython/Parser/pgen/automata.py371280 20 71 cpython/Parser/pgen/pgen.py305255 8 42 cpython/Parser/pgen/grammar.py 147124 0 23 cpython/Parser/pgen/metaparser.py 152115 21 16 cpython/Parser/pgen/keywordgen.py59 42 0 17 cpython/Parser/pgen/__main__.py 33 26 0 7 cpython/Parser/pgen/token.py38 22 4 12 ------------------------------------------------------------------------------------------------------------------------- Total 9 2898 2369111418 -------------------------------------------------------------------------------------------------------------------------$ tokei cpython/Parser/ --files -t=Python -s code -e __init__.py
最后,其实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一下你的代码吧~