Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡ Blazingly FAST Line Counter

GitHub License Go GitHub Downloads (all assets, all releases)

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.

Installation

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"

Usage

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.md

Example

To analyze the directory ~/projects/my-app and display the top 5 extensions:

lines --top 5 ~/projects/my-app

To get the output in JSON format, which can be piped to other tools like jq:

lines --json ~/projects/my-app

Example output (--json):

{
    ".css": 1122,
    ".go": 15230,
    ".html": 4357,
    ".js": 8828,
    ".mod": 4980
}

Library Usage

The core counting logic is available as a library. It can be imported into other Go projects.

Example

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)
	}
}

Custom Configuration

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.

Building from Source

  1. Clone the repository:
    git clone https://github.com/Moderrek/lines.git
  2. Navigate to the project directory:
    cd lines
  3. Build the binary:
    go build ./cmd/lines
    This will create a lines executable in the current directory.

Releasing

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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Releases

Sponsor this project

Used by

Contributors

Languages