Skip to content

Commit

Permalink
fix: getting last rendered line counts including alt screen lines (#1254
Browse files Browse the repository at this point in the history
)

* get last rendered line counts including alt screen lines

* update the comment
  • Loading branch information
semihbkgr authored Dec 4, 2024
1 parent 7dc1e85 commit 2556e01
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions standard_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
Expand All @@ -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())
}
}
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down

0 comments on commit 2556e01

Please sign in to comment.