تخطَّ إلى المحتوى

tokei

tokei هو برنامج فائق السرعة لحساب أسطر الكود والتعليقات والأسطر الفارغة. مكتوب بلغة Rust، يدعم أكثر من 200 لغة برمجة، ويوفر صيغ إخراج متعددة، ويحترم أنماط gitignore.

التثبيت

# 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

الاستخدام الأساسي

# 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

Reading المخرجات

# 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

Language Breakdown

# 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

Exclude Patterns

# 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

صيغ الإخراج

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')
"

YAML المخرجات

# YAML output
tokei -o yaml > stats.yaml

CBOR المخرجات

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

Sorting

# 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 for 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)"

Comparing Projects

# 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})')
"

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

Hidden Files and التكوين

# 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