⌚️

tokei で行数を測ってみよう

に公開2

tokei で行数を測ってみよう

tokei https://github.com/XAMPPRocky/tokei はコードの統計情報を表示するコマンドラインツールです。 Rust で書かれています。

コードの統計情報を表示するので「統計」かと思いきや「時計」と、リポジトリの README に書いてあります。

行数なんて……と思われるかもしれませんが、規模感のある程度の目安にはなります。あるリポジトリがあったとき 100 万行と 100 行ではさすがに違いますよね (もちろん自動生成された何かという可能性はありますが) 。

インストール

いくつかの方法でインストールできますが、 Rust の開発環境があるなら cargo install でインストールできます。

$ cargo install tokei

使い方

試しに dtolnay/thiserror に対して実行してみます。

$ ghq get dtolnay/thiserror
     clone https://github.com/dtolnay/thiserror -> /home/bouzuya/ghq/github.com/dtolnay/thiserror
       git clone --recursive https://github.com/dtolnay/thiserror /home/bouzuya/ghq/github.com/dtolnay/thiserror
Cloning into '/home/bouzuya/ghq/github.com/dtolnay/thiserror'...
remote: Enumerating objects: 3792, done.
remote: Counting objects: 100% (1192/1192), done.
remote: Compressing objects: 100% (200/200), done.
remote: Total 3792 (delta 1106), reused 1000 (delta 992), pack-reused 2600 (from 2)
Receiving objects: 100% (3792/3792), 1.01 MiB | 1.27 MiB/s, done.
Resolving deltas: 100% (2379/2379), done.

$ cd /home/bouzuya/ghq/github.com/dtolnay/thiserror

$ tokei
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 TOML                    4           90           68           11           11
-------------------------------------------------------------------------------
 Markdown                1          154            0          106           48
 |- Rust                 1           82           74            3            5
 |- TOML                 1            2            2            0            0
 (Total)                            238           76          109           53
-------------------------------------------------------------------------------
 Rust                   68         4932         4189          161          582
 |- Markdown             1          257          106          110           41
 (Total)                           5189         4295          271          623
===============================================================================
 Total                  73         5176         4257          278          641
===============================================================================

計測したいディレクトリで tokei を実行すれば良いです。引数にディレクトリを指定することもできます。

言語・ファイル数・行数・コード行・コメント行・空行がテーブルの形でそれぞれ表示されます。

いろいろなオプション

いろいろオプションがあります。 tokei --help で確認できます。

tokei 12.1.2 compiled with serialization support: json
Erin P. <xampprocky@gmail.com> + Contributors
Count your code, quickly.
Support this project on GitHub Sponsors: https://github.com/sponsors/XAMPPRocky

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

FLAGS:
    -C, --compact             Do not print statistics about embedded languages.
    -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 (.gitignore, .ignore, etc.). This implies --no-ignore-parent,
                              --no-ignore-dot, and --no-ignore-vcs.
        --no-ignore-dot       Don't respect .ignore and .tokeignore files, including those in parent directories.
        --no-ignore-parent    Don't respect ignore files (.gitignore, .ignore, etc.) in parent directories.
        --no-ignore-vcs       Don't respect VCS ignore files (.gitignore, .hgignore, etc.), including those in parent
                              directories.
    -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.
    -n, --num-format <num_format_style>    Format of printed numbers, i.e. plain (1234, default), commas (1,234), dots
                                           (1.234), or underscores (1_234). Cannot be used with --output. [possible
                                           values: commas, dots, plain, underscores]
    -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.

個人的にはこった使い方をしていないので、書くことはないですねー。

一応 --compact でもうすこしシンプルな結果を得たり、 --output json で出力して jq で……なんてこともできますね。

$ tokei --output json | jq '.Rust | .blanks + .code + .comments'
4932

おわり

thiserror crate についての記事を書く前に行数を測ったので、ついでに tokei を紹介してみました。

GitHubで編集を提案
ドクターメイト

Discussion

kanaruskanarus

tokei https://github.com/XAMPPRocky/tokei は Rust で書かれたコードの統計情報を表示するコマンドラインツールです。

この書き方だと、知らない人からすると「tokei は Rust コードベース専用の統計ツール」と読めてしまうと思います。

その後の実行例で出てくる Language が Rust と TOML と Markdown なので、「やっぱり Rust コードベース専用のツールなんだな」という認識が補強されることになりかねないと思います。

bouzuyabouzuya

コメントありがとうございます! 確かにそうかもしれません。書き直してみました。