1
1
mirror of https://github.com/walles/moar.git synced 2024-11-30 02:34:13 +03:00

Unbreak search

This commit is contained in:
Johan Walles 2022-03-07 06:54:42 +01:00
parent 41b7b8ab7c
commit 3bd4dd8df2
2 changed files with 23 additions and 5 deletions

View File

@ -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
}
}
}

View File

@ -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 := ""