Add --tail=NUM to limit the number of items to keep in memory

This commit is contained in:
Junegunn Choi
2024-06-04 15:48:38 +09:00
parent 4c83d8596d
commit 93bbb3032d
10 changed files with 226 additions and 56 deletions

View File

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