refactor: Separate function for grep results formatting
This commit is contained in:
@@ -11,6 +11,21 @@ import (
|
||||
// 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)
|
||||
@@ -31,16 +46,12 @@ func DoGrepMain(config *models.GrepConfigInternal, root string) error {
|
||||
|
||||
return nil
|
||||
})
|
||||
for ; totalFiles > 0; totalFiles-- {
|
||||
curProcessed := <-processedChannel
|
||||
if len(curProcessed.Lines) == 0 {
|
||||
continue
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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")
|
||||
err = printGrepResults(processedChannel, totalFiles)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user