diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2019-02-19 01:12:57 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2019-02-19 01:12:57 +0900 |
commit | 6c32148f9020e222bae335fa823229715e067815 (patch) | |
tree | 402d0735161514145b32ca64e7962727d060a963 | |
parent | 315e568de006e80138f79c77d5508c7e4853e6b2 (diff) |
Add placeholder expression for zero-based item index: {n} and {+n}
Close #1482
-rw-r--r-- | man/man1/fzf.1 | 4 | ||||
-rw-r--r-- | src/terminal.go | 19 | ||||
-rwxr-xr-x | test/test_go.rb | 16 |
3 files changed, 27 insertions, 12 deletions
diff --git a/man/man1/fzf.1 b/man/man1/fzf.1 index 68a387c2..2d00cb84 100644 --- a/man/man1/fzf.1 +++ b/man/man1/fzf.1 @@ -301,7 +301,9 @@ e.g. \fBfzf --multi --preview='head -10 {+}'\fR When using a field index expression, leading and trailing whitespace is stripped from the replacement string. To preserve the whitespace, use the \fBs\fR flag. -Also, \fB{q}\fR is replaced to the current query string. +Also, \fB{q}\fR is replaced to the current query string, and \fB{n}\fR is +replaced to zero-based ordinal index of the line. Use \fB{+n}\fR if you want +all index numbers when multiple lines are selected. Note that you can escape a placeholder pattern by prepending a backslash. diff --git a/src/terminal.go b/src/terminal.go index b2b38254..c807a4ba 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -24,7 +24,7 @@ import ( var placeholder *regexp.Regexp func init() { - placeholder = regexp.MustCompile("\\\\?(?:{[+s]*[0-9,-.]*}|{q})") + placeholder = regexp.MustCompile("\\\\?(?:{[+s]*[0-9,-.]*}|{q}|{\\+?n})") } type jumpMode int @@ -227,6 +227,7 @@ const ( type placeholderFlags struct { plus bool preserveSpace bool + number bool query bool } @@ -1198,6 +1199,9 @@ func parsePlaceholder(match string) (bool, string, placeholderFlags) { case 's': flags.preserveSpace = true skipChars++ + case 'n': + flags.number = true + skipChars++ case 'q': flags.query = true default: @@ -1253,7 +1257,16 @@ func replacePlaceholder(template string, stripAnsi bool, delimiter Delimiter, fo if match == "{}" { for idx, item := range items { - replacements[idx] = quoteEntry(item.AsString(stripAnsi)) + if flags.number { + n := int(item.text.Index) + if n < 0 { + replacements[idx] = "" + } else { + replacements[idx] = strconv.Itoa(n) + } + } else { + replacements[idx] = quoteEntry(item.AsString(stripAnsi)) + } } return strings.Join(replacements, " ") } @@ -1351,7 +1364,7 @@ func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item // 2. or it contains {+} and we have more than one item already selected. // To do so, we pass an empty Item instead of nil to trigger an update. if current == nil { - current = &Item{} + current = &minItem } var sels []*Item diff --git a/test/test_go.rb b/test/test_go.rb index c34a9259..df333c08 100755 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -1400,21 +1400,21 @@ class TestGoFZF < TestBase def test_preview_flags tmux.send_keys %(seq 10 | sed 's/^/:: /; s/$/ /' | - #{FZF} --multi --preview 'echo {{2}/{s2}/{+2}/{+s2}/{q}}'), :Enter - tmux.until { |lines| lines[1].include?('{1/1 /1/1 /}') } + #{FZF} --multi --preview 'echo {{2}/{s2}/{+2}/{+s2}/{q}/{n}/{+n}}'), :Enter + tmux.until { |lines| lines[1].include?('{1/1 /1/1 //0/0}') } tmux.send_keys '123' - tmux.until { |lines| lines[1].include?('{////123}') } + tmux.until { |lines| lines[1].include?('{////123//}') } tmux.send_keys 'C-u', '1' tmux.until { |lines| lines.match_count == 2 } - tmux.until { |lines| lines[1].include?('{1/1 /1/1 /1}') } + tmux.until { |lines| lines[1].include?('{1/1 /1/1 /1/0/0}') } tmux.send_keys :BTab - tmux.until { |lines| lines[1].include?('{10/10 /1/1 /1}') } + tmux.until { |lines| lines[1].include?('{10/10 /1/1 /1/9/0}') } tmux.send_keys :BTab - tmux.until { |lines| lines[1].include?('{10/10 /1 10/1 10 /1}') } + tmux.until { |lines| lines[1].include?('{10/10 /1 10/1 10 /1/9/0 9}') } tmux.send_keys '2' - tmux.until { |lines| lines[1].include?('{//1 10/1 10 /12}') } + tmux.until { |lines| lines[1].include?('{//1 10/1 10 /12//0 9}') } tmux.send_keys '3' - tmux.until { |lines| lines[1].include?('{//1 10/1 10 /123}') } + tmux.until { |lines| lines[1].include?('{//1 10/1 10 /123//0 9}') } end def test_preview_q_no_match |