From 93bbb3032d1e7550dbabb2d450999cd434c60509 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 4 Jun 2024 15:48:38 +0900 Subject: Add --tail=NUM to limit the number of items to keep in memory --- src/core.go | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'src/core.go') diff --git a/src/core.go b/src/core.go index 0c7abff1..f2563a08 100644 --- a/src/core.go +++ b/src/core.go @@ -18,6 +18,28 @@ Matcher -> EvtSearchFin -> Terminal (update list) Matcher -> EvtHeader -> Terminal (update header) */ +type revision struct { + major int + minor int +} + +func (r *revision) bumpMajor() { + r.major++ + r.minor = 0 +} + +func (r *revision) bumpMinor() { + r.minor++ +} + +func (r revision) equals(other revision) bool { + return r.major == other.major && r.minor == other.minor +} + +func (r revision) compatible(other revision) bool { + return r.major == other.major +} + // Run starts fzf func Run(opts *Options) (int, error) { if opts.Tmux != nil && len(os.Getenv("TMUX")) > 0 && opts.Tmux.index >= opts.Height.index { @@ -155,8 +177,8 @@ func Run(opts *Options) (int, error) { opts.Fuzzy, opts.FuzzyAlgo, opts.Extended, opts.Case, opts.Normalize, forward, withPos, opts.Filter == nil, opts.Nth, opts.Delimiter, runes) } - inputRevision := 0 - snapshotRevision := 0 + inputRevision := revision{} + snapshotRevision := revision{} matcher := NewMatcher(cache, patternBuilder, sort, opts.Tac, eventBox, inputRevision) // Filtering mode @@ -190,7 +212,8 @@ func Run(opts *Options) (int, error) { eventBox.Unwatch(EvtReadNew) eventBox.WaitFor(EvtReadFin) - snapshot, _ := chunkList.Snapshot() + // NOTE: Streaming filter is inherently not compatible with --tail + snapshot, _, _ := chunkList.Snapshot(opts.Tail) merger, _ := matcher.scan(MatchRequest{ chunks: snapshot, pattern: pattern}) @@ -261,7 +284,7 @@ func Run(opts *Options) (int, error) { reading = true chunkList.Clear() itemIndex = 0 - inputRevision++ + inputRevision.bumpMajor() header = make([]string, 0, opts.HeaderLines) go reader.restart(command, environ) } @@ -306,10 +329,14 @@ func Run(opts *Options) (int, error) { useSnapshot = false } if !useSnapshot { - if snapshotRevision != inputRevision { + if !snapshotRevision.compatible(inputRevision) { query = []rune{} } - snapshot, count = chunkList.Snapshot() + var changed bool + snapshot, count, changed = chunkList.Snapshot(opts.Tail) + if changed { + inputRevision.bumpMinor() + } snapshotRevision = inputRevision } total = count @@ -349,7 +376,10 @@ func Run(opts *Options) (int, error) { break } if !useSnapshot { - newSnapshot, newCount := chunkList.Snapshot() + newSnapshot, newCount, changed := chunkList.Snapshot(opts.Tail) + if changed { + inputRevision.bumpMinor() + } // We want to avoid showing empty list when reload is triggered // and the query string is changed at the same time i.e. command != nil && changed if command == nil || newCount > 0 { -- cgit v1.2.3