From 5985b9883ea05729f8be2819a66d57569f6dbdae Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 8 Nov 2024 11:14:39 -0500 Subject: [PATCH] fix: renderer: erase the rest of the line when it's shorter than the width When the cursor reaches the end of the line, any escape sequences that follow will only affect the last cell of the line. This is why we only erase the rest of the line when the line is shorter than the width of the terminal. Fixes: https://github.com/charmbracelet/bubbletea/issues/1225 Fixes: 0cef3c7896a8 (feat(render): remove flickering) --- standard_renderer.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/standard_renderer.go b/standard_renderer.go index 583e5b389e..f564b6a094 100644 --- a/standard_renderer.go +++ b/standard_renderer.go @@ -190,8 +190,15 @@ func (r *standardRenderer) flush() { if flushQueuedMessages { // Dump the lines we've queued up for printing. for _, line := range r.queuedMessageLines { - // Removing previousy rendered content at the end of line. - line = line + ansi.EraseLineRight + if ansi.StringWidth(line) < r.width { + // We only erase the rest of the line when the line is shorter than + // the width of the terminal. When the cursor reaches the end of + // the line, any escape sequences that follow will only affect the + // last cell of the line. + + // Removing previously rendered content at the end of line. + line = line + ansi.EraseLineRight + } _, _ = buf.WriteString(line) _, _ = buf.WriteString("\r\n") @@ -221,9 +228,6 @@ func (r *standardRenderer) flush() { line := newLines[i] - // Removing previousy rendered content at the end of line. - line = line + ansi.EraseLineRight - // Truncate lines wider than the width of the window to avoid // wrapping, which will mess up rendering. If we don't have the // width of the window this will be ignored. @@ -235,6 +239,16 @@ func (r *standardRenderer) flush() { line = ansi.Truncate(line, r.width, "") } + if ansi.StringWidth(line) < r.width { + // We only erase the rest of the line when the line is shorter than + // the width of the terminal. When the cursor reaches the end of + // the line, any escape sequences that follow will only affect the + // last cell of the line. + + // Removing previously rendered content at the end of line. + line = line + ansi.EraseLineRight + } + _, _ = buf.WriteString(line) if i < len(newLines)-1 {