mirror of
https://github.com/walles/moar.git
synced 2024-11-30 02:34:13 +03:00
Unbreak search
This commit is contained in:
parent
41b7b8ab7c
commit
3bd4dd8df2
14
m/pager.go
14
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 := ""
|
||||
|
Loading…
Reference in New Issue
Block a user