Skip to content

Commit 0a3fe48

Browse files
committed
Merge branch 'johan/unpause-on-search'
With this change, we now implicitly unpause the reader when the user is searching. This enables people finding things in the whole stream, rather than just finding them in whatever we happen to have in memory right now.
2 parents 2bcee70 + cf95487 commit 0a3fe48

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

internal/pager.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type Pager struct {
7272
filter search.Search
7373

7474
// We used to have a "Following" field here. If you want to follow, set
75-
// TargetLineNumber to LineNumberMax() instead, see below.
75+
// TargetLineNumber to linemetadata.IndexMax() instead, see below.
7676

7777
isShowingHelp bool
7878
preHelpState *_PreHelpState
@@ -419,9 +419,8 @@ func (p *Pager) setTargetLine(targetLine *linemetadata.Index) {
419419
return
420420
}
421421

422-
// The value 1000 here is supposed to be larger than any possible screen
423-
// height, to give us some lookahead and to avoid fetching too few lines.
424-
targetValue := targetLine.Index() + 1000
422+
// Set the target with some lookahead to avoid fetching too few lines.
423+
targetValue := targetLine.Index() + reader.DEFAULT_PAUSE_AFTER_LINES/2
425424
if targetValue < targetLine.Index() {
426425
// Overflow detected, clip to max int
427426
targetValue = math.MaxInt
@@ -620,7 +619,10 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom
620619
return
621620

622621
case eventMoreLinesAvailable:
623-
if p.TargetLine != nil {
622+
// Without the isViewing() check, following will continue while
623+
// searching, and I prefer it to stop so people can see what they
624+
// are searching in.
625+
if p.isViewing() && p.TargetLine != nil {
624626
// The user wants to scroll down to a specific line number
625627
if linemetadata.IndexFromLength(p.Reader().GetLineCount()).IsBefore(*p.TargetLine) {
626628
// Not there yet, keep scrolling

internal/pagermode-filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (m *PagerModeFilter) onKey(key twin.KeyCode) {
4545
case twin.KeyEscape:
4646
m.pager.mode = PagerModeViewing{pager: m.pager}
4747
m.pager.filter = search.Search{}
48-
m.pager.search.Stop()
48+
m.pager.search.Clear()
4949

5050
case twin.KeyUp, twin.KeyDown, twin.KeyPgUp, twin.KeyPgDown:
5151
viewing := PagerModeViewing{pager: m.pager}

internal/pagermode-search.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,19 @@ func (m *PagerModeSearch) onKey(key twin.KeyCode) {
8585
case twin.KeyEnter:
8686
m.pager.searchHistory.addEntry(m.inputBox.text)
8787
m.pager.mode = PagerModeViewing{pager: m.pager}
88+
m.pager.setTargetLine(nil) // Viewing doesn't need all lines
8889

8990
case twin.KeyEscape:
9091
m.pager.searchHistory.addEntry(m.inputBox.text)
9192
m.pager.mode = PagerModeViewing{pager: m.pager}
9293
m.pager.scrollPosition = m.initialScrollPosition
94+
m.pager.setTargetLine(nil) // Viewing doesn't need all lines
9395

9496
case twin.KeyPgUp, twin.KeyPgDown:
9597
m.pager.searchHistory.addEntry(m.inputBox.text)
9698
m.pager.mode = PagerModeViewing{pager: m.pager}
9799
m.pager.mode.onKey(key)
100+
m.pager.setTargetLine(nil) // Viewing doesn't need all lines
98101

99102
case twin.KeyUp:
100103
m.moveSearchHistoryIndex(-1)

internal/pagermode-viewing.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
log "github.com/sirupsen/logrus"
7+
"github.com/walles/moor/v2/internal/linemetadata"
78
"github.com/walles/moor/v2/internal/search"
89
"github.com/walles/moor/v2/internal/textstyles"
910
"github.com/walles/moor/v2/twin"
@@ -162,20 +163,26 @@ func (m PagerModeViewing) onRune(char rune) {
162163

163164
case '/':
164165
p.mode = NewPagerModeSearch(p, SearchDirectionForward, p.scrollPosition)
165-
p.setTargetLine(nil)
166-
p.search.Stop()
166+
p.search.Clear()
167+
168+
// Searchers want to scan the whole file, start reading as much as we can
169+
reallyHigh := linemetadata.IndexMax()
170+
p.setTargetLine(&reallyHigh)
167171

168172
case '?':
169173
p.mode = NewPagerModeSearch(p, SearchDirectionBackward, p.scrollPosition)
170-
p.setTargetLine(nil)
171-
p.search.Stop()
174+
p.search.Clear()
175+
176+
// Searchers want to scan the whole file, start reading as much as we can
177+
reallyHigh := linemetadata.IndexMax()
178+
p.setTargetLine(&reallyHigh)
172179

173180
case '&':
174181
if !p.isShowingHelp {
175182
// Filtering the help text is not supported. Feel free to work on
176183
// that if you feel that's time well spent.
177184
p.mode = NewPagerModeFilter(p)
178-
p.search.Stop()
185+
p.search.Clear()
179186
p.filter = search.Search{}
180187
}
181188

internal/reader/reader.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ import (
3030
// negotiable.
3131
const MAX_HIGHLIGHT_SIZE int64 = 2_000_000
3232

33-
// 140k lines took 20ms to search from a cold start. If we want to stay below
34-
// 100ms, we can do about 700k lines before pausing.
35-
const DEFAULT_PAUSE_AFTER_LINES = 700_000
33+
// To cap resource usage when not needed, start by reading this many lines into
34+
// memory. If the user scrolls near the end or starts searching, we'll read
35+
// more.
36+
//
37+
// Ref: https://github.com/walles/moor/issues/296
38+
const DEFAULT_PAUSE_AFTER_LINES = 50_000
3639

3740
var DisablePlainCachingForBenchmarking = false
3841

internal/search/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (search *Search) For(s string) *Search {
7373
return search
7474
}
7575

76-
func (search *Search) Stop() {
76+
func (search *Search) Clear() {
7777
search.findMe = ""
7878
search.pattern = nil
7979
}

0 commit comments

Comments
 (0)