もた日記

くだらないことを真面目にやる

Linuxメモ : Rust製のtokeiでコードの行数、コメント行、空白行をカウント

tokei

github.com

Ruse製のtokeiはコードの行数、コメント行、空白行を高速にカウントしてくれるコマンドラインツール。
プログラミング言語毎のカウントや、ファイル単位のカウントが可能。

f:id:wonder-wall:20200404235657p:plain


インストール

README.mdのインストール方法によるとバイナリのダウンロードやcargoコマンドでインストールできる。

$ cargo install tokei

ヘルプメッセージ。

$ tokei --help
tokei 11.0.0 compiled without serialization formats.
Erin P. <xampprocky@gmail.com> + Contributors
Count your code, quickly.

USAGE:
    tokei [FLAGS] [OPTIONS] [--] [input]...

FLAGS:
    -f, --files               Will print out statistics on individual files.
    -h, --help                Prints help information
        --hidden              Count hidden files.
    -l, --languages           Prints out supported languages and their extensions.
        --no-ignore           Don't respect ignore files.
        --no-ignore-parent    Don't respect ignore files in parent directories.
        --no-ignore-vcs       Don't respect VCS (.gitignore, .hgignore, etc) ignore files.
    -V, --version             Prints version information
    -v, --verbose             Set log output level:
                                          1: to show unknown file extensions,
                                          2: reserved for future debugging,
                                          3: enable file level trace. Not recommended on multiple files

OPTIONS:
    -c, --columns <columns>       Sets a strict column width of the output, only available for terminal output.
    -e, --exclude <exclude>...    Ignore all files & directories matching the pattern.
    -i, --input <file_input>      Gives statistics from a previous tokei run. Can be given a file path, or "stdin" to
                                  read from stdin.
    -o, --output <output>         Outputs Tokei in a specific format. Compile with additional features for more format
                                  support. [possible values: cbor, json, yaml]
    -s, --sort <sort>             Sort languages based on column [possible values: files, lines, blanks, code, comments]
    -t, --type <types>            Filters output by language type, seperated by a comma. i.e. -t=Rust,Markdown

ARGS:
    <input>...    The path(s) to the file or directory to be counted.


使い方

対象ディレクトリを引数としてtokeiコマンドを実行すると、サブディレクトリ含めて行数をカウントしてくれる。
gitのリポジトリに対して実行したところ数秒で結果が表示された。

$ tokei ./git
-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 ASP.NET                 9         1083          942            0          141
 GNU Style Assembly      1          224           98          100           26
 Autoconf                9          128          108            3           17
 BASH                    1         3381         2909          203          269
 Batch                   3          287          117          118           52
 C                     477       278177       213912        28515        35750
 C Header              223        36117        18253        13075         4789
 CSS                     2          982          783           30          169
 Emacs Lisp              2           12           12            0            0
 Go                      4          558          421           84           53
 JavaScript              7         1613          779          631          203
 Makefile               24         5275         3739          805          731
 Markdown                4          347          347            0            0
 Perl                   49        14023        10814         1183         2026
 PHP                     2          249          123           82           44
 Python                  5         9018         6772          681         1565
 ReStructuredText        6         1142         1142            0            0
 Ruby                    1           48           43            0            5
 Shell                1074       251945       206789        11367        33789
 TCL                    41        13124        10818          637         1669
 Plain Text            719       105847       105847            0            0
 XSL                     9          154          102           33           19
 YAML                    1          558          510            3           45
 Zsh                     1          244          189           18           37
-------------------------------------------------------------------------------
 Total                2674       724536       585569        57568        81399
-------------------------------------------------------------------------------

-sまたは--sortオプションでカラムを指定してソートできる。

$ tokei ./git -s Code
-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 C                     477       278177       213912        28515        35750
 Shell                1074       251945       206789        11367        33789
 Plain Text            719       105847       105847            0            0
 C Header              223        36117        18253        13075         4789
 TCL                    41        13124        10818          637         1669
 Perl                   49        14023        10814         1183         2026
 Python                  5         9018         6772          681         1565
 Makefile               24         5275         3739          805          731
 BASH                    1         3381         2909          203          269
 ReStructuredText        6         1142         1142            0            0
 ASP.NET                 9         1083          942            0          141
 CSS                     2          982          783           30          169
 JavaScript              7         1613          779          631          203
 YAML                    1          558          510            3           45
 Go                      4          558          421           84           53
 Markdown                4          347          347            0            0
 Zsh                     1          244          189           18           37
 PHP                     2          249          123           82           44
 Batch                   3          287          117          118           52
 Autoconf                9          128          108            3           17
 XSL                     9          154          102           33           19
 GNU Style Assembly      1          224           98          100           26
 Ruby                    1           48           43            0            5
 Emacs Lisp              2           12           12            0            0
-------------------------------------------------------------------------------
 Total                2674       724536       585569        57568        81399
-------------------------------------------------------------------------------

ファイル単位でカウントしたい場合は-fまたは--filesオプションを指定する。
-tオプションは言語の指定、-cオプションは出力幅の指定。

$ tokei -f -t Python,CSS -c 100 ./git/
----------------------------------------------------------------------------------------------------
 Language                                 Files        Lines         Code     Comments       Blanks
----------------------------------------------------------------------------------------------------
 CSS                                          2          982          783           30          169
----------------------------------------------------------------------------------------------------
 ./git/Documentation/docbook-xsl.css                     296          243           10           43
 ./git/gitweb/static/gitweb.css                          441          540           20          126
----------------------------------------------------------------------------------------------------
 Python                                       5         9018         6772          681         1565
----------------------------------------------------------------------------------------------------
 ./git/git-p4.py                                        4253         3231          355          686
 ./git/contrib/fast-import/import-zips.py                 61           53            9           16
 ./git/contrib/hg-to-git/hg-to-git.py                    252          181           30           43
 ./git/contrib/svn-fe/svnrdump_sim.py                     67           59            2            7
 ./git/contrib/hooks/multimail/git_multimail.py         4344         3248          285          813
----------------------------------------------------------------------------------------------------
 Total                                        7        10000         7555          711         1734
----------------------------------------------------------------------------------------------------

サポート言語

サポート言語は-lまたは--languagesオプションで確認できる(またはREADME.md)。

$ tokei -l | wc -l
198
$ tokei -l | head
ABAP
ActionScript
Ada
Agda
Alex
ASN.1
ASP
ASP.NET
Assembly
GNU Style Assembly