diff --git a/m/pager.go b/m/pager.go index 5c28861..6681845 100644 --- a/m/pager.go +++ b/m/pager.go @@ -216,9 +216,13 @@ func (p *Pager) scrollToSearchHits() { func (p *Pager) findFirstHit(startPosition scrollPosition, backwards bool) *scrollPosition { // FIXME: We should take startPosition.deltaScreenLines into account as well! - searchPosition := startPosition + + // NOTE: When we search, we do that by looping over the *input lines*, not + // the screen lines. That's why we're using an int rather than a + // scrollPosition for searching. + searchPosition := startPosition.lineNumberOneBased(p) for { - line := p.reader.GetLine(searchPosition.lineNumberOneBased(p)) + line := p.reader.GetLine(searchPosition) if line == nil { // No match, give up return nil @@ -226,13 +230,13 @@ func (p *Pager) findFirstHit(startPosition scrollPosition, backwards bool) *scro lineText := line.Plain() if p.searchPattern.MatchString(lineText) { - return scrollPositionFromLineNumber("findFirstHit", searchPosition.lineNumberOneBased(p)) + return scrollPositionFromLineNumber("findFirstHit", searchPosition) } if backwards { - searchPosition.PreviousLine(1) + searchPosition -= 1 } else { - searchPosition.NextLine(1) + searchPosition += 1 } } } diff --git a/m/pager_test.go b/m/pager_test.go index 1d382d4..9359b98 100644 --- a/m/pager_test.go +++ b/m/pager_test.go @@ -269,6 +269,20 @@ func TestFindFirstHitAnsi(t *testing.T) { assert.Equal(t, hit.internalDontTouch.deltaScreenLines, 0) } +func TestFindFirstHitNoMatch(t *testing.T) { + reader := NewReaderFromStream("TestFindFirstHitSimple", strings.NewReader("AB")) + pager := NewPager(reader) + pager.screen = twin.NewFakeScreen(40, 10) + + // Wait for reader to finish reading + <-reader.done + + pager.searchPattern = toPattern("this pattern should not be found") + + hit := pager.findFirstHit(newScrollPosition("TestFindFirstHitSimple"), false) + assert.Assert(t, hit == nil) +} + // Converts a cell row to a plain string and removes trailing whitespace. func rowToString(row []twin.Cell) string { rowString := ""