From 1a9259eb9a10202c28dbd959e6cfa2e2fcf3e064 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 19 Jun 2025 17:16:14 +0300 Subject: Fix dgrep transmission percentage display The dgrep tool was showing 0% transmission rate in non-plain mode even when all matched lines were successfully transmitted. This was due to incorrect stats tracking. The issue was that DirectProcessor was updating stats position for every line read from the file, but GrepProcessor was only returning results for matching lines. This caused the stats array position to advance for non-matching lines, breaking the percentage calculation. Fixed by: 1. Moving updatePosition() call to only happen when a line will be sent 2. Having DirectProcessor call updateLineMatched() for all sent lines 3. Removing duplicate updateLineMatched() calls from GrepProcessor 4. Ensuring stats are consistently updated in DirectProcessor, not in individual processors Now dgrep correctly shows 100% (green) when all matched lines are transmitted. --- internal/io/fs/directprocessor.go | 23 +++++++++++++---------- internal/io/fs/grepprocessor.go | 8 +------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/internal/io/fs/directprocessor.go b/internal/io/fs/directprocessor.go index 312ac9c..c7d5cdc 100644 --- a/internal/io/fs/directprocessor.go +++ b/internal/io/fs/directprocessor.go @@ -218,13 +218,13 @@ func (dp *DirectProcessor) processReaderPreservingLineEndings(ctx context.Contex lineContent = nil } - // Update position stats - if dp.stats != nil { - dp.stats.updatePosition() - } - // Process the chunk if result, shouldSend := dp.processor.ProcessLine(chunk, lineNum, filePath, dp.stats, dp.sourceID); shouldSend { + // Update position stats only for lines that will be sent + if dp.stats != nil { + dp.stats.updatePosition() + dp.stats.updateLineMatched() + } if _, err := dp.output.Write(result); err != nil { return err } @@ -239,13 +239,13 @@ func (dp *DirectProcessor) processReaderPreservingLineEndings(ctx context.Contex // Normal line processing lineNum++ - // Update position stats - if dp.stats != nil { - dp.stats.updatePosition() - } - // Process line directly (line includes original line ending) if result, shouldSend := dp.processor.ProcessLine(line, lineNum, filePath, dp.stats, dp.sourceID); shouldSend { + // Update position stats only for lines that will be sent + if dp.stats != nil { + dp.stats.updatePosition() + dp.stats.updateLineMatched() + } if _, err := dp.output.Write(result); err != nil { return err } @@ -290,6 +290,9 @@ func (dp *DirectProcessor) processReaderPreservingLineEndings(ctx context.Contex // Update transmission stats if dp.stats != nil { dp.stats.updateLineTransmitted() + // DEBUG: Log stats + // fmt.Printf("DEBUG: After transmission - matchCount=%d, transmitCount=%d, percentage=%d\n", + // dp.stats.matchCount, dp.stats.transmitCount, dp.stats.transmittedPerc()) } } } diff --git a/internal/io/fs/grepprocessor.go b/internal/io/fs/grepprocessor.go index c0db7b6..57c4c2f 100644 --- a/internal/io/fs/grepprocessor.go +++ b/internal/io/fs/grepprocessor.go @@ -70,9 +70,6 @@ func (gp *GrepProcessor) ProcessLine(line []byte, lineNum int, filePath string, if gp.afterRemaining > 0 { gp.afterRemaining-- // Send this line as context - if stats != nil { - stats.updateLineMatched() // Count context lines as transmitted - } return gp.formatLine(line, lineNum, filePath, stats, sourceID), true } // If we have before context, buffer this line @@ -100,10 +97,7 @@ func (gp *GrepProcessor) ProcessLine(line []byte, lineNum int, filePath string, return nil, false } - // Update stats for matched line - if stats != nil { - stats.updateLineMatched() - } + // Stats will be updated by DirectProcessor when the line is actually sent // Build result with before context, current line, and set up after context var result []byte -- cgit v1.2.3