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/[email protected]This will download the source, compile it, and place the lines binary in your Go bin directory ($GOPATH/bin or $HOME/go/bin). The binary works on Linux and Windows, and the release assets are published for both platforms.
If you want to use the library in another Go module, add it with:
go get github.com/Moderrek/[email protected]Then import the package from pkg/lines:
import "github.com/Moderrek/lines/pkg/lines"The lines command accepts the following flags and targets:
Usage: lines [options] [--dir PATH ...] [PATH ...]
You can analyze multiple directories or files in one run by repeating --dir or passing positional arguments.
Options:
-color
Force color output (e.g. when piping)
-no-color
Disable color output
-help
Print the help message
-hidden
Allows to analyze hidden files
-jobs uint
Specifies the number of jobs
-json
Output results in JSON format
-top uint
Print the top N extensions
-verbose
Verbose output
-version
Print the version
lines --dir ./cmd --dir ./pkg ./README.mdTo analyze the directory ~/projects/my-app and display the top 5 extensions:
lines --top 5 ~/projects/my-appTo get the output in JSON format, which can be piped to other tools like jq:
lines --json ~/projects/my-appExample 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.
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: lines.IgnoredDirSet("node_modules", ".git"),
IgnoredExtensions: lines.IgnoredExtensionSet("exe", ".env"),
}
counter := lines.NewCounter(config)
result, err := counter.Run("./src")If IgnoredDirs or IgnoredExtensions are not provided, the library uses sensible defaults. The helper functions above are optional, but they make the config easier to read.
- 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.
For v1.3.0, the intended distribution flow is:
go install github.com/Moderrek/lines/cmd/[email protected]Or, if you are integrating the library into another module:
go get github.com/Moderrek/[email protected]The GitHub release pipeline builds binaries for Linux and Windows so users without Go installed can download a ready-made executable.
This project is licensed under the MIT License. See the LICENSE file for details.