From 2556e01416b7f7cda13b27724c4e07ad5bb5d51c Mon Sep 17 00:00:00 2001 From: Semih Buyukgungor Date: Wed, 4 Dec 2024 23:51:28 +0300 Subject: [PATCH] fix: getting last rendered line counts including alt screen lines (#1254) * get last rendered line counts including alt screen lines * update the comment --- standard_renderer.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/standard_renderer.go b/standard_renderer.go index 369f27f87d..f2fd152b1c 100644 --- a/standard_renderer.go +++ b/standard_renderer.go @@ -258,13 +258,8 @@ func (r *standardRenderer) flush() { } } - lastLinesRendered := r.linesRendered - if r.altScreenActive { - lastLinesRendered = r.altLinesRendered - } - // Clearing left over content from last render. - if lastLinesRendered > len(newLines) { + if r.lastLinesRendered() > len(newLines) { buf.WriteString(ansi.EraseScreenBelow) } @@ -295,6 +290,14 @@ func (r *standardRenderer) flush() { r.buf.Reset() } +// lastLinesRendered returns the number of lines rendered lastly. +func (r *standardRenderer) lastLinesRendered() int { + if r.altScreenActive { + return r.altLinesRendered + } + return r.linesRendered +} + // write writes to the internal buffer. The buffer will be outputted via the // ticker which calls flush(). func (r *standardRenderer) write(s string) { @@ -507,7 +510,7 @@ func (r *standardRenderer) setWindowTitle(title string) { func (r *standardRenderer) setIgnoredLines(from int, to int) { // Lock if we're going to be clearing some lines since we don't want // anything jacking our cursor. - if r.linesRendered > 0 { + if r.lastLinesRendered() > 0 { r.mtx.Lock() defer r.mtx.Unlock() } @@ -520,16 +523,17 @@ func (r *standardRenderer) setIgnoredLines(from int, to int) { } // Erase ignored lines - if r.linesRendered > 0 { + lastLinesRendered := r.lastLinesRendered() + if lastLinesRendered > 0 { buf := &bytes.Buffer{} - for i := r.linesRendered - 1; i >= 0; i-- { + for i := lastLinesRendered - 1; i >= 0; i-- { if _, exists := r.ignoreLines[i]; exists { buf.WriteString(ansi.EraseEntireLine) } buf.WriteString(ansi.CursorUp1) } - buf.WriteString(ansi.SetCursorPosition(0, r.linesRendered)) // put cursor back + buf.WriteString(ansi.SetCursorPosition(0, lastLinesRendered)) // put cursor back _, _ = r.out.Write(buf.Bytes()) } } @@ -575,7 +579,7 @@ func (r *standardRenderer) insertTop(lines []string, topBoundary, bottomBoundary buf.WriteString(ansi.SetScrollingRegion(0, r.height)) // Move cursor back to where the main rendering routine expects it to be - buf.WriteString(ansi.SetCursorPosition(0, r.linesRendered)) + buf.WriteString(ansi.SetCursorPosition(0, r.lastLinesRendered())) _, _ = r.out.Write(buf.Bytes()) } @@ -604,7 +608,7 @@ func (r *standardRenderer) insertBottom(lines []string, topBoundary, bottomBound buf.WriteString(ansi.SetScrollingRegion(0, r.height)) // Move cursor back to where the main rendering routine expects it to be - buf.WriteString(ansi.SetCursorPosition(0, r.linesRendered)) + buf.WriteString(ansi.SetCursorPosition(0, r.lastLinesRendered())) _, _ = r.out.Write(buf.Bytes()) }