Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fish] Allow setting multi-select and list reload for history #4179

Merged
merged 3 commits into from
Jan 18, 2025

Conversation

bitraid
Copy link
Contributor

@bitraid bitraid commented Jan 14, 2025

The purpose of this PR is twofold:

  • Allow users to extend the history widget functionality, so that it can delete selected items and reload the list, with something like:
set -gx FZF_CTRL_R_OPTS '--multi --bind=\'ctrl-x:execute(eval history delete -- (string escape -- {+2..} | string replace -a "\n\t" "\n"))+reload(eval $FZF_DEFAULT_COMMAND)\''
  • Make the loading of history items non-blocking.

@bitraid
Copy link
Contributor Author

bitraid commented Jan 14, 2025

The tests fail because the -f switch of set didn't exist in older versions (I forgot about that). I'll fix it tomorrow.

@bitraid
Copy link
Contributor Author

bitraid commented Jan 15, 2025

There seems to be some other issues, too (also with <CTRL-T> ???). I'll have to have closer look.

@junegunn
Copy link
Owner

Thank you. I'm not a fish user, so I can't really say, but if you think there's no point in supporting older versions of fish, because it makes the code overly complex and few people actually use such old versions, let me know.

@bitraid
Copy link
Contributor Author

bitraid commented Jan 15, 2025

I just figured out why <CTRL-T> was failing. In the last commit I tried to fix a bug (which I forgot to document) where if an environment variable called result existed, the output of fzf would include the contents of the variable, and the results of fzf would be added to the variable (in the script the variable was not explicitly set as local). I now have a proper fix, but I still have to find the issue with <CTRL-R> tests, which probably doesn't have to do with the compatibility changes.

@bitraid
Copy link
Contributor Author

bitraid commented Jan 16, 2025

Apparently, using $FZF_DEFAULT_COMMAND for history causes the tests to fail.
I also have some issues running the tests locally, which fail for fish, even without any changes. This is strange, because the last time I ran them a while back, I didn't have this issue. I'll try to resolve it and figure out what's wrong.

@junegunn
Copy link
Owner

Apparently, using $FZF_DEFAULT_COMMAND for history causes the tests to fail.

Maybe it's because $SHELL is not set to fish on the test environment? Would adding --with-shell "fish -c" help?

@bitraid
Copy link
Contributor Author

bitraid commented Jan 16, 2025

Apparently, using $FZF_DEFAULT_COMMAND for history causes the tests to fail.

Maybe it's because $SHELL is not set to fish on the test environment? Would adding --with-shell "fish -c" help?

I originally (before the forced push of only the first commit) had a check (in the second commit) that added this parameter when $SHELL wasn't fish, but I forgot about it, and now I realize that it was also missing the -c switch.

What is really confusing though, is that even with that correction, running the tests locally, they inconsistently fail most of the time with:

  1) Failure:
TestFish#test_ctrl_r_multiline [test/test_go.rb:3877]:
Expected false to be truthy.

I don't know why this happens, or why it passes some times. I must look more into it.

@junegunn
Copy link
Owner

A couple observations.

The test (ruby test/test_go.rb --name test_ctrl_r_multiline) never passes without --with-shell option on my system. But even if I add it, it works only sometimes, as you also noticed.

diff --git a/shell/key-bindings.fish b/shell/key-bindings.fish
index 9039348..7f0931e 100644
--- a/shell/key-bindings.fish
+++ b/shell/key-bindings.fish
@@ -84,7 +84,7 @@ function fzf_key_bindings
             'string join0 -- $i\t(string replace -a -- \n \n\t $h[$i] | string collect);' \
             'end'
         end
-        eval (__fzfcmd) --read0 --print0 -q '(commandline)' | string replace -r '^\d*\t' '' | read -lz result
+        eval (__fzfcmd) --with-shell 'fish\ -c' --read0 --print0 -q '(commandline)' | string replace -r '^\d*\t' '' | read -lz result
         and commandline -- $result
       else
         builtin history | eval (__fzfcmd) -q '(commandline)' | read -l result

So I tried to see what's really going on by binding a reload action to re-evaluate $FZF_DEFAULT_COMMAND, and I noticed when I press 'space' myself on the tmux pane, the list correctly populates and the test passes. So it seems like a fish issue to me.

diff --git a/shell/key-bindings.fish b/shell/key-bindings.fish
index 9039348..f0fb45f 100644
--- a/shell/key-bindings.fish
+++ b/shell/key-bindings.fish
@@ -84,7 +84,7 @@ function fzf_key_bindings
             'string join0 -- $i\t(string replace -a -- \n \n\t $h[$i] | string collect);' \
             'end'
         end
-        eval (__fzfcmd) --read0 --print0 -q '(commandline)' | string replace -r '^\d*\t' '' | read -lz result
+        fzf --with-shell 'fish -c' --bind 'space:reload:eval $FZF_DEFAULT_COMMAND' --read0 --print0 -q (commandline) | string replace -r '^\d*\t' '' | read -lz result
         and commandline -- $result
       else
         builtin history | eval (__fzfcmd) -q '(commandline)' | read -l result

@bitraid
Copy link
Contributor Author

bitraid commented Jan 17, 2025

I don't like this workaround - closing.

@bitraid bitraid closed this Jan 17, 2025
@bitraid
Copy link
Contributor Author

bitraid commented Jan 18, 2025

I reopened this because upon closer testing 0a10d14, I realized that it was still blocking.
As it turns out, even though these command are non-blocking:

set -l cmd fzf
begin echo start; sleep 3; echo end; end | $cmd
$cmd | read -l result

This becomes blocking:

begin echo start; sleep 3; echo end; end | fzf | read -l result

Upon further testing, I figured that this is non-blocking:

set -l result (begin echo start; sleep 3; echo end; end | fzf)

So I adjusted this PR with those changes, and everything is working fine locally, with the test consistently passing.
I also removed the support for fish versions older that 3.0b1 to simplify the code (the script was using set -a which was not supported by those versions anyway).

But the tests here now fail with:

  1) Failure:
TestFish#test_ctrl_r_abort [test/test_go.rb:3929]:
Expected: "> foo"
  Actual: "foo"

Any idea why this might be happening?

@bitraid
Copy link
Contributor Author

bitraid commented Jan 18, 2025

I even ran the tests a few times on a VM with ubuntu-24.04.1 (same as GitHub actions): They always pass...

@junegunn
Copy link
Owner

I reran the task, and the test passed this time. And it constantly passes on my system. I'll see if there can be some timing issues, but anyway I think this is ready for merge. Thanks.

@junegunn junegunn merged commit 5a32634 into junegunn:master Jan 18, 2025
5 checks passed
@bitraid bitraid deleted the fish-hist branch January 18, 2025 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants