m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-18 00:03:39 -05:00

Add reload-sync action

Close #2816
This commit is contained in:
Junegunn Choi
2022-12-29 20:03:51 +09:00
parent 14775aa975
commit 6c37177cf5
6 changed files with 124 additions and 66 deletions

View File

@@ -213,15 +213,6 @@ func Run(opts *Options, version string, revision string) {
clearSelection := util.Once(false)
ticks := 0
var nextCommand *string
restart := func(command string) {
reading = true
clearCache = util.Once(true)
clearSelection = util.Once(true)
chunkList.Clear()
itemIndex = 0
header = make([]string, 0, opts.HeaderLines)
go reader.restart(command)
}
eventBox.Watch(EvtReadNew)
total := 0
query := []rune{}
@@ -236,6 +227,25 @@ func Run(opts *Options, version string, revision string) {
terminal.startChan <- fitpad{-1, -1}
}
}
useSnapshot := false
var snapshot []*Chunk
var prevSnapshot []*Chunk
var count int
restart := func(command string) {
reading = true
clearCache = util.Once(true)
clearSelection = util.Once(true)
// We should not update snapshot if reload is triggered again while
// the previous reload is in progress
if useSnapshot && prevSnapshot != nil {
snapshot, count = chunkList.Snapshot()
}
chunkList.Clear()
itemIndex = 0
header = make([]string, 0, opts.HeaderLines)
go reader.restart(command)
}
for {
delay := true
ticks++
@@ -267,7 +277,13 @@ func Run(opts *Options, version string, revision string) {
} else {
reading = reading && evt == EvtReadNew
}
snapshot, count := chunkList.Snapshot()
if useSnapshot && evt == EvtReadFin {
useSnapshot = false
prevSnapshot = nil
}
if !useSnapshot {
snapshot, count = chunkList.Snapshot()
}
total = count
terminal.UpdateCount(total, !reading, value.(*string))
if opts.Sync {
@@ -286,6 +302,9 @@ func Run(opts *Options, version string, revision string) {
case searchRequest:
sort = val.sort
command = val.command
if command != nil {
useSnapshot = val.sync
}
}
if command != nil {
if reading {
@@ -296,7 +315,9 @@ func Run(opts *Options, version string, revision string) {
}
break
}
snapshot, _ := chunkList.Snapshot()
if !useSnapshot {
snapshot, _ = chunkList.Snapshot()
}
reset := clearCache()
matcher.Reset(snapshot, input(reset), true, !reading, sort, reset)
delay = false

View File

@@ -892,7 +892,7 @@ const (
func init() {
executeRegexp = regexp.MustCompile(
`(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-query|change-prompt|change-preview-window|change-preview|(?:re|un)bind|pos|put|transform-query)`)
`(?si)[:+](execute(?:-multi|-silent)?|reload(?:-sync)?|preview|change-query|change-prompt|change-preview-window|change-preview|(?:re|un)bind|pos|put|transform-query)`)
splitRegexp = regexp.MustCompile("[,:]+")
actionNameRegexp = regexp.MustCompile("(?i)^[a-z-]+")
}
@@ -1185,6 +1185,8 @@ func isExecuteAction(str string) actionType {
switch prefix {
case "reload":
return actReload
case "reload-sync":
return actReloadSync
case "unbind":
return actUnbind
case "rebind":

View File

@@ -335,6 +335,7 @@ const (
actFirst
actLast
actReload
actReloadSync
actDisableSearch
actEnableSearch
actSelect
@@ -353,6 +354,7 @@ type placeholderFlags struct {
type searchRequest struct {
sort bool
sync bool
command *string
}
@@ -2540,6 +2542,7 @@ func (t *Terminal) Loop() {
}()
for looping {
var newCommand *string
var reloadSync bool
changed := false
beof := false
queryChanged := false
@@ -3030,7 +3033,7 @@ func (t *Terminal) Loop() {
}
}
}
case actReload:
case actReload, actReloadSync:
t.failed = nil
valid, list := t.buildPlusList(a.a, false)
@@ -3044,6 +3047,7 @@ func (t *Terminal) Loop() {
if valid {
command := t.replacePlaceholder(a.a, false, string(t.input), list)
newCommand = &command
reloadSync = a.t == actReloadSync
t.reading = true
}
case actUnbind:
@@ -3173,7 +3177,7 @@ func (t *Terminal) Loop() {
t.mutex.Unlock() // Must be unlocked before touching reqBox
if changed || newCommand != nil {
t.eventBox.Set(EvtSearchNew, searchRequest{sort: t.sort, command: newCommand})
t.eventBox.Set(EvtSearchNew, searchRequest{sort: t.sort, sync: reloadSync, command: newCommand})
}
for _, event := range events {
t.reqBox.Set(event, nil)