Skip to content
Myr

Revert "Add {*} placeholder flag"

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This reverts commit 27258f720723d5de32d483fb6b9ff39608ff9cf9.

authored by Junegunn Choi · committed by Junegunn Choi · parent 27258f7 · Browse files · 4 files+41 added−70 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+41 added−70 removed

  • tests:+0 added−10 removed
  • other:+41 added−60 removed

4 files+41 added−70 removed

modified:man/man1/fzf.1+5 added-7 removed
787 unmodified lines
788fzf also exports \fB$FZF_PREVIEW_TOP\fR and \fB$FZF_PREVIEW_LEFT\fR so that789the preview command can determine the position of the preview window.790791A placeholder expression starting with \fB+\fR flag will be replaced to the792space-separated list of the selected items (or the current item if no selection792space-separated list of the selected lines (or the current line if no selection793was made) individually quoted.794795e.g.796 \fBfzf \-\-multi \-\-preview='head \-10 {+}'797 git log \-\-oneline | fzf \-\-multi \-\-preview 'git show {+1}'\fR798799Similarly, a placeholder expression starting with \fB*\fR flag will be replaced800to the space-separated list of all matched items individually quoted.801799Each expression expands to a quoted string, so that it's safe to pass it as an800argument to an external command. So you should not manually add quotes around801the curly braces. But if you don't want this behavior, you can put802\fBr\fR flag (raw) in the expression (e.g. \fB{r}\fR, \fB{r1}\fR, etc).
3 unmodified lines
806from the replacement string. To preserve the whitespace, use the \fBs\fR flag.807808A placeholder expression with \fBf\fR flag is replaced to the path of809a temporary file that holds the evaluated list. This is useful when you813pass a large number of items and the length of the evaluated string may810multi-select a large number of items and the length of the evaluated string may811exceed \fBARG_MAX\fR.812813e.g.817 \fB# See the sum of all the matched numbers814 \fB# Press CTRL\-A to select 100K items and see the sum of all the numbers.815 # This won't work properly without 'f' flag due to ARG_MAX limit.819 seq 100000 | fzf \-\-preview "awk '{sum+=\\$1} END {print sum}' {*f}"\fR816 seq 100000 | fzf \-\-multi \-\-bind ctrl\-a:select\-all \\817 \-\-preview "awk '{sum+=\\$1} END {print sum}' {+f}"\fR818819Also,820821* \fB{q}\fR is replaced to the current query string
1172 unmodified lines
modified:src/terminal.go+36 added-48 removed
64 unmodified lines
65// After this duration, users can press CTRL-C to terminate the command.66const blockDuration = 1 * time.Second6768func init() {69 placeholder = regexp.MustCompile(`\\?(?:{[+*sfr]*[0-9,-.]*}|{q(?::s?[0-9,-.]+)?}|{fzf:(?:query|action|prompt)}|{\+?f?nf?})`)69 placeholder = regexp.MustCompile(`\\?(?:{[+sfr]*[0-9,-.]*}|{q(?::s?[0-9,-.]+)?}|{fzf:(?:query|action|prompt)}|{\+?f?nf?})`)70 whiteSuffix = regexp.MustCompile(`\s*$`)71 offsetComponentRegex = regexp.MustCompile(`([+-][0-9]+)|(-?/[1-9][0-9]*)`)72 offsetTrimCharsRegex = regexp.MustCompile(`[^0-9/+-]`)73
617 unmodified lines
691}692693type placeholderFlags struct {694 plus bool695 asterisk bool695 preserveSpace bool696 number bool697 forceUpdate bool698 file bool
13 unmodified lines
712713type previewRequest struct {714 template string715 scrollOffset int717 list [3][]*Item716 list []*Item717 env []string718 query string719}720
3377 unmodified lines
40984099 trimmed := ""4100 for _, char := range match[1:] {4101 switch char {4103 case '*':4104 flags.asterisk = true4102 case '+':4103 flags.plus = true4104 case 's':4105 flags.preserveSpace = true
15 unmodified lines
41214122 return false, matchWithoutFlags, flags4123}41244128func hasPreviewFlags(template string) (slot bool, plus bool, asterisk bool, forceUpdate bool) {4125func hasPreviewFlags(template string) (slot bool, plus bool, forceUpdate bool) {4126 for _, match := range placeholder.FindAllString(template, -1) {4127 escaped, _, flags := parsePlaceholder(match)4128 if escaped {4129 continue4130 }4131 if flags.plus {4132 plus = true4133 }4134 if flags.forceUpdate {4135 forceUpdate = true4136 }4137 slot = true4135 plus = plus || flags.plus4136 asterisk = asterisk || flags.asterisk4137 forceUpdate = forceUpdate || flags.forceUpdate4138 }4139 return4140}4141
3 unmodified lines
4145 delimiter Delimiter4146 printsep string4147 forcePlus bool4148 query string4149 allItems [3][]*Item // current, select, and all matched items4149 allItems []*Item4150 lastAction actionType4151 prompt string4152 executor *util.Executor4153}41544155func (t *Terminal) replacePlaceholderInInitialCommand(template string) (string, []string) {4156 return t.replacePlaceholder(template, false, string(t.input), [3][]*Item{nil, nil, nil})4156 return t.replacePlaceholder(template, false, string(t.input), []*Item{nil, nil})4157}41584159func (t *Terminal) replacePlaceholder(template string, forcePlus bool, input string, list [3][]*Item) (string, []string) {4159func (t *Terminal) replacePlaceholder(template string, forcePlus bool, input string, list []*Item) (string, []string) {4160 return replacePlaceholder(replacePlaceholderParams{4161 template: template,4162 stripAnsi: t.ansi,4163 delimiter: t.delimiter,
12 unmodified lines
4176 return 04177 }41784179 // We only need the current item to calculate the scroll offset4180 replaced, tempFiles := t.replacePlaceholder(t.activePreviewOpts.scroll, false, "", [3][]*Item{{t.currentItem()}, nil, nil})4180 replaced, tempFiles := t.replacePlaceholder(t.activePreviewOpts.scroll, false, "", []*Item{t.currentItem(), nil})4181 removeFiles(tempFiles)4182 offsetExpr := offsetTrimCharsRegex.ReplaceAllString(replaced, "")41834184 atoi := func(s string) int {
23 unmodified lines
4208}42094210func replacePlaceholder(params replacePlaceholderParams) (string, []string) {4211 tempFiles := []string{}4212 current := params.allItems[0]4213 selected := params.allItems[1]4214 matched := params.allItems[2]4212 current := params.allItems[:1]4213 selected := params.allItems[1:]4214 if current[0] == nil {4215 current = []*Item{}4216 }4217 if selected[0] == nil {4218 selected = []*Item{}4219 }42204221 // replace placeholders one by one4222 replaced := placeholder.ReplaceAllStringFunc(params.template, func(match string) string {4223 escaped, match, flags := parsePlaceholder(match)
87 unmodified lines
43114312 // apply 'replace' function over proper set of items and return result43134314 items := current4310 if flags.asterisk {4311 items = matched4312 } else if flags.plus || params.forcePlus {4315 if flags.plus || params.forcePlus {4316 items = selected4317 }4318 replacements := make([]string, len(items))4319
225 unmodified lines
4545 }4546 return nil4547}45484546func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, [3][]*Item) {4549func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item) {4550 current := t.currentItem()4548 slot, plus, asterisk, forceUpdate := hasPreviewFlags(template)4549 if !(!slot || forceUpdate || asterisk || (forcePlus || plus) && len(t.selected) > 0) {4550 if current == nil {4551 // Invalid4552 return false, [3][]*Item{nil, nil, nil}4553 }4554 return true, [3][]*Item{{current}, {current}, nil}4551 slot, plus, forceUpdate := hasPreviewFlags(template)4552 if !(!slot || forceUpdate || (forcePlus || plus) && len(t.selected) > 0) {4553 return current != nil, []*Item{current, current}4554 }45554556 // We would still want to update preview window even if there is no match if4557 // 1. the command template contains {q}
2 unmodified lines
4560 if current == nil {4561 current = &minItem4562 }45634565 var all []*Item4566 if asterisk {4567 cnt := t.merger.Length()4568 all = make([]*Item, cnt)4569 for i := 0; i < cnt; i++ {4570 item := t.merger.Get(i).item4571 all[i] = item4572 }4573 }45744564 var sels []*Item4565 if len(t.selected) == 0 {4577 sels = []*Item{current}4578 } else if len(t.selected) > 0 {4579 sels = make([]*Item, len(t.selected))4566 sels = []*Item{current, current}4567 } else {4568 sels = make([]*Item, len(t.selected)+1)4569 sels[0] = current4570 for i, sel := range t.sortSelected() {4581 sels[i] = sel.item4571 sels[i+1] = sel.item4572 }4573 }4584 return true, [3][]*Item{{current}, sels, all}4574 return true, sels4575}45764577func (t *Terminal) selectItem(item *Item) bool {4578 if len(t.selected) >= t.multi {
251 unmodified lines
4830 var version int644831 stop := false4832 t.previewBox.WaitFor(reqPreviewReady)4833 for {4844 requested := false4845 var items [3][]*Item4834 var items []*Item4835 var commandTemplate string4836 var env []string4837 var query string4838 initialOffset := 0
9 unmodified lines
4848 initialOffset = request.scrollOffset4849 items = request.list4850 env = request.env4851 query = request.query4863 requested = true4852 }4853 }4854 events.Clear()4855 })4856 if stop {4857 break4858 }4871 if !requested {4859 if items == nil {4860 continue4861 }4862 version++4863 // We don't display preview window if no match
1531 unmodified lines
6395 if !valid {6396 // We run the command even when there's no match6397 // 1. If the template doesn't have any slots6398 // 2. If the template has {q}6411 slot, _, _, forceUpdate := hasPreviewFlags(a.a)6399 slot, _, forceUpdate := hasPreviewFlags(a.a)6400 valid = !slot || forceUpdate6401 }6402 if valid {6403 command, tempFiles := t.replacePlaceholder(a.a, false, string(t.input), list)
180 unmodified lines
6584 req(reqList)6585 }65866587 if queryChanged && t.canPreview() && len(t.previewOpts.command) > 0 {6600 _, _, _, forceUpdate := hasPreviewFlags(t.previewOpts.command)6588 _, _, forceUpdate := hasPreviewFlags(t.previewOpts.command)6589 if forceUpdate {6590 t.version++6591 }6592 }
239 unmodified lines
modified:test/test_preview.rb-10 removed
187 unmodified lines
188 tmux.send_keys '3'189 tmux.until { |lines| assert_includes lines[1], ' {//1 10/1 10 /123//0 9} ' }190 end191192 def test_preview_asterisk193 tmux.send_keys %(seq 5 | #{FZF} --multi --preview 'echo {} / {+} / {*}'), :Enter194 tmux.until { |lines| assert_equal 5, lines.match_count }195 tmux.until { |lines| assert_includes lines[1], ' 1 / 1 / 1 2 3 4 5 ' }196 tmux.send_keys :BTab197 tmux.until { |lines| assert_includes lines[1], ' 2 / 1 / 1 2 3 4 5 ' }198 tmux.send_keys :BTab199 tmux.until { |lines| assert_includes lines[1], ' 3 / 1 2 / 1 2 3 4 5 ' }200 end201192 def test_preview_file193 tmux.send_keys %[(echo foo bar; echo bar foo) | #{FZF} --multi --preview 'cat {+f} {+f2} {+nf} {+fn}' --print0], :Enter194 tmux.until { |lines| assert_includes lines[1], ' foo barbar00 ' }195 tmux.send_keys :BTab
366 unmodified lines
modified:CHANGELOG.md-5 removed
1 unmodified line
2=========340.63.05------6- Added `{*}` placeholder flag that evaluates to all matched items.7 ```bash8 seq 10000 | fzf --preview "awk '{sum += \$1} END {print sum}' {*f}"9 ```10 - Use this with caution, as it can make fzf sluggish for large lists.6- Added background variants of transform actions with `bg-` prefix that run asynchronously in the background7 ```sh8 GETTER='curl -s http://metaphorpsum.com/sentences/1'9 fzf --style full --border --preview : \
2660 unmodified lines