Aller au contenu

Commandes tokei

tokei est un programme ultra-rapide pour compter les lignes de code, les commentaires et les lignes vides dans plus de 200 langages de programmation. Il est écrit en Rust et gère efficacement les grands dépôts.

Installation

# Using cargo (Rust)
cargo install tokei

# macOS
brew install tokei

# Ubuntu/Debian (via snap)
sudo snap install tokei

# Arch Linux
sudo pacman -S tokei

# Nix
nix-env -i tokei

# Download prebuilt binary
curl -sSfL https://github.com/XAMPPRocky/tokei/releases/latest/download/tokei-x86_64-unknown-linux-gnu.tar.gz | \
    tar xz -C /usr/local/bin/

# Verify installation
tokei --version

Utilisation de base

# Count lines in current directory
tokei

# Count lines in a specific directory
tokei /path/to/project

# Count lines in multiple directories
tokei src/ tests/ lib/

# Count a specific file
tokei main.rs

Lecture de la sortie

# Example tokei output:
# ===============================================================================
#  Language            Files        Lines         Code     Comments       Blanks
# ===============================================================================
#  Rust                   45        12345         9876          987         1482
#  TOML                    3          120           95           10           15
#  Markdown                5          450            0          350          100
#  YAML                    2           80           65            5           10
# ===============================================================================
#  Total                  55        12995        10036         1352         1607
# ===============================================================================

# Columns:
# Files    - Number of files for that language
# Lines    - Total lines (Code + Comments + Blanks)
# Code     - Lines of actual code
# Comments - Comment lines
# Blanks   - Empty lines

Répartition par langage

# Show detailed file-level breakdown
tokei --files

# Output includes per-file statistics:
# ===============================================================================
#  Language            Files        Lines         Code     Comments       Blanks
# ===============================================================================
#  Rust                   45        12345         9876          987         1482
# -------------------------------------------------------------------------------
#  src/main.rs                        250          200           20           30
#  src/lib.rs                         180          150           10           20
#  ...

# Show only specific languages
tokei -t Rust,Python,JavaScript

# List all supported languages
tokei --languages

Motifs d’exclusion

# Exclude specific directories
tokei -e node_modules -e target -e .git

# Exclude multiple patterns
tokei -e "*.min.js" -e vendor -e dist

# Exclude test files
tokei -e tests -e "*_test.go" -e "test_*.py"

# tokei respects .gitignore by default
# Files in .gitignore are automatically excluded

Formats de sortie

Sortie JSON

# JSON output for scripting
tokei -o json > stats.json

# Pretty-print JSON
tokei -o json | python3 -m json.tool

# Parse with jq
tokei -o json | python3 -c "
import sys, json
data = json.load(sys.stdin)
for lang, info in sorted(data.items()):
    if isinstance(info, dict) and 'code' in info:
        print(f'{lang}: {info[\"code\"]} lines of code')
"

Sortie YAML

# YAML output
tokei -o yaml > stats.yaml

Sortie CBOR

# CBOR (Concise Binary Object Representation) output
tokei -o cbor > stats.cbor

Tri

# Sort by lines of code (default)
tokei -s code

# Sort by number of files
tokei -s files

# Sort by total lines
tokei -s lines

# Sort by comments
tokei -s comments

# Sort by blank lines
tokei -s blanks

.tokeignore

# Create a .tokeignore file (same syntax as .gitignore)
# This excludes paths from tokei's count

# .tokeignore example:
# node_modules/
# dist/
# *.min.js
# *.min.css
# vendor/
# __pycache__/
# *.generated.go
# coverage/
# .tokeignore
node_modules/
dist/
build/
*.min.js
*.min.css
vendor/
__pycache__/
*.generated.*
coverage/
.next/

Badges pour les READMEs

# Generate a badge showing lines of code
# Use with shields.io or similar badge services

# Get total lines of code
LINES=$(tokei -o json | python3 -c "
import sys, json
data = json.load(sys.stdin)
total = sum(v.get('code', 0) for v in data.values() if isinstance(v, dict))
print(total)
")
echo "Total lines of code: $LINES"

# Markdown badge (using shields.io)
echo "![Lines of Code](https://img.shields.io/badge/lines%20of%20code-${LINES}-blue)"

Comparaison de projets

# Compare code statistics between directories
echo "=== Project A ==="
tokei /path/to/project-a
echo ""
echo "=== Project B ==="
tokei /path/to/project-b

# Track growth over time with JSON
tokei -o json > stats-$(date +%Y%m%d).json

# Compare two snapshots
python3 -c "
import json
with open('stats-20260101.json') as f:
    old = json.load(f)
with open('stats-20260601.json') as f:
    new = json.load(f)

for lang in new:
    if isinstance(new[lang], dict) and 'code' in new[lang]:
        old_code = old.get(lang, {}).get('code', 0)
        new_code = new[lang]['code']
        diff = new_code - old_code
        if diff != 0:
            print(f'{lang}: {old_code} -> {new_code} ({diff:+d})')
"

Intégration CI

# Use tokei in CI to track code metrics
# GitHub Actions example:
# - name: Count lines of code
#   run: |
#     cargo install tokei
#     tokei -o json > code-stats.json
#     tokei

# Fail if code exceeds a threshold
MAX_LINES=100000
CURRENT=$(tokei -o json | python3 -c "
import sys, json
data = json.load(sys.stdin)
print(sum(v.get('code', 0) for v in data.values() if isinstance(v, dict)))
")
if [ "$CURRENT" -gt "$MAX_LINES" ]; then
    echo "Code exceeds $MAX_LINES lines ($CURRENT)"
    exit 1
fi

Fichiers cachés et configuration

# Count hidden files too
tokei --hidden

# Specify number of columns for output
tokei --columns 100

# Show processing speed
tokei --verbose
# Shows how many files were processed and how fast