Skip to content
Myr

Fix `--preview-window follow` not working correctly with wrapping

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

Fix #3243 Fix #4258

authored by Junegunn Choi · committed by Junegunn Choi · parent 69e9abd · Browse files · 6 files+98 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

6 files+98 added−8 removed

  • tests:+11 added−1 removed
  • other:+87 added−7 removed

6 files+98 added−8 removed

modified:src/tui/light.go+4 added-2 removed
1421 unmodified lines
1422 allLines := strings.Split(str, "\n")1423 for i, line := range allLines {1424 lines := WrapLine(line, w.posx, w.width, w.tabstop, w.wrapSignWidth)1425 for j, wl := range lines {1426 if w.posx < w.width {1426 w.stderrInternal(wl.Text, false, resetCode)1427 w.posx += wl.DisplayWidth1427 w.stderrInternal(wl.Text, false, resetCode)1428 w.posx += wl.DisplayWidth1429 }14301431 // Wrap line1432 if j < len(lines)-1 || i < len(allLines)-1 {1433 if w.posy+1 >= w.height {
104 unmodified lines
modified:src/tui/tcell.go+3 added-1 removed
969 unmodified lines
970 if j > 0 {971 w.renderWrapSign(style)972 }973 }974 if w.lastX < w.width {974 w.renderGraphemes(wl.Text, style)975 w.renderGraphemes(wl.Text, style)976 }977 }978 }979 if w.lastX >= w.width {980 w.lastY++
122 unmodified lines
modified:src/tui/tui.go+4 added-3 removed
1375 unmodified lines
1376 lines := []WrappedLine{}1377 width := 01378 line := ""1379 gr := uniseg.NewGraphemes(input)1380 max := initialMax1380 maxWidth := initialMax1381 contMax := max(1, initialMax-wrapSignWidth)1382 for gr.Next() {1383 rs := gr.Runes()1384 str := string(rs)1385 var w int
6 unmodified lines
1392 w = uniseg.StringWidth(str)1393 }1394 width += w13951395 if prefixLength+width <= max {1396 if prefixLength+width <= maxWidth {1397 line += str1398 } else {1399 lines = append(lines, WrappedLine{string(line), width - w})1400 line = str1401 prefixLength = 01402 width = w1402 max = initialMax - wrapSignWidth1403 maxWidth = contMax1404 }1405 }1406 lines = append(lines, WrappedLine{string(line), width})1407 return lines
1 unmodified line
modified:src/terminal.go+75 added-1 removed
4091 unmodified lines
40924093 return passThroughs, transformed4094}40954096// followOffset computes the correct content-line offset for follow mode,4097// accounting for line wrapping in the preview window.4098func (t *Terminal) followOffset() int {4099 lines := t.previewer.lines4100 headerLines := t.activePreviewOpts.headerLines4101 height := t.pwindow.Height() - headerLines4102 if height <= 0 || len(lines) <= headerLines {4103 return headerLines4104 }41054106 body := lines[headerLines:]4107 if !t.activePreviewOpts.wrap {4108 return max(t.previewer.offset, headerLines+len(body)-height)4109 }41104111 maxWidth := t.pwindow.Width()4112 visualLines := 04113 for i := len(body) - 1; i >= 0; i-- {4114 h := t.previewLineHeight(body[i], maxWidth)4115 if visualLines+h > height {4116 return headerLines + i + 14117 }4118 visualLines += h4119 }4120 return headerLines4121}41224123// previewLineHeight estimates the number of visual lines a preview content line4124// occupies when wrapping is enabled.4125func (t *Terminal) previewLineHeight(line string, maxWidth int) int {4126 if maxWidth <= 0 {4127 return 14128 }41294130 // For word-wrap mode, count the sub-lines produced by word wrapping.4131 // Each sub-line may still char-wrap if it contains a word longer than the width.4132 if t.activePreviewOpts.wrapWord {4133 subLines := t.wordWrapAnsiLine(line, maxWidth, t.wrapSignWidth)4134 total := 04135 for i, sub := range subLines {4136 prefixWidth := 04137 cols := maxWidth4138 if i > 0 {4139 prefixWidth = t.wrapSignWidth4140 cols -= t.wrapSignWidth4141 }4142 w := t.ansiLineWidth(sub, prefixWidth)4143 if cols <= 0 {4144 cols = 14145 }4146 total += max(1, (w+cols-1)/cols)4147 }4148 return total4149 }41504151 // For char-wrap, compute visible width and divide by available width.4152 w := t.ansiLineWidth(line, 0)4153 if w <= maxWidth {4154 return 14155 }4156 remaining := w - maxWidth4157 contWidth := max(1, maxWidth-t.wrapSignWidth)4158 return 1 + (remaining+contWidth-1)/contWidth4159}41604161// ansiLineWidth computes the display width of a string, skipping ANSI escape sequences.4162// prefixWidth is the visual offset where the content starts (e.g. wrap sign width for4163// continuation lines), used for correct tab stop alignment.4164func (t *Terminal) ansiLineWidth(line string, prefixWidth int) int {4165 trimmed, _, _ := extractColor(line, nil, nil)4166 _, width := t.processTabs([]rune(trimmed), prefixWidth)4167 return width - prefixWidth4168}41694170func (t *Terminal) wordWrapAnsiLine(line string, maxWidth int, wrapSignWidth int) []string {4171 if maxWidth <= 0 {4172 return []string{line}4173 }
1603 unmodified lines
5777 }5778 t.previewer.lines = result.lines5779 t.previewer.spinner = result.spinner5780 if t.hasPreviewWindow() && t.previewer.following.Enabled() {5707 t.previewer.offset = max(t.previewer.offset, len(t.previewer.lines)-(t.pwindow.Height()-t.activePreviewOpts.headerLines))5781 t.previewer.offset = t.followOffset()5782 } else if result.offset >= 0 {5783 t.previewer.offset = util.Constrain(result.offset, t.activePreviewOpts.headerLines, len(t.previewer.lines)-1)5784 }5785 t.printPreview()
1848 unmodified lines
modified:test/test_preview.rb+11 added-1 removed
381 unmodified lines
382 tmux.send_keys 'C-w'383 end384 end385386 def test_preview_follow_wrap387 tmux.send_keys "seq 1 | #{FZF} --preview 'seq 1000' --preview-window right,2,follow,wrap", :Enter388 tmux.until { |lines| assert_equal 1, lines.match_count }389 tmux.until do |lines|390 assert_includes lines[-4], '│ 10 │'391 assert_includes lines[-3], '│ ↳ │'392 assert_includes lines[-2], '│ ↳ │'393 end394 end395396 def test_close397 tmux.send_keys "seq 100 | #{FZF} --preview 'echo foo' --bind ctrl-c:close", :Enter398 tmux.until { |lines| assert_equal 100, lines.match_count }399 tmux.until { |lines| assert_includes lines[1], 'foo' }
150 unmodified lines
550 def test_alternative_preview_window_opts551 tmux.send_keys "seq 10 | #{FZF} --preview-border rounded --preview-window '~5,2,+0,<100000(~0,+100,wrap,noinfo)' --preview 'seq 1000'", :Enter552 tmux.until { |lines| assert_equal 10, lines.match_count }553 tmux.until do |lines|544 assert_equal ['╭────╮', '│ 10 │', '│ ↳ 0│', '│ 10 │', '│ ↳ 1│'], lines.take(5).map(&:strip)554 assert_equal ['╭────╮', '│ 10 │', '│ ↳ │', '│ 10 │', '│ ↳ │'], lines.take(5).map(&:strip)555 end556 end557558 def test_preview_window_width_exception
27 unmodified lines
modified:CHANGELOG.md+1 added
34 unmodified lines
35- Added `alt-gutter` color option (#4602) (@hedgieinsocks)36- Added fish completion support (#4605) (@lalvarezt)37- zsh: Handle multi-line history selection (#4595) (@LangLangBart)38- Bug fixes39 - Fixed `--preview-window follow` not working correctly with wrapping (#3243, #4258)40 - Fixed symlinks to directories being returned as files (#4676) (@skk64)41 - Fixed SIGHUP signal handling (#4668) (@LangLangBart)42 - Fixed preview process not killed on exit (#4667)43 - Fixed coloring of items with zero-width characters (#4620)
3126 unmodified lines