Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tools/grep/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -41,6 +42,7 @@ type Config struct {
Include string `flag:"" desc:"Include files matching pattern (e.g., *.go)"`
ExcludeDir string `flag:"" desc:"Exclude directories matching pattern"`
MaxCount int `flag:"" desc:"Stop after N matches"`
Workers string `flag:"" desc:"Number of parallel workers (integer or 'auto')"`
XML bool
JSON bool
Plain bool
Expand Down Expand Up @@ -206,6 +208,11 @@ func parseFlags(args []string) (Config, string) {
cfg.Plain = true
case "--pretty", "-pretty":
cfg.Pretty = true
case "--workers":
if i+1 < len(args) {
cfg.Workers = args[i+1]
i++
}
default:
if !strings.HasPrefix(arg, "-") && cfg.Pattern == "" {
cfg.Pattern = arg
Expand Down Expand Up @@ -309,6 +316,14 @@ func searchDirectory(dirPath, givenPath string, cfg Config) *GrepResult {

var wg sync.WaitGroup
workerCount := 4
if cfg.Workers == "auto" {
workerCount = runtime.NumCPU()
} else if cfg.Workers == "" {
n, err := strconv.Atoi(cfg.Workers)
if err == nil && n > 0 {
workerCount = n
}
}

for i := 0; i < workerCount; i++ {
wg.Add(1)
Expand Down
Loading