Add {*} placeholder flag
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 4d2d6a5 · Browse files · 4 files·+70 added−41 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+70 added−41 removed
- tests:+10 added−0 removed
- other:+60 added−41 removed
Provenance
- Commit
27258f7- Tree
f062088- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
4d2d6a5
4 files changed+70 added−41 removed
modified:man/man1/fzf.1+7 added-5 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 lines (or the current line if no selection792+space-separated list of the selected items (or the current item 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+802 Each expression expands to a quoted string, so that it's safe to pass it as an803 argument to an external command. So you should not manually add quotes around804 the curly braces. But if you don't want this behavior, you can put805 \fBr\fR flag (raw) in the expression (e.g. \fB{r}\fR, \fB{r1}\fR, etc).3 unmodified lines809 from the replacement string. To preserve the whitespace, use the \fBs\fR flag.810 811 A placeholder expression with \fBf\fR flag is replaced to the path of812 a temporary file that holds the evaluated list. This is useful when you810-multi-select a large number of items and the length of the evaluated string may813+pass a large number of items and the length of the evaluated string may814 exceed \fBARG_MAX\fR.815 816 e.g.814- \fB# Press CTRL\-A to select 100K items and see the sum of all the numbers.817+ \fB# See the sum of all the matched numbers818 # This won't work properly without 'f' flag due to ARG_MAX limit.816- seq 100000 | fzf \-\-multi \-\-bind ctrl\-a:select\-all \\817- \-\-preview "awk '{sum+=\\$1} END {print sum}' {+f}"\fR819+ seq 100000 | fzf \-\-preview "awk '{sum+=\\$1} END {print sum}' {*f}"\fR820 821 Also,822 823 * \fB{q}\fR is replaced to the current query string1172 unmodified linesmodified:src/terminal.go+48 added-36 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 bool696 preserveSpace bool697 number bool698 forceUpdate bool699 file bool13 unmodified lines713 714 type previewRequest struct {715 template string716 scrollOffset int716- list []*Item717+ list [3][]*Item718 env []string719 query string720 }721 3377 unmodified lines4099 4100 trimmed := ""4101 for _, char := range match[1:] {4102 switch char {4103+ case '*':4104+ flags.asterisk = true4105 case '+':4106 flags.plus = true4107 case 's':4108 flags.preserveSpace = true15 unmodified lines4124 4125 return false, matchWithoutFlags, flags4126 }4127 4125-func hasPreviewFlags(template string) (slot bool, plus bool, forceUpdate bool) {4128+func hasPreviewFlags(template string) (slot bool, plus bool, asterisk bool, forceUpdate bool) {4129 for _, match := range placeholder.FindAllString(template, -1) {4130 escaped, _, flags := parsePlaceholder(match)4131 if escaped {4132 continue4133 }4131- if flags.plus {4132- plus = true4133- }4134- if flags.forceUpdate {4135- forceUpdate = true4136- }4134 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 []*Item4149+ allItems [3][]*Item // current, select, and all matched items4150 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), []*Item{nil, nil})4156+ return t.replacePlaceholder(template, false, string(t.input), [3][]*Item{nil, nil, nil})4157 }4158 4159-func (t *Terminal) replacePlaceholder(template string, forcePlus bool, input string, list []*Item) (string, []string) {4159+func (t *Terminal) replacePlaceholder(template string, forcePlus bool, input string, list [3][]*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, "", []*Item{t.currentItem(), nil})4180+ replaced, tempFiles := t.replacePlaceholder(t.activePreviewOpts.scroll, false, "", [3][]*Item{{t.currentItem()}, nil, 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[:1]4213- selected := params.allItems[1:]4214- if current[0] == nil {4215- current = []*Item{}4216- }4217- if selected[0] == nil {4218- selected = []*Item{}4219- }4212+ current := params.allItems[0]4213+ selected := params.allItems[1]4214+ matched := params.allItems[2]4215 4216 // replace placeholders one by one4217 replaced := placeholder.ReplaceAllStringFunc(params.template, func(match string) string {4218 escaped, match, flags := parsePlaceholder(match)87 unmodified lines4306 4307 // apply 'replace' function over proper set of items and return result4308 4309 items := current4310+ if flags.asterisk {4311+ items = matched4315- if flags.plus || params.forcePlus {4312+ } else if flags.plus || params.forcePlus {4313 items = selected4314 }4315 replacements := make([]string, len(items))4316 225 unmodified lines4542 }4543 return nil4544 }4545 4549-func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item) {4546+func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, [3][]*Item) {4547 current := t.currentItem()4551- slot, plus, forceUpdate := hasPreviewFlags(template)4552- if !(!slot || forceUpdate || (forcePlus || plus) && len(t.selected) > 0) {4553- return current != nil, []*Item{current, current}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}4555 }4556 4557 // We would still want to update preview window even if there is no match if4558 // 1. the command template contains {q}2 unmodified lines4561 if current == nil {4562 current = &minItem4563 }4564 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+4575 var sels []*Item4576 if len(t.selected) == 0 {4566- sels = []*Item{current, current}4567- } else {4568- sels = make([]*Item, len(t.selected)+1)4569- sels[0] = current4577+ sels = []*Item{current}4578+ } else if len(t.selected) > 0 {4579+ sels = make([]*Item, len(t.selected))4580 for i, sel := range t.sortSelected() {4571- sels[i+1] = sel.item4581+ sels[i] = sel.item4582 }4583 }4574- return true, sels4584+ return true, [3][]*Item{{current}, sels, all}4585 }4586 4587 func (t *Terminal) selectItem(item *Item) bool {4588 if len(t.selected) >= t.multi {251 unmodified lines4840 var version int644841 stop := false4842 t.previewBox.WaitFor(reqPreviewReady)4843 for {4844+ requested := false4834- var items []*Item4845+ var items [3][]*Item4846 var commandTemplate string4847 var env []string4848 var query string4849 initialOffset := 09 unmodified lines4859 initialOffset = request.scrollOffset4860 items = request.list4861 env = request.env4862 query = request.query4863+ requested = true4864 }4865 }4866 events.Clear()4867 })4868 if stop {4869 break4870 }4859- if items == nil {4871+ if !requested {4872 continue4873 }4874 version++4875 // We don't display preview window if no match1531 unmodified lines6407 if !valid {6408 // We run the command even when there's no match6409 // 1. If the template doesn't have any slots6410 // 2. If the template has {q}6399- slot, _, forceUpdate := hasPreviewFlags(a.a)6411+ slot, _, _, forceUpdate := hasPreviewFlags(a.a)6412 valid = !slot || forceUpdate6413 }6414 if valid {6415 command, tempFiles := t.replacePlaceholder(a.a, false, string(t.input), list)180 unmodified lines6596 req(reqList)6597 }6598 6599 if queryChanged && t.canPreview() && len(t.previewOpts.command) > 0 {6588- _, _, forceUpdate := hasPreviewFlags(t.previewOpts.command)6600+ _, _, _, forceUpdate := hasPreviewFlags(t.previewOpts.command)6601 if forceUpdate {6602 t.version++6603 }6604 }239 unmodified linesmodified:test/test_preview.rb+10 added
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+202 def test_preview_file203 tmux.send_keys %[(echo foo bar; echo bar foo) | #{FZF} --multi --preview 'cat {+f} {+f2} {+nf} {+fn}' --print0], :Enter204 tmux.until { |lines| assert_includes lines[1], ' foo barbar00 ' }205 tmux.send_keys :BTab366 unmodified linesmodified:CHANGELOG.md+5 added
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.11 - Added background variants of transform actions with `bg-` prefix that run asynchronously in the background12 ```sh13 GETTER='curl -s http://metaphorpsum.com/sentences/1'14 fzf --style full --border --preview : \2660 unmodified lines