mirror of
https://github.com/junegunn/fzf.git
synced 2025-11-14 14:23:47 -05:00
Add support for {n} in --with-nth and --accept-nth templates
Close #4275
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
CHANGELOG
|
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
|
0.60.1
|
||||||
------
|
------
|
||||||
- Bug fixes and minor improvements
|
- Bug fixes and minor improvements
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ func Run(opts *Options) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
transformed := nthTransformer(tokens)
|
transformed := nthTransformer(tokens, itemIndex)
|
||||||
if len(header) < opts.HeaderLines {
|
if len(header) < opts.HeaderLines {
|
||||||
header = append(header, transformed)
|
header = append(header, transformed)
|
||||||
eventBox.Set(EvtHeader, header)
|
eventBox.Set(EvtHeader, header)
|
||||||
|
|||||||
@@ -544,8 +544,8 @@ type Options struct {
|
|||||||
Case Case
|
Case Case
|
||||||
Normalize bool
|
Normalize bool
|
||||||
Nth []Range
|
Nth []Range
|
||||||
WithNth func(Delimiter) func([]Token) string
|
WithNth func(Delimiter) func([]Token, int32) string
|
||||||
AcceptNth func(Delimiter) func([]Token) string
|
AcceptNth func(Delimiter) func([]Token, int32) string
|
||||||
Delimiter Delimiter
|
Delimiter Delimiter
|
||||||
Sort int
|
Sort int
|
||||||
Track trackOption
|
Track trackOption
|
||||||
@@ -769,30 +769,31 @@ func splitNth(str string) ([]Range, error) {
|
|||||||
return ranges, nil
|
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,-.]+$"
|
// ^[0-9,-.]+$"
|
||||||
if match, _ := regexp.MatchString("^[0-9,-.]+$", str); match {
|
if match, _ := regexp.MatchString("^[0-9,-.]+$", str); match {
|
||||||
nth, err := splitNth(str)
|
nth, err := splitNth(str)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return func(Delimiter) func([]Token) string {
|
return func(Delimiter) func([]Token, int32) string {
|
||||||
return func(tokens []Token) string {
|
return func(tokens []Token, index int32) string {
|
||||||
return JoinTokens(Transform(tokens, nth))
|
return JoinTokens(Transform(tokens, nth))
|
||||||
}
|
}
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// {...} {...} ...
|
// {...} {...} ...
|
||||||
placeholder := regexp.MustCompile("{[0-9,-.]+}")
|
placeholder := regexp.MustCompile("{[0-9,-.]+}|{n}")
|
||||||
indexes := placeholder.FindAllStringIndex(str, -1)
|
indexes := placeholder.FindAllStringIndex(str, -1)
|
||||||
if indexes == nil {
|
if indexes == nil {
|
||||||
return nil, errors.New("template should include at least 1 placeholder: " + str)
|
return nil, errors.New("template should include at least 1 placeholder: " + str)
|
||||||
}
|
}
|
||||||
|
|
||||||
type NthParts struct {
|
type NthParts struct {
|
||||||
str string
|
str string
|
||||||
nth []Range
|
index bool
|
||||||
|
nth []Range
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := make([]NthParts, len(indexes))
|
parts := make([]NthParts, len(indexes))
|
||||||
@@ -801,7 +802,10 @@ func nthTransformer(str string) (func(Delimiter) func([]Token) string, error) {
|
|||||||
if idx < index[0] {
|
if idx < index[0] {
|
||||||
parts = append(parts, NthParts{str: str[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})
|
parts = append(parts, NthParts{nth: nth})
|
||||||
}
|
}
|
||||||
idx = index[1]
|
idx = index[1]
|
||||||
@@ -810,12 +814,16 @@ func nthTransformer(str string) (func(Delimiter) func([]Token) string, error) {
|
|||||||
parts = append(parts, NthParts{str: str[idx:]})
|
parts = append(parts, NthParts{str: str[idx:]})
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(delimiter Delimiter) func([]Token) string {
|
return func(delimiter Delimiter) func([]Token, int32) string {
|
||||||
return func(tokens []Token) string {
|
return func(tokens []Token, index int32) string {
|
||||||
str := ""
|
str := ""
|
||||||
for _, holder := range parts {
|
for _, holder := range parts {
|
||||||
if holder.nth != nil {
|
if holder.nth != nil {
|
||||||
str += StripLastDelimiter(JoinTokens(Transform(tokens, holder.nth)), delimiter)
|
str += StripLastDelimiter(JoinTokens(Transform(tokens, holder.nth)), delimiter)
|
||||||
|
} else if holder.index {
|
||||||
|
if index >= 0 {
|
||||||
|
str += strconv.Itoa(int(index))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
str += holder.str
|
str += holder.str
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -305,7 +305,7 @@ type Terminal struct {
|
|||||||
nthAttr tui.Attr
|
nthAttr tui.Attr
|
||||||
nth []Range
|
nth []Range
|
||||||
nthCurrent []Range
|
nthCurrent []Range
|
||||||
acceptNth func([]Token) string
|
acceptNth func([]Token, int32) string
|
||||||
tabstop int
|
tabstop int
|
||||||
margin [4]sizeSpec
|
margin [4]sizeSpec
|
||||||
padding [4]sizeSpec
|
padding [4]sizeSpec
|
||||||
@@ -1576,7 +1576,7 @@ func (t *Terminal) output() bool {
|
|||||||
if t.acceptNth != nil {
|
if t.acceptNth != nil {
|
||||||
transform = func(item *Item) string {
|
transform = func(item *Item) string {
|
||||||
tokens := Tokenize(item.AsString(t.ansi), t.delimiter)
|
tokens := Tokenize(item.AsString(t.ansi), t.delimiter)
|
||||||
transformed := t.acceptNth(tokens)
|
transformed := t.acceptNth(tokens, item.Index())
|
||||||
return StripLastDelimiter(transformed, t.delimiter)
|
return StripLastDelimiter(transformed, t.delimiter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1801,11 +1801,11 @@ class TestCore < TestInteractive
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_accept_nth_template
|
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
|
wait do
|
||||||
assert_path_exists tempname
|
assert_path_exists tempname
|
||||||
# Last delimiter and the whitespaces are removed
|
# 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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user