From 3c34dd82750ca61a1ee7f329ed47fe01e6d1ee30 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 17 Mar 2023 13:22:20 +0900 Subject: [PATCH] Fix extra new line in the preview window When a colored text ends at the right end of the window Fix #3209 --- src/terminal.go | 14 ++++++++------ test/test_go.rb | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/terminal.go b/src/terminal.go index 2140037..0310a9f 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -1828,12 +1828,14 @@ func (t *Terminal) renderPreviewText(height int, lines []string, lineNo int, unc trimmed, isTrimmed = t.trimRight(trimmed, maxWidth-t.pwindow.X()) } str, width := t.processTabs(trimmed, prefixWidth) - prefixWidth += width - if t.theme.Colored && ansi != nil && ansi.colored() { - lbg = ansi.lbg - fillRet = t.pwindow.CFill(ansi.fg, ansi.bg, ansi.attr, str) - } else { - fillRet = t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), tui.AttrRegular, str) + if width > prefixWidth { + prefixWidth = width + if t.theme.Colored && ansi != nil && ansi.colored() { + lbg = ansi.lbg + fillRet = t.pwindow.CFill(ansi.fg, ansi.bg, ansi.attr, str) + } else { + fillRet = t.pwindow.CFill(tui.ColPreview.Fg(), tui.ColPreview.Bg(), tui.AttrRegular, str) + } } return !isTrimmed && (fillRet == tui.FillContinue || t.previewOpts.wrap && fillRet == tui.FillNextLine) diff --git a/test/test_go.rb b/test/test_go.rb index 6ef2d62..07a7c19 100755 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -2656,6 +2656,23 @@ class TestGoFZF < TestBase tmux.send_keys :Enter tmux.until { |lines| assert_equal 99, lines.item_count } end + + def test_no_extra_newline_issue_3209 + tmux.send_keys(%(seq 100 | #{FZF} --height 10 --preview-window up,wrap --preview 'printf "─%.0s" $(seq 1 "$((FZF_PREVIEW_COLUMNS - 5))"); printf $"\\e[7m%s\\e[0m" title; echo; echo something'), :Enter) + expected = <<~OUTPUT + ╭────────── + │ ───────── + │ something + │ + ╰────────── + 3 + 2 + > 1 + 100/100 ─ + > + OUTPUT + tmux.until { assert_block(expected, _1) } + end end module TestShell