summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2025-02-23 19:47:56 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2025-02-23 19:47:56 +0900
commit461115afde3041cff31c27e490d7c28c0b6c28c4 (patch)
treeaaec0ce4a82dd50842ea9b5bd49e59135705fc79 /src
parentbae196523189988d9d5fa2a13880f9b1f36dedf0 (diff)
downloadfzf-461115afde3041cff31c27e490d7c28c0b6c28c4.tar.gz
Add support for {n} in --with-nth and --accept-nth templates
Close #4275
Diffstat (limited to 'src')
-rw-r--r--src/core.go2
-rw-r--r--src/options.go30
-rw-r--r--src/terminal.go4
3 files changed, 22 insertions, 14 deletions
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)
}
}