From f177fe02548cc5fd43aecea541999a40e7cba397 Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Mon, 25 Nov 2024 18:44:26 +0300 Subject: [PATCH] get last rendered line counts including alt screen lines --- standard_renderer.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/standard_renderer.go b/standard_renderer.go index 369f27f87d..80abdf79d8 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 last rendered lines +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()) }