Skip to content
Myr

Improve preview window update events

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

- Update preview window even if there is no match for the query string if any of the placeholder expressions evaluates to a non-empty string. - Also, if the command template contains {q}, preview window will be updated if the query string changes even though the focus remains on the same item. An example: git log --oneline --color=always | fzf --reverse --ansi --preview \ '[ -n {1} ] && git show --color=always {1} || git show --color=always {q}' Close #1307

authored by Junegunn Choi · committed by Junegunn Choi · parent af87650 · Browse files · 3 files+67 added−8 removed
This commit

State

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

Diff stats

3 files+67 added−8 removed

  • tests:+29 added−0 removed
  • other:+38 added−8 removed

3 files+67 added−8 removed

modified:man/man1/fzf.1+3 added
289 unmodified lines
290291Also, \fB{q}\fR is replaced to the current query string.292293Note that you can escape a placeholder pattern by prepending a backslash.294295Preview window will be updated even when there is no match for the current296query if any of the placeholder expressions evaluates to a non-empty string.297.RE298.TP299.BI "--preview-window=" "[POSITION][:SIZE[%]][:wrap][:hidden]"300Determine the layout of the preview window. If the argument ends with
318 unmodified lines
modified:src/terminal.go+35 added-8 removed
222 unmodified lines
223224type placeholderFlags struct {225 plus bool226 preserveSpace bool227 query bool228}229230func toActions(types ...actionType) []action {231 actions := make([]action, len(types))
920 unmodified lines
1152 skipChars++1153 case 's':1154 flags.preserveSpace = true1155 skipChars++1156 case 'q':1157 flags.query = true1158 default:1159 break1160 }1161 }
2 unmodified lines
11641165 return false, matchWithoutFlags, flags1166}11671165func hasPlusFlag(template string) bool {1168func hasPreviewFlags(template string) (plus bool, query bool) {1169 for _, match := range placeholder.FindAllString(template, -1) {1170 _, _, flags := parsePlaceholder(match)1171 if flags.plus {1169 return true1172 plus = true1173 }1174 if flags.query {1175 query = true1176 }1177 }1172 return false1178 return1179}11801181func replacePlaceholder(template string, stripAnsi bool, delimiter Delimiter, forcePlus bool, query string, allItems []*Item) string {1182 current := allItems[:1]
110 unmodified lines
1293}12941295func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item) {1296 current := t.currentItem()1291 if !forcePlus && !hasPlusFlag(template) || len(t.selected) == 0 {1297 plus, query := hasPreviewFlags(template)1298 if !(query && len(t.input) > 0 || (forcePlus || plus) && len(t.selected) > 0) {1299 return current != nil, []*Item{current, current}1300 }1294 sels := make([]*Item, len(t.selected)+1)1295 sels[0] = current1296 for i, sel := range t.sortSelected() {1297 sels[i+1] = sel.item13011302 // We would still want to update preview window even if there is no match if1303 // 1. command template contains {q} and the query string is not empty1304 // 2. or it contains {+} and we have more than one item already selected.1305 // To do so, we pass an empty Item instead of nil to trigger an update.1306 if current == nil {1307 current = &Item{}1308 }13091310 var sels []*Item1311 if len(t.selected) == 0 {1312 sels = []*Item{current, current}1313 } else {1314 sels = make([]*Item, len(t.selected)+1)1315 sels[0] = current1316 for i, sel := range t.sortSelected() {1317 sels[i+1] = sel.item1318 }1319 }1320 return true, sels1321}13221323func (t *Terminal) truncateQuery() {
557 unmodified lines
1881 }1882 t.mutex.Unlock() // Must be unlocked before touching reqBox18831884 if changed {1885 if t.isPreviewEnabled() {1886 _, q := hasPreviewFlags(t.preview.command)1887 if q {1888 t.version++1889 }1890 }1891 t.eventBox.Set(EvtSearchNew, t.sort)1892 }1893 for _, event := range events {1894 t.reqBox.Set(event, nil)
51 unmodified lines
modified:test/test_go.rb+29 added
1353 unmodified lines
1354 tmux.until { |lines| lines[4] == '> 3' }1355 tmux.until { |_| %w[1 2 3] == File.readlines(tempname).map(&:chomp) }1356 end13571358 def test_preview_flags1359 tmux.send_keys %(seq 10 | sed 's/^/:: /; s/$/ /' |1360 #{FZF} --multi --preview 'echo {{2}/{s2}/{+2}/{+s2}/{q}}'), :Enter1361 tmux.until { |lines| lines[1].include?('{1/1 /1/1 /}') }1362 tmux.send_keys '123'1363 tmux.until { |lines| lines[1].include?('{////123}') }1364 tmux.send_keys 'C-u', '1'1365 tmux.until { |lines| lines.match_count == 2 }1366 tmux.until { |lines| lines[1].include?('{1/1 /1/1 /1}') }1367 tmux.send_keys :BTab1368 tmux.until { |lines| lines[1].include?('{10/10 /1/1 /1}') }1369 tmux.send_keys :BTab1370 tmux.until { |lines| lines[1].include?('{10/10 /1 10/1 10 /1}') }1371 tmux.send_keys '2'1372 tmux.until { |lines| lines[1].include?('{//1 10/1 10 /12}') }1373 tmux.send_keys '3'1374 tmux.until { |lines| lines[1].include?('{//1 10/1 10 /123}') }1375 end13761377 def test_preview_q_no_match1378 tmux.send_keys %(: | #{FZF} --preview 'echo foo {q}'), :Enter1379 tmux.until { |lines| lines.match_count == 0 }1380 tmux.until { |lines| !lines[1].include?('foo') }1381 tmux.send_keys 'bar'1382 tmux.until { |lines| lines[1].include?('foo bar') }1383 tmux.send_keys 'C-u'1384 tmux.until { |lines| !lines[1].include?('foo') }1385 end13861387 def test_no_clear1388 tmux.send_keys "seq 10 | fzf --no-clear --inline-info --height 5 > #{tempname}", :Enter1389 prompt = '> < 10/10'1390 tmux.until { |lines| lines[-1] == prompt }
456 unmodified lines