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
Provenance
- Commit
16d338d- Tree
cf88b19- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
27258f7
4 files changed+41 added−70 removed
modified:man/man1/fzf.1+5 added-7 removed
787 unmodified lines788 fzf also exports \fB$FZF_PREVIEW_TOP\fR and \fB$FZF_PREVIEW_LEFT\fR so that789 the preview command can determine the position of the preview window.790 791 A placeholder expression starting with \fB+\fR flag will be replaced to the792-space-separated list of the selected items (or the current item if no selection792+space-separated list of the selected lines (or the current line if no selection793 was made) individually quoted.794 795 e.g.796 \fBfzf \-\-multi \-\-preview='head \-10 {+}'797 git log \-\-oneline | fzf \-\-multi \-\-preview 'git show {+1}'\fR798 799-Similarly, a placeholder expression starting with \fB*\fR flag will be replaced800-to the space-separated list of all matched items individually quoted.801-799 Each expression expands to a quoted string, so that it's safe to pass it as an800 argument to an external command. So you should not manually add quotes around801 the 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 lines806 from the replacement string. To preserve the whitespace, use the \fBs\fR flag.807 808 A placeholder expression with \fBf\fR flag is replaced to the path of809 a temporary file that holds the evaluated list. This is useful when you813-pass a large number of items and the length of the evaluated string may810+multi-select a large number of items and the length of the evaluated string may811 exceed \fBARG_MAX\fR.812 813 e.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}"\fR818 819 Also,820 821 * \fB{q}\fR is replaced to the current query string1172 unmodified linesmodified:src/terminal.go+36 added-48 removed
64 unmodified lines65 // After this duration, users can press CTRL-C to terminate the command.66 const blockDuration = 1 * time.Second67 68 func 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 lines691 }692 693 type placeholderFlags struct {694 plus bool695- asterisk bool695 preserveSpace bool696 number bool697 forceUpdate bool698 file bool13 unmodified lines712 713 type previewRequest struct {714 template string715 scrollOffset int717- list [3][]*Item716+ list []*Item717 env []string718 query string719 }720 3377 unmodified lines4098 4099 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 = true15 unmodified lines4121 4122 return false, matchWithoutFlags, flags4123 }4124 4128-func hasPreviewFlags(template string) (slot bool, plus bool, asterisk bool, forceUpdate bool) {4125+func 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 lines4145 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 }4154 4155 func (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 }4158 4159-func (t *Terminal) replacePlaceholder(template string, forcePlus bool, input string, list [3][]*Item) (string, []string) {4159+func (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 lines4176 return 04177 }4178 4179 // 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, "")4183 4184 atoi := func(s string) int {23 unmodified lines4208 }4209 4210 func 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+ }4220 4221 // replace placeholders one by one4222 replaced := placeholder.ReplaceAllStringFunc(params.template, func(match string) string {4223 escaped, match, flags := parsePlaceholder(match)87 unmodified lines4311 4312 // apply 'replace' function over proper set of items and return result4313 4314 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 lines4545 }4546 return nil4547 }4548 4546-func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, [3][]*Item) {4549+func (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 }4555 4556 // We would still want to update preview window even if there is no match if4557 // 1. the command template contains {q}2 unmodified lines4560 if current == nil {4561 current = &minItem4562 }4563 4565- 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- }4574-4564 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 }4576 4577 func (t *Terminal) selectItem(item *Item) bool {4578 if len(t.selected) >= t.multi {251 unmodified lines4830 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 := 09 unmodified lines4848 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 match1531 unmodified lines6395 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 lines6584 req(reqList)6585 }6586 6587 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 linesmodified:test/test_preview.rb-10 removed
187 unmodified lines188 tmux.send_keys '3'189 tmux.until { |lines| assert_includes lines[1], ' {//1 10/1 10 /123//0 9} ' }190 end191 192- 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- end201-192 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 :BTab366 unmodified linesmodified:CHANGELOG.md-5 removed
1 unmodified line2 =========3 4 0.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