Output formats
By the end of this chapter you can take tokei's counts out of the table and into other programs — and you know which of its output options the lab could not make work, because it says so here rather than letting you find out.
The table, and its receipts
The default output is the table
you have been reading. Add
--files for the per-file
receipts; know that long paths
are trimmed from the left (a
leading | marks the cut), so
the table is for eyes, not for
parsing. For programs, use one of
the formats below.
JSON, and the summing trap
$ tokei -o json tokei-lab/fixtures/basics
writes one JSON object: a key per
language, each holding its totals
and its per-file reports, plus
a Total. Feeding it to Python is
three lines of work, and the lab
did — and walked into a trap worth
a paragraph. Summing code over
the top-level languages gives
11 for the basics tree. The
Total says 12. The missing
line is the embedded Python line
inside notes.md: embedded counts
live in a report's blobs, a
level below the language totals,
and a naive sum walks straight
past them. The rule for consumers:
read the Total for totals,
and descend into reports and
blobs only when you mean to.
The stream
$ tokei --streaming simple tokei-lab/fixtures/basics
C .../basics/block.c 10 6 3 1
Python .../basics/greet.py 10 5 2 3
Markdown .../basics/notes.md 7 0 5 2
One record per file — language,
path, lines, code, comments,
blanks — spaced for a script to
split. Note notes.md appears
once, as its host language only;
the stream does not break out
embedded children. A json
variant of the stream exists, one
JSON record per line.
Formats are a build-time choice
$ tokei -o yaml tokei-lab/fixtures/basics
error: invalid value 'yaml' for '--output <output>':
This version of tokei was compiled without any 'yaml'
serialization support, to enable serialization, reinstall
tokei with the features flag. ... cargo install tokei --features yaml
The lab's build writes JSON
because it was compiled with JSON
and nothing else (chapter 3's
--version line said so). YAML,
CBOR and friends are features you
switch on when you build. If a
script of yours depends on one,
check the version line of the
tokei that will run it — the
option is accepted by the manual
and refused by the wrong binary,
politely, at run time.
A number format for humans
On big trees the table's numbers
grow long. -n commas prints
them grouped — in tokei's own
source tree, its languages file
reads 2,124 lines instead of
2124 (dots and underscores
are the other styles). Purely
cosmetic, and refused in
combination with --output.
The option that did nothing: -i
The help offers
-i, --input <file> — "Gives
statistics from a previous tokei
run" — meant for re-displaying or
combining saved counts. The lab
tested it four ways: with the
JSON file as an argument and on
standard input, from an empty
folder and from the guide's own
root. From the empty folder it
printed an all-zero table rather
than the saved counts; from the
populated folder the table was
identical to a plain run. In
15.0.0, as built here, -i never
changed a single number
(transcript
tokei-lab/transcripts/t07-formats.txt).
Until a future version proves
otherwise on your machine, treat
the saved JSON itself as the
record, and recount when you need
a table.
Check: produce the basics JSON,
read its Total with any tool
you like, and confirm the code
count is 12 — then find the
embedded line hiding in the
Markdown report's blobs. If
both numbers are where this
chapter left them, you have it.