diff --git a/CHANGELOG.md b/CHANGELOG.md index b9e2eb3d..6eba8cbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ CHANGELOG ========= +0.60.2 +------ +- Template for `--with-nth` and `--accept-nth` now supports `{n}` which evaluates to the zero-based ordinal index of the item. + 0.60.1 ------ - Bug fixes and minor improvements diff --git a/src/core.go b/src/core.go index 939910b3..d639769d 100644 --- a/src/core.go +++ b/src/core.go @@ -128,7 +128,7 @@ func Run(opts *Options) (int, error) { } } } - transformed := nthTransformer(tokens) + transformed := nthTransformer(tokens, itemIndex) if len(header) < opts.HeaderLines { header = append(header, transformed) eventBox.Set(EvtHeader, header) diff --git a/src/options.go b/src/options.go index 80079a4c..8a9e33ff 100644 --- a/src/options.go +++ b/src/options.go @@ -544,8 +544,8 @@ type Options struct { Case Case Normalize bool Nth []Range - WithNth func(Delimiter) func([]Token) string - AcceptNth func(Delimiter) func([]Token) string + WithNth func(Delimiter) func([]Token, int32) string + AcceptNth func(Delimiter) func([]Token, int32) string Delimiter Delimiter Sort int Track trackOption @@ -769,30 +769,31 @@ func splitNth(str string) ([]Range, error) { return ranges, nil } -func nthTransformer(str string) (func(Delimiter) func([]Token) string, error) { +func nthTransformer(str string) (func(Delimiter) func([]Token, int32) string, error) { // ^[0-9,-.]+$" if match, _ := regexp.MatchString("^[0-9,-.]+$", str); match { nth, err := splitNth(str) if err != nil { return nil, err } - return func(Delimiter) func([]Token) string { - return func(tokens []Token) string { + return func(Delimiter) func([]Token, int32) string { + return func(tokens []Token, index int32) string { return JoinTokens(Transform(tokens, nth)) } }, nil } // {...} {...} ... - placeholder := regexp.MustCompile("{[0-9,-.]+}") + placeholder := regexp.MustCompile("{[0-9,-.]+}|{n}") indexes := placeholder.FindAllStringIndex(str, -1) if indexes == nil { return nil, errors.New("template should include at least 1 placeholder: " + str) } type NthParts struct { - str string - nth []Range + str string + index bool + nth []Range } parts := make([]NthParts, len(indexes)) @@ -801,7 +802,10 @@ func nthTransformer(str string) (func(Delimiter) func([]Token) string, error) { if idx < index[0] { parts = append(parts, NthParts{str: str[idx:index[0]]}) } - if nth, err := splitNth(str[index[0]+1 : index[1]-1]); err == nil { + expr := str[index[0]+1 : index[1]-1] + if expr == "n" { + parts = append(parts, NthParts{index: true}) + } else if nth, err := splitNth(expr); err == nil { parts = append(parts, NthParts{nth: nth}) } idx = index[1] @@ -810,12 +814,16 @@ func nthTransformer(str string) (func(Delimiter) func([]Token) string, error) { parts = append(parts, NthParts{str: str[idx:]}) } - return func(delimiter Delimiter) func([]Token) string { - return func(tokens []Token) string { + return func(delimiter Delimiter) func([]Token, int32) string { + return func(tokens []Token, index int32) string { str := "" for _, holder := range parts { if holder.nth != nil { str += StripLastDelimiter(JoinTokens(Transform(tokens, holder.nth)), delimiter) + } else if holder.index { + if index >= 0 { + str += strconv.Itoa(int(index)) + } } else { str += holder.str } diff --git a/src/terminal.go b/src/terminal.go index bf74b77b..03d90064 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -305,7 +305,7 @@ type Terminal struct { nthAttr tui.Attr nth []Range nthCurrent []Range - acceptNth func([]Token) string + acceptNth func([]Token, int32) string tabstop int margin [4]sizeSpec padding [4]sizeSpec @@ -1576,7 +1576,7 @@ func (t *Terminal) output() bool { if t.acceptNth != nil { transform = func(item *Item) string { tokens := Tokenize(item.AsString(t.ansi), t.delimiter) - transformed := t.acceptNth(tokens) + transformed := t.acceptNth(tokens, item.Index()) return StripLastDelimiter(transformed, t.delimiter) } } diff --git a/test/test_core.rb b/test/test_core.rb index eb60e815..a68a8e95 100644 --- a/test/test_core.rb +++ b/test/test_core.rb @@ -1801,11 +1801,11 @@ class TestCore < TestInteractive end def test_accept_nth_template - tmux.send_keys %(echo "foo ,bar,baz" | #{FZF} -d, --accept-nth '1st: {1}, 3rd: {3}, 2nd: {2}' --sync --bind start:accept > #{tempname}), :Enter + tmux.send_keys %(echo "foo ,bar,baz" | #{FZF} -d, --accept-nth '[{n}] 1st: {1}, 3rd: {3}, 2nd: {2}' --sync --bind start:accept > #{tempname}), :Enter wait do assert_path_exists tempname # Last delimiter and the whitespaces are removed - assert_equal ['1st: foo, 3rd: baz, 2nd: bar'], File.readlines(tempname, chomp: true) + assert_equal ['[0] 1st: foo, 3rd: baz, 2nd: bar'], File.readlines(tempname, chomp: true) end end end