From fc28d646d0584a8f518eb16a2f257ce5f53a5d0b Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 9 Jul 2019 14:01:41 +0100 Subject: [PATCH] Primitive widths/heights should not become negative. Also fixed a TextView issue where purging led to panics. Fixes #306 --- box.go | 24 +++++++++++++++++++----- textview.go | 7 ++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/box.go b/box.go index 6bcd6d2..809b3c9 100644 --- a/box.go +++ b/box.go @@ -88,7 +88,8 @@ func (b *Box) GetRect() (int, int, int, int) { } // GetInnerRect returns the position of the inner rectangle (x, y, width, -// height), without the border and without any padding. +// height), without the border and without any padding. Width and height values +// will clamp to 0 and thus never be negative. func (b *Box) GetInnerRect() (int, int, int, int) { if b.innerX >= 0 { return b.innerX, b.innerY, b.innerWidth, b.innerHeight @@ -100,10 +101,17 @@ func (b *Box) GetInnerRect() (int, int, int, int) { width -= 2 height -= 2 } - return x + b.paddingLeft, - y + b.paddingTop, - width - b.paddingLeft - b.paddingRight, - height - b.paddingTop - b.paddingBottom + x, y, width, height = x+b.paddingLeft, + y+b.paddingTop, + width-b.paddingLeft-b.paddingRight, + height-b.paddingTop-b.paddingBottom + if width < 0 { + width = 0 + } + if height < 0 { + height = 0 + } + return x, y, width, height } // SetRect sets a new position of the primitive. Note that this has no effect @@ -318,6 +326,12 @@ func (b *Box) Draw(screen tcell.Screen) { b.innerHeight += b.innerY b.innerY = 0 } + if b.innerWidth < 0 { + b.innerWidth = 0 + } + if b.innerHeight < 0 { + b.innerHeight = 0 + } } // Focus is called when this primitive receives focus. diff --git a/textview.go b/textview.go index b7d0394..82b878f 100644 --- a/textview.go +++ b/textview.go @@ -939,8 +939,13 @@ func (t *TextView) Draw(screen tcell.Screen) { // If this view is not scrollable, we'll purge the buffer of lines that have // scrolled out of view. if !t.scrollable && t.lineOffset > 0 { - t.buffer = t.buffer[t.index[t.lineOffset].Line:] + if t.lineOffset <= len(t.index) { + t.buffer = nil + } else { + t.buffer = t.buffer[t.index[t.lineOffset].Line:] + } t.index = nil + t.lineOffset = 0 } }