A concurrent, non-blank line counter for source code directories, written in GO.
It recursively walks a directory, concurrently analyzes files, and reports the number of non-blank lines of code, grouped by file extension.
The tool is designed for performance, utilizing goroutines to process files in parallel.
To install the lines command-line tool, ensure you have Go installed and configured, then run:
go install github.com/moderrek/lines/cmd/lines@latestThis will download the source, compile it, and place the lines binary in your Go bin directory ($GOPATH/bin or $HOME/go/in).
The lines command accepts the following flags:
Usage: lines [options]
Options:
-dir string
The directory to analyze (default ".")
-hidden
Include hidden files and directories in the analysis
-top uint
Show only the top N extensions by line count
-no-color
Disable colorized output
-json
Output results in JSON format
-version
Print version information and exit
-help
Show this help message and exit
To analyze the directory ~/projects/my-app and display the top 5 extensions:
lines --dir ~/projects/my-app --top 5To get the output in JSON format, which can be piped to other tools like jq:
lines --dir ~/projects/my-app --jsonExample output (--json):
{
".css": 1122,
".go": 15230,
".html": 4357,
".js": 8828,
".mod": 4980
}The core counting logic is available as a library. It can be imported into other Go projects.
import "github.com/moderrek/lines/pkg/lines"package main
import (
"fmt"
"log"
"github.com/moderrek/lines/pkg/lines"
)
func main() {
// Configure the counter with default settings.
config := lines.Config{
IncludeHidden: false,
}
counter := lines.NewCounter(config)
// Run the analysis on the current directory.
result, err := counter.Run(".")
if err != nil {
log.Fatalf("Analysis failed: %v", err)
}
// Print results.
for ext, count := range result.LinesByExtension {
fmt.Printf("Extension: %s, Lines: %d\n", ext, count)
}
}You can customize which directories and file extensions to ignore:
config := lines.Config{
IncludeHidden: false,
IgnoredDirs: []string{"node_modules", "vendor", ".git", "target", "dist"},
IgnoredExtensions: []string{".exe", ".dll", ".jpg", ".png"},
}
counter := lines.NewCounter(config)
result, err := counter.Run("./src")If IgnoredDirs or IgnoredExtensions are not provided, the library uses sensible defaults.
- Clone the repository:
git clone https://github.com/Moderrek/lines.git
- Navigate to the project directory:
cd lines - Build the binary:
This will create a
go build ./cmd/lines
linesexecutable in the current directory.
This project is licensed under the MIT License. See the LICENSE file for details.