package processing import ( "fmt" "io/fs" "path/filepath" models "gitea.likemath.ru/alex/gogrep/internal/models" ) // ChannelBuffering is the size of file processing results queue const ChannelBuffering = 10 func printGrepResults(processedChannel chan models.FileMatchData, expectedItems int) error { for ; expectedItems > 0; expectedItems-- { curProcessed := <-processedChannel if len(curProcessed.Lines) == 0 { continue } fmt.Printf("%s:\n", curProcessed.Path) for i := 0; i < len(curProcessed.Lines); i++ { fmt.Printf("%d:%s\n", curProcessed.LineIndexes[i], curProcessed.Lines[i]) } fmt.Print("\n") } return nil } // DoGrepMain performs full grep accorgind to config and root func DoGrepMain(config *models.GrepConfigInternal, root string) error { processedChannel := make(chan models.FileMatchData, ChannelBuffering) totalFiles := 0 err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error { if err != nil { fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err) return err } if config.PathPattern.FindString(path) == "" { return nil } if info.IsDir() { return nil } go ProcessSingleFile(config.TextPattern, path, processedChannel) totalFiles++ return nil }) if err != nil { return err } err = printGrepResults(processedChannel, totalFiles) if err != nil { return err } return err }