refactor: Separate function for grep results formatting

This commit is contained in:
2024-01-02 21:13:28 +00:00
parent e526e378cc
commit 163d17eaf6

View File

@@ -11,6 +11,21 @@ import (
// ChannelBuffering is the size of file processing results queue // ChannelBuffering is the size of file processing results queue
const ChannelBuffering = 10 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 // DoGrepMain performs full grep accorgind to config and root
func DoGrepMain(config *models.GrepConfigInternal, root string) error { func DoGrepMain(config *models.GrepConfigInternal, root string) error {
processedChannel := make(chan models.FileMatchData, ChannelBuffering) processedChannel := make(chan models.FileMatchData, ChannelBuffering)
@@ -31,16 +46,12 @@ func DoGrepMain(config *models.GrepConfigInternal, root string) error {
return nil return nil
}) })
for ; totalFiles > 0; totalFiles-- { if err != nil {
curProcessed := <-processedChannel return err
if len(curProcessed.Lines) == 0 { }
continue err = printGrepResults(processedChannel, totalFiles)
} if err != nil {
fmt.Printf("%s:\n", curProcessed.Path) return err
for i := 0; i < len(curProcessed.Lines); i++ {
fmt.Printf("%d:%s\n", curProcessed.LineIndexes[i], curProcessed.Lines[i])
}
fmt.Print("\n")
} }
return err return err
} }