From 2b584586ed1caf15429625da981575ee35d407b8 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 9 Feb 2025 11:53:35 +0900 Subject: Add --accept-nth option to transform the output This option can be used to replace a sed or awk in the post-processing step. ps -ef | fzf --multi --header-lines 1 | awk '{print $2}' ps -ef | fzf --multi --header-lines 1 --accept-nth 2 This may not be a very "Unix-y" thing to do, so I've always felt that fzf shouldn't have such an option, but I've finally changed my mind because: * fzf can be configured with a custom delimiter that is a fixed string or a regular expression. * In such cases, you'd need to repeat the delimiter again in the post-processing step. * Also, tools like awk or sed may interpret a regular expression differently, causing mismatches. You can still use sed, cut, or awk if you prefer. Close #3987 Close #1323 --- test/test_core.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'test/test_core.rb') diff --git a/test/test_core.rb b/test/test_core.rb index 276fa03d..e15ab8ee 100644 --- a/test/test_core.rb +++ b/test/test_core.rb @@ -1665,4 +1665,30 @@ class TestCore < TestInteractive assert_equal '', File.read(tempname).chomp end end + + def test_accept_nth + tmux.send_keys %((echo "foo bar baz"; echo "bar baz foo") | #{FZF} --multi --accept-nth 2,2 --sync --bind start:select-all+accept > #{tempname}), :Enter + wait do + assert_path_exists tempname + assert_equal ['bar bar', 'baz baz'], File.readlines(tempname, chomp: true) + end + end + + def test_accept_nth_string_delimiter + tmux.send_keys %(echo "foo ,bar,baz" | #{FZF} -d, --accept-nth 2,2,1,3,1 --sync --bind start:accept > #{tempname}), :Enter + wait do + assert_path_exists tempname + # Last delimiter and the whitespaces are removed + assert_equal ['bar,bar,foo ,bazfoo'], File.readlines(tempname, chomp: true) + end + end + + def test_accept_nth_regex_delimiter + tmux.send_keys %(echo "foo :,:bar,baz" | #{FZF} --delimiter='[:,]+' --accept-nth 2,2,1,3,1 --sync --bind start:accept > #{tempname}), :Enter + wait do + assert_path_exists tempname + # Last delimiter and the whitespaces are removed + assert_equal ['bar,bar,foo :,:bazfoo'], File.readlines(tempname, chomp: true) + end + end end -- cgit v1.2.3