junegunn / fzf

:cherry_blossom: A command-line fuzzy finder
https://junegunn.github.io/fzf/
MIT License
61.81k stars 2.35k forks source link

Constraint to only exit fzf if processes base on key press are done? #3903

Closed tmpm697 closed 3 days ago

tmpm697 commented 3 days ago

Checklist

Output of fzf --version

0.53

OS

Shell

Problem / Steps to reproduce

So I found out why fzf causes prompt flick issue, especially with fzf --expect 'ctrl-h,ctrl-m', this dues to users will run other processes base on the key press later after fzf already exit:

test=$(echo "1\n2\n3\n" | fzf --expect 'ctrl-h,ctrl-l')

key=$(head -1 $test)

if [[ $key == 'ctrl-h' ]]; then
  sleep 2 # this represents multiple processes which cause the slowness
  # this 100% cause prompt flick in which it will disappear and re-appear again when the processes are done.
fi

To fix this, fzf need methods to monitor processes after keys pressed with --expect, and make sure only exit fzf if they're all done.

I think this is better than requires all shells to give some kind of constraint for a special process (fzf) to aware of others before exit/return.

junegunn commented 3 days ago

You're sequentially running two separate programs from your shell and I don't think it's possible to accomplish seamless transition between the two.

Having said that, you can experiment with --no-clear (though I don't think it's worth it), and you'll generally have better experience with non-full screen layouts i.e. --height or --tmux.

example() {
  local result
  result=$(fzf --no-clear --no-tmux --no-height)
  sleep 2
  [[ -n $result ]] && vim "$result"
  tput rmcup
}
tmpm697 commented 3 days ago

I simply can't put sleep 2 out of if [[ $key == 'ctrl-h' ]] dues to I'm processing result base on the key pressed.

If it's ctrl-h then do something with result but else do other things.

junegunn commented 3 days ago

I'm not suggesting that you add "sleep" to your script, it doesn't make any sense. I was trying to demonstrate the effect of --no-clear.

tmpm697 commented 3 days ago

If sleep 2 does not make sense then check this:

test=$(echo "1\n2\n3\n" | fzf --expect 'ctrl-h,ctrl-l')

key=$(head -1 <<< $test)

if [[ $key == 'ctrl-h' ]]; then
  cat ./test.txt # this file is 10MB text file
fi

--no-clear will cause prompt swallow which fzf takes whole prompt and after fzf exit, prompt still black until user manually redraw screen, is there a way to use it without swallow prompt?