m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-16 15:23:48 -05:00

Reorganize code to ensure deletion of temp files

This commit is contained in:
Junegunn Choi
2025-06-21 23:06:46 +09:00
parent c36ddce36f
commit b2a8a283c7

View File

@@ -395,7 +395,7 @@ type Terminal struct {
killChan chan bool killChan chan bool
serverInputChan chan []*action serverInputChan chan []*action
callbackChan chan versionedCallback callbackChan chan versionedCallback
bgQueue map[action][]func() bgQueue map[action][]func(bool)
bgSemaphore chan struct{} bgSemaphore chan struct{}
bgSemaphores map[action]chan struct{} bgSemaphores map[action]chan struct{}
keyChan chan tui.Event keyChan chan tui.Event
@@ -1047,7 +1047,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox, executor *util.Executor
killChan: make(chan bool), killChan: make(chan bool),
serverInputChan: make(chan []*action, 100), serverInputChan: make(chan []*action, 100),
callbackChan: make(chan versionedCallback, maxBgProcesses), callbackChan: make(chan versionedCallback, maxBgProcesses),
bgQueue: make(map[action][]func()), bgQueue: make(map[action][]func(bool)),
bgSemaphore: make(chan struct{}, maxBgProcesses), bgSemaphore: make(chan struct{}, maxBgProcesses),
bgSemaphores: make(map[action]chan struct{}), bgSemaphores: make(map[action]chan struct{}),
keyChan: make(chan tui.Event), keyChan: make(chan tui.Event),
@@ -4371,27 +4371,29 @@ func (t *Terminal) captureAsync(a action, firstLineOnly bool, callback func(stri
version := t.bgVersion version := t.bgVersion
cmd := t.executor.ExecCommand(command, false) cmd := t.executor.ExecCommand(command, false)
cmd.Env = t.environ() cmd.Env = t.environ()
item := func() { item := func(proceed bool) {
out, _ := cmd.StdoutPipe() if proceed {
reader := bufio.NewReader(out) out, _ := cmd.StdoutPipe()
var output string reader := bufio.NewReader(out)
if err := cmd.Start(); err == nil { var output string
if firstLineOnly { if err := cmd.Start(); err == nil {
output, _ = reader.ReadString('\n') if firstLineOnly {
output = strings.TrimRight(output, "\r\n") output, _ = reader.ReadString('\n')
} else { output = strings.TrimRight(output, "\r\n")
bytes, _ := io.ReadAll(reader) } else {
output = string(bytes) bytes, _ := io.ReadAll(reader)
output = string(bytes)
}
cmd.Wait()
} }
cmd.Wait() t.callbackChan <- versionedCallback{version, func() { callback(output) }}
} }
removeFiles(tempFiles) removeFiles(tempFiles)
t.callbackChan <- versionedCallback{version, func() { callback(output) }}
} }
queue, prs := t.bgQueue[a] queue, prs := t.bgQueue[a]
if !prs { if !prs {
queue = []func(){} queue = []func(bool){}
} }
queue = append(queue, item) queue = append(queue, item)
t.bgQueue[a] = queue t.bgQueue[a] = queue
@@ -4416,6 +4418,9 @@ Loop:
case semaphore <- struct{}{}: case semaphore <- struct{}{}:
default: default:
// Failed to acquire local semaphore, putting only the last one back to the queue // Failed to acquire local semaphore, putting only the last one back to the queue
for _, item := range queue[:len(queue)-1] {
item(false)
}
t.bgQueue[a] = queue[len(queue)-1:] t.bgQueue[a] = queue[len(queue)-1:]
continue Loop continue Loop
} }
@@ -4424,7 +4429,7 @@ Loop:
// Acquire global semaphore // Acquire global semaphore
t.bgSemaphore <- struct{}{} t.bgSemaphore <- struct{}{}
todo() todo(true)
// Release local semaphore // Release local semaphore
<-semaphore <-semaphore
// Release global semaphore // Release global semaphore
@@ -5291,7 +5296,7 @@ func (t *Terminal) Loop() error {
var event tui.Event var event tui.Event
actions := []*action{} actions := []*action{}
callbacks := []func(){} callbacks := []versionedCallback{}
select { select {
case event = <-t.keyChan: case event = <-t.keyChan:
needBarrier = true needBarrier = true
@@ -5326,16 +5331,12 @@ func (t *Terminal) Loop() error {
case callback := <-t.callbackChan: case callback := <-t.callbackChan:
event = tui.Invalid.AsEvent() event = tui.Invalid.AsEvent()
actions = append(actions, &action{t: actAsync}) actions = append(actions, &action{t: actAsync})
if callback.version == t.bgVersion { callbacks = append(callbacks, callback)
callbacks = append(callbacks, callback.callback)
}
DrainCallback: DrainCallback:
for { for {
select { select {
case callback = <-t.callbackChan: case callback = <-t.callbackChan:
if callback.version == t.bgVersion { callbacks = append(callbacks, callback)
callbacks = append(callbacks, callback.callback)
}
continue DrainCallback continue DrainCallback
default: default:
break DrainCallback break DrainCallback
@@ -5450,7 +5451,9 @@ func (t *Terminal) Loop() error {
case actIgnore, actStart, actClick: case actIgnore, actStart, actClick:
case actAsync: case actAsync:
for _, callback := range callbacks { for _, callback := range callbacks {
callback() if t.bgVersion == callback.version {
callback.callback()
}
} }
case actBecome: case actBecome:
valid, list := t.buildPlusList(a.a, false) valid, list := t.buildPlusList(a.a, false)