summaryrefslogtreecommitdiff
path: root/src/core.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2024-06-04 15:48:38 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2024-06-04 17:50:46 +0900
commit93bbb3032d1e7550dbabb2d450999cd434c60509 (patch)
treef6b4331975184172b256c71335f5964bcddc77df /src/core.go
parent4c83d8596d48acc106ffe62d50a55b3f9608c830 (diff)
downloadfzf-93bbb3032d1e7550dbabb2d450999cd434c60509.tar.gz
Add --tail=NUM to limit the number of items to keep in memory
Diffstat (limited to 'src/core.go')
-rw-r--r--src/core.go44
1 files changed, 37 insertions, 7 deletions
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 {