Skip to content

Commit 8d68852

Browse files
authored
Fix --accept-nth being ignored in filter mode (#4636)
The --accept-nth option was not being respected when using --filter mode. This caused fzf to output entire lines instead of only the specified fields. Added buildItemTransformer() helper function to consistently apply field transformations across filter mode (both streaming and non-streaming) and select1/exit0 modes. Fixes #4615
1 parent 7751293 commit 8d68852

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/core.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ func (r revision) compatible(other revision) bool {
3838
return r.major == other.major
3939
}
4040

41+
func buildItemTransformer(opts *Options) func(*Item) string {
42+
if opts.AcceptNth != nil {
43+
fn := opts.AcceptNth(opts.Delimiter)
44+
return func(item *Item) string {
45+
return item.acceptNth(opts.Ansi, opts.Delimiter, fn)
46+
}
47+
}
48+
return func(item *Item) string {
49+
return item.AsString(opts.Ansi)
50+
}
51+
}
52+
4153
// Run starts fzf
4254
func Run(opts *Options) (int, error) {
4355
if opts.Filter == nil {
@@ -243,6 +255,8 @@ func Run(opts *Options) (int, error) {
243255
pattern := patternBuilder([]rune(*opts.Filter))
244256
matcher.sort = pattern.sortable
245257

258+
transformer := buildItemTransformer(opts)
259+
246260
found := false
247261
if streamingFilter {
248262
slab := util.MakeSlab(slab16Size, slab32Size)
@@ -253,7 +267,7 @@ func Run(opts *Options) (int, error) {
253267
if chunkList.trans(&item, runes) {
254268
mutex.Lock()
255269
if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil {
256-
opts.Printer(item.text.ToString())
270+
opts.Printer(transformer(&item))
257271
found = true
258272
}
259273
mutex.Unlock()
@@ -271,7 +285,7 @@ func Run(opts *Options) (int, error) {
271285
chunks: snapshot,
272286
pattern: pattern})
273287
for i := 0; i < result.merger.Length(); i++ {
274-
opts.Printer(result.merger.Get(i).item.AsString(opts.Ansi))
288+
opts.Printer(transformer(result.merger.Get(i).item))
275289
found = true
276290
}
277291
}
@@ -493,15 +507,7 @@ func Run(opts *Options) (int, error) {
493507
if len(opts.Expect) > 0 {
494508
opts.Printer("")
495509
}
496-
transformer := func(item *Item) string {
497-
return item.AsString(opts.Ansi)
498-
}
499-
if opts.AcceptNth != nil {
500-
fn := opts.AcceptNth(opts.Delimiter)
501-
transformer = func(item *Item) string {
502-
return item.acceptNth(opts.Ansi, opts.Delimiter, fn)
503-
}
504-
}
510+
transformer := buildItemTransformer(opts)
505511
for i := range count {
506512
opts.Printer(transformer(merger.Get(i).item))
507513
}

test/test_filter.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,18 @@ def test_boundary_match
312312
assert_equal expected, result
313313
end
314314
end
315+
316+
def test_accept_nth
317+
# Single field selection
318+
assert_equal 'three', `echo 'one two three' | #{FZF} -d' ' --with-nth 1 --accept-nth -1 -f one`.chomp
319+
320+
# Multiple field selection
321+
writelines(['ID001:John:Developer', 'ID002:Jane:Manager', 'ID003:Bob:Designer'])
322+
assert_equal 'ID001', `#{FZF} -d: --with-nth 2 --accept-nth 1 -f John < #{tempname}`.chomp
323+
assert_equal "ID002:Manager", `#{FZF} -d: --with-nth 2 --accept-nth 1,3 -f Jane < #{tempname}`.chomp
324+
325+
# Test with different delimiters
326+
writelines(['emp001 Alice Engineering', 'emp002 Bob Marketing'])
327+
assert_equal 'emp001', `#{FZF} -d' ' --with-nth 2 --accept-nth 1 -f Alice < #{tempname}`.chomp
328+
end
315329
end

0 commit comments

Comments
 (0)