From 163d17eaf668a7aa206bf3b296c4371d9d2ecb16 Mon Sep 17 00:00:00 2001 From: Aleksey Lobanov Date: Tue, 2 Jan 2024 21:13:28 +0000 Subject: [PATCH] refactor: Separate function for grep results formatting --- internal/processing/walk.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/internal/processing/walk.go b/internal/processing/walk.go index 5556a0e..2a2647a 100644 --- a/internal/processing/walk.go +++ b/internal/processing/walk.go @@ -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 - } - 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") + if err != nil { + return err + } + err = printGrepResults(processedChannel, totalFiles) + if err != nil { + return err } return err }