Skip to content

How tokei knows

By the end of this chapter you can say how tokei decides what a file is, why some files are not counted at all, and how to make it name the ones it skipped.

The specimens live in the lab's detection folder: five files, of which tokei counts four — each for a different reason (transcript tokei-lab/transcripts/t03-detection.txt).

Claimed by name

Two files have no extension at all:

 |ixtures/detection/Makefile            3            2            1            0
 |tures/detection/Dockerfile            2            1            1            0

Makefile and Dockerfile are not descriptions; they are entries in tokei's table in their own right — the filename is the language evidence. The Makefile's tabbed recipe line counts as code and its # line as a comment, by the rules of make, once the name has claimed the file.

Claimed by the first line

stocktake has no extension and an ordinary name. Its first line does the claiming:

#!/bin/sh
# Count the shelves before opening.

echo "shelves: 12"

Tokei reads the shebang and counts the file as Shell: 4 lines, 1 code, 2 comments, 1 blank. Note where the shebang itself landed — it counts as a comment, because in shell # is the comment marker and the shebang is, grammatically, a comment the kernel happens to obey. Small, but it is a rule speaking: shebangs are not code in tokei's arithmetic.

Claimed as prose

disguised.txt contains two lines of Python under a .txt name. Tokei counts it — as Plain Text, 2 lines, 0 code, 2 comments. The detector believed the name tag, and under the Plain Text definition every line is a comment, the print included. A file counted in the wrong language is worse than a skipped file: it looks like evidence.

Not claimed at all

data.weirdext — plausible content, an extension nobody registered — does not appear in the output. Not as an error, not as a zero row: absence. By default tokei says nothing about the files it cannot place.

One flag makes it confess. Verbose mode level 1 reports unknown extensions:

$ tokei -v tokei-lab/fixtures/detection
[... WARN  tokei::language::language_type] Unknown extension: weirdext

When a total looks light, this is the check: run with -v and look for the warnings before you suspect the counting rules.

The map itself

tokei --languages prints the whole table — name and extensions for every language the detector knows. The lab counted 334 language rows in tokei 15.0.0 (transcript tokei-lab/transcripts/t10-languages-speed.txt). It is a generated table, not a curiosity: a file is a language exactly when this table, a filename or a shebang says so — and chapter 13 returns to what that costs.

Check: find a file on your machine whose extension you do not recognise, run tokei on its folder with and without -v, and see which witness, if any, claims it. If you can explain the outcome, you have this chapter.