CounterLines - Source Lines of Code CLI Tool
A high-performance, multi-language source code line counter written in Rust that fully implements the requirements specified in REQUIREMENTS.md.
- Multi-language support with extensible language definitions (REQ-3.1, REQ-3.3)
- Excludes unsupported files from statistics (REQ-3.5.1), not included in line/language counts (REQ-3.5.2), and lists them separately (REQ-3.5.3)
- Accurate line counting differentiating between total, logical, and empty lines (REQ-1.1)
- Multiple output formats: JSON, XML, CSV (REQ-6.1, REQ-6.2, REQ-6.3)
- Report processing and comparison without rescanning files (REQ-7.1, REQ-7.2)
- Parallel processing for large codebases (REQ-9.4)
- Progress indicators for long operations (REQ-9.5)
- Cross-platform support for Linux, macOS, and Windows (REQ-9.1)
- UTF-8 and UTF-16 encoding support (REQ-9.2)
Installation
# Clone the repository
# Build the project
# The binary will be available at target/release/sloc
Usage
Basic Commands
Count Lines (REQ-8.3)
# Count lines in specific files
# Count with wildcards (REQ-2.2)
# Recursive directory traversal (REQ-2.3)
# Show per-file statistics and unsupported file list (only if requested)
# Read file list from stdin (REQ-2.4)
|
Generate Reports (REQ-8.3)
# Generate JSON report (REQ-6.1) (explicit output)
# Generate JSON report letting the tool auto-name the file (sloc-report.json)
# Generate XML report (REQ-6.2)
# Generate CSV report (REQ-6.3)
# Include checksum (REQ-6.9)
# Show per-file statistics and unsupported file list in console (only if requested)
Default auto-generated report file name:
sloc-report.<format>
(e.g.,sloc-report.json
). Configure viadefaults.output_file
inconfig.toml
. Applies to bothcount
andreport
when--format
is provided without--output
.
Note: By default, the tool prints only summary and language statistics. Use
--details
to print per-file statistics and the list of unsupported files in the console output.
Process Existing Reports (REQ-7.1)
# Process and display statistics from a report
# Sort by different metrics (REQ-5.4)
# Export processed results
Compare Reports (REQ-7.2)
# Compare two reports
# Export comparison (REQ-7.4)
Advanced Options
Language Configuration
# Use custom language definitions (REQ-3.3)
# Override language detection (REQ-3.4)
# Ignore preprocessor directives (REQ-4.5)
Performance Options
# Set parallel threads (REQ-9.4)
# Progress bar shown by default (REQ-9.5). Use --no-progress to disable
Supported Languages & Unsupported Files
Built-in support for (REQ-3.1):
Files for which no language definition is available are:
-
Excluded from all statistics and summaries (REQ-3.5.1)
-
Not included in line or language counts (REQ-3.5.2)
-
Listed separately in the console output and reports (REQ-3.5.3)
-
Rust (including nested comments)
-
C/C++ (with preprocessor directives)
-
Python (including docstrings)
-
JavaScript/TypeScript
-
Java
-
Go
-
Ruby
-
Shell scripts
-
SQL
-
HTML
-
CSS/SCSS/SASS
-
YAML
-
TOML
-
And more...
Line Counting Rules
The tool counts four types of lines (REQ-1.1):
- Total Lines: All lines in a file
- Logical Lines: Lines containing actual code (excluding comments, comment-only lines, and blanks)
- Comment Lines: Lines containing only comments (excluding code and blank lines)
- Empty Lines: Lines with only whitespace
Special Features
- Nested Comments (REQ-4.3): Properly handles nested comments in languages like Rust, Scala, and Haskell
- Mixed Lines (REQ-4.4): Correctly identifies lines containing both code and comments
- Preprocessor Directives (REQ-4.5): Option to ignore or count preprocessor directives
Report Format
Reports include (REQ-6.4, REQ-6.5, REQ-6.6):
- Per-file statistics (path, language, line counts: total, logical, comment, empty)
- Language summaries
- Global statistics
- Generation timestamp (RFC 3339/ISO 8601)
- Report format version
- Optional SHA256 checksum (REQ-6.9)
Example JSON Report Structure
Custom Language Configuration
Create a languages.toml
file to add or modify language definitions (REQ-3.3):
[]
= "MyLanguage"
= ["ml", "mli"]
= ["//", "#"]
= [
{ = "/*", = "*/" },
{ = "(*", = "*)" }
]
= true
= "#"
Performance
- Parallel Processing (REQ-9.4): Utilizes multiple CPU cores via Rayon
- Memory Efficient (REQ-9.6): Streams files instead of loading them entirely
- Progress Indicators (REQ-9.5): Progress bars enabled by default for all operations
- Deterministic Output (REQ-9.3): Consistent results for identical inputs
Console Output
The tool provides formatted console output with (REQ-5.1, REQ-5.2, REQ-5.3):
- Color-coded tables
- Thousands separators
- Percentage calculations
- Sortable columns
Example Output
════════════════════════════════════════════════════════════════════════════════
Source Lines of Code (SLOC) Report
════════════════════════════════════════════════════════════════════════════════
Global Summary
────────────────────────────────────
┌──────────────┬──────────┐
│ Metric │ Value │
├──────────────┼──────────┤
│ Total Files │ 25 │
│ Total Lines │ 10,543 │
│ Logical Lines│ 8,234 │
│ Empty Lines │ 2,309 │
│ Languages │ 3 │
│ Code Density │ 78.10% │
│ Empty Ratio │ 21.90% │
└──────────────┴──────────┘
Language Summary
────────────────────────────────────────────────────────────────────────────────
┌────────────┬───────┬─────────┬──────────┬───────┬───────────┐
│ Language │ Files │ Total │ Logical │ Empty │ Density % │
├────────────┼───────┼─────────┼──────────┼───────┼───────────┤
│ Rust │ 15 │ 7,234 │ 5,890 │ 1,344 │ 81.42 │
│ TOML │ 5 │ 1,543 │ 1,234 │ 309 │ 79.97 │
│ Markdown │ 5 │ 1,766 │ 1,110 │ 656 │ 62.85 │
└────────────┴───────┴─────────┴──────────┴───────┴───────────┘
Error Handling
The tool provides clear error messages for (REQ-2.5):
- Invalid or inaccessible paths
- Unsupported file formats (excluded from statistics and listed separately; see REQ-3.5.1, REQ-3.5.2, REQ-3.5.3)
- Permission issues
- Encoding problems
Contributing
Contributions are welcome! Please ensure that:
- All existing tests pass
- New features include tests
- Code follows Rust conventions
- Documentation is updated
License
This project is licensed under the MIT License - see the LICENSE file for details.
Requirements Compliance
This implementation fully complies with all requirements specified in REQUIREMENTS.md:
- ✅ Purpose (REQ-1.x): Complete line counting functionality, including comment lines
- ✅ Input Handling (REQ-2.x): Flexible input methods
- ✅ Language Support (REQ-3.x, REQ-3.5.1, REQ-3.5.2, REQ-3.5.3): Extensible multi-language support, unsupported files excluded from statistics, not counted, and listed separately
- ✅ Counting Rules (REQ-4.x): Accurate line and comment classification
- ✅ Console Output (REQ-5.x): Formatted, sortable output
- ✅ Report Generation (REQ-6.x): Multiple export formats
- ✅ Report Processing (REQ-7.x): Compare and analyze reports
- ✅ CLI Interface (REQ-8.x): Intuitive command structure
- ✅ Non-Functional (REQ-9.x): Performance and compatibility