From a7067fbfa2ed2cb89b3eeff1bde9ccd1870765aa Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 13 Oct 2022 03:38:32 +0200 Subject: [PATCH] fix: prevent renderer from overflowing available height Drops lines from the top when the render buffer is taller than the available height. Fixes #297. --- standard_renderer.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/standard_renderer.go b/standard_renderer.go index 7d68b41..54c0f5d 100644 --- a/standard_renderer.go +++ b/standard_renderer.go @@ -136,6 +136,15 @@ func (r *standardRenderer) flush() { out := termenv.NewOutput(buf) newLines := strings.Split(r.buf.String(), "\n") + + // If we know the output's height, we can use it to determine how many + // lines we can render. We drop lines from the top of the render buffer if + // necessary, as we can't navigate the cursor into the terminal's scrollback + // buffer. + if r.height > 0 && len(newLines) > r.height { + newLines = newLines[len(newLines)-r.height:] + } + numLinesThisFlush := len(newLines) oldLines := strings.Split(r.lastRender, "\n") skipLines := make(map[int]struct{})