Terminate running background transform on exit (addendum)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #4422
authored by Junegunn Choi · committed by Junegunn Choi · parent 247d168 · Browse files · 1 file·+17 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+17 added−7 removed
- other:+17 added−7 removed
Provenance
- Commit
3a21116- Tree
1c7d500- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
247d168
1 file changed+17 added−7 removed
modified:src/terminal.go+17 added-7 removed
234 unmodified lines235 version int64236 callback func()237 }238 239+type runningCmd struct {240+ cmd *exec.Cmd241+ tempFiles []string242+}243+244 // Terminal represents terminal input/output245 type Terminal struct {246 initDelay time.Duration247 infoCommand string134 unmodified lines382 selected map[int32]selectedItem383 version int64384 revision revision385 bgVersion int64381- runningCmds *util.ConcurrentSet[*exec.Cmd]386+ runningCmds *util.ConcurrentSet[*runningCmd]387 reqBox *util.EventBox388 initialPreviewOpts previewOpts389 previewOpts previewOpts390 activePreviewOpts *previewOpts645 unmodified lines1036 printsep: opts.PrintSep,1037 proxyScript: opts.ProxyScript,1038 merger: EmptyMerger(revision{}),1039 selected: make(map[int32]selectedItem),1035- runningCmds: util.NewConcurrentSet[*exec.Cmd](),1040+ runningCmds: util.NewConcurrentSet[*runningCmd](),1041 reqBox: util.NewEventBox(),1042 initialPreviewOpts: opts.Preview,1043 previewOpts: opts.Preview,1044 activePreviewOpts: &opts.Preview,3331 unmodified lines4376 func (t *Terminal) captureAsync(a action, firstLineOnly bool, callback func(string)) {4377 _, list := t.buildPlusList(a.a, false)4378 command, tempFiles := t.replacePlaceholder(a.a, false, string(t.input), list)4379 version := t.bgVersion4375- cmd := t.executor.ExecCommand(command, false)4380+ cmd := t.executor.ExecCommand(command, true)4381 cmd.Env = t.environ()4382 item := func(proceed bool) {4383 if proceed {4384 out, _ := cmd.StdoutPipe()4385 reader := bufio.NewReader(out)4386 var output string4387 if err := cmd.Start(); err == nil {4388+ runningCmd := runningCmd{cmd, tempFiles}4383- t.runningCmds.Add(cmd)4389+ t.runningCmds.Add(&runningCmd)4390 if firstLineOnly {4391 output, _ = reader.ReadString('\n')4392 output = strings.TrimRight(output, "\r\n")4393 } else {4394 bytes, _ := io.ReadAll(reader)4395 output = string(bytes)4396 }4397 cmd.Wait()4392- t.runningCmds.Remove(cmd)4398+ t.runningCmds.Remove(&runningCmd)4399 }4400 t.callbackChan <- versionedCallback{version, func() { callback(output) }}4401 }4402 removeFiles(tempFiles)660 unmodified lines5063 code = getCode()5064 if code <= ExitNoMatch && t.history != nil {5065 t.history.append(string(t.input))5066 }5061- t.runningCmds.ForEach(func(cmd *exec.Cmd) {5062- util.KillCommand(cmd)5067+ t.runningCmds.ForEach(func(cmd *runningCmd) {5068+ util.KillCommand(cmd.cmd)5069+ removeFiles(cmd.tempFiles)5070 })5071 running = false5072 t.mutex.Unlock()5073 }653 unmodified lines5727 }5728 })5729 case actBgCancel:5730 t.bgVersion++5731+ t.runningCmds.ForEach(func(cmd *runningCmd) {5732+ util.KillCommand(cmd.cmd)5733+ })5734 case actChangePrompt:5735 t.promptString = a.a5736 t.prompt, t.promptLen = t.parsePrompt(a.a)5737 req(reqPrompt)1162 unmodified lines