Export FZF_* variables to 'reload' process as well

This commit is contained in:
Junegunn Choi 2024-02-15 22:27:32 +09:00
parent 425deadca9
commit 5e6788c679
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
4 changed files with 26 additions and 11 deletions

View File

@ -213,6 +213,7 @@ func Run(opts *Options, version string, revision string) {
reading := true
ticks := 0
var nextCommand *string
var nextEnviron []string
eventBox.Watch(EvtReadNew)
total := 0
query := []rune{}
@ -232,13 +233,13 @@ func Run(opts *Options, version string, revision string) {
useSnapshot := false
var snapshot []*Chunk
var count int
restart := func(command string) {
restart := func(command string, environ []string) {
reading = true
chunkList.Clear()
itemIndex = 0
inputRevision++
header = make([]string, 0, opts.HeaderLines)
go reader.restart(command)
go reader.restart(command, environ)
}
for {
delay := true
@ -266,8 +267,9 @@ func Run(opts *Options, version string, revision string) {
os.Exit(value.(int))
case EvtReadNew, EvtReadFin:
if evt == EvtReadFin && nextCommand != nil {
restart(*nextCommand)
restart(*nextCommand, nextEnviron)
nextCommand = nil
nextEnviron = nil
break
} else {
reading = reading && evt == EvtReadNew
@ -292,11 +294,13 @@ func Run(opts *Options, version string, revision string) {
case EvtSearchNew:
var command *string
var environ []string
var changed bool
switch val := value.(type) {
case searchRequest:
sort = val.sort
command = val.command
environ = val.environ
changed = val.changed
if command != nil {
useSnapshot = val.sync
@ -306,8 +310,9 @@ func Run(opts *Options, version string, revision string) {
if reading {
reader.terminate()
nextCommand = command
nextEnviron = environ
} else {
restart(*command)
restart(*command, environ)
}
}
if !changed {

View File

@ -85,10 +85,10 @@ func (r *Reader) terminate() {
r.mutex.Unlock()
}
func (r *Reader) restart(command string) {
func (r *Reader) restart(command string, environ []string) {
r.event = int32(EvtReady)
r.startEventPoller()
success := r.readFromCommand(command)
success := r.readFromCommand(command, environ)
r.fin(success)
}
@ -101,7 +101,8 @@ func (r *Reader) ReadSource() {
if len(cmd) == 0 {
success = r.readFiles()
} else {
success = r.readFromCommand(cmd)
// We can't export FZF_* environment variables to the default command
success = r.readFromCommand(cmd, nil)
}
} else {
success = r.readFromStdin()
@ -171,11 +172,14 @@ func (r *Reader) readFiles() bool {
return fastwalk.Walk(&conf, ".", fn) == nil
}
func (r *Reader) readFromCommand(command string) bool {
func (r *Reader) readFromCommand(command string, environ []string) bool {
r.mutex.Lock()
r.killed = false
r.command = &command
r.exec = util.ExecCommand(command, true)
if environ != nil {
r.exec.Env = environ
}
out, err := r.exec.StdoutPipe()
if err != nil {
r.mutex.Unlock()

View File

@ -499,6 +499,7 @@ type searchRequest struct {
sort bool
sync bool
command *string
environ []string
changed bool
}
@ -4081,10 +4082,15 @@ func (t *Terminal) Loop() {
req(reqPrompt)
}
reload := changed || newCommand != nil
var reloadRequest *searchRequest
if reload {
reloadRequest = &searchRequest{sort: t.sort, sync: reloadSync, command: newCommand, environ: t.environ(), changed: changed}
}
t.mutex.Unlock() // Must be unlocked before touching reqBox
if changed || newCommand != nil {
t.eventBox.Set(EvtSearchNew, searchRequest{sort: t.sort, sync: reloadSync, command: newCommand, changed: changed})
if reload {
t.eventBox.Set(EvtSearchNew, *reloadRequest)
}
for _, event := range events {
t.reqBox.Set(event, nil)

View File

@ -1918,7 +1918,7 @@ class TestGoFZF < TestBase
end
def test_reload
tmux.send_keys %(seq 1000 | #{FZF} --bind 'change:reload(seq {q}),a:reload(seq 100),b:reload:seq 200' --header-lines 2 --multi 2), :Enter
tmux.send_keys %(seq 1000 | #{FZF} --bind 'change:reload(seq $FZF_QUERY),a:reload(seq 100),b:reload:seq 200' --header-lines 2 --multi 2), :Enter
tmux.until { |lines| assert_equal 998, lines.match_count }
tmux.send_keys 'a'
tmux.until do |lines|