From b0a574c38708ecb837db59fe378c64b66c759557 Mon Sep 17 00:00:00 2001 From: gizak Date: Thu, 9 Apr 2015 16:40:49 -0400 Subject: [PATCH] Fix https://github.com/gizak/termui/issues/18 Add Block.chopOverflow and apply it to all widgets --- bar.go | 2 +- block.go | 18 ++++++++++++++++++ chart.go | 2 +- gauge.go | 2 +- list.go | 2 +- p.go | 2 +- sparkline.go | 2 +- 7 files changed, 24 insertions(+), 6 deletions(-) diff --git a/bar.go b/bar.go index 025ce26..807b02d 100644 --- a/bar.go +++ b/bar.go @@ -120,5 +120,5 @@ func (bc *BarChart) Buffer() []Point { } } - return ps + return bc.Block.chopOverflow(ps) } diff --git a/block.go b/block.go index 7037166..9531365 100644 --- a/block.go +++ b/block.go @@ -122,3 +122,21 @@ func (d *Block) SetY(y int) { func (d *Block) SetWidth(w int) { d.Width = w } + +// chop the overflow parts +func (d *Block) chopOverflow(ps []Point) []Point { + nps := make([]Point, 0, len(ps)) + x := d.X + y := d.Y + w := d.Width + h := d.Height + for _, v := range ps { + if v.X >= x && + v.X < x+w && + v.Y >= y && + v.Y < y+h { + nps = append(nps, v) + } + } + return nps +} diff --git a/chart.go b/chart.go index d752362..d2cd635 100644 --- a/chart.go +++ b/chart.go @@ -327,5 +327,5 @@ func (lc *LineChart) Buffer() []Point { ps = append(ps, lc.renderBraille()...) } - return ps + return lc.Block.chopOverflow(ps) } diff --git a/gauge.go b/gauge.go index e437899..3e0003d 100644 --- a/gauge.go +++ b/gauge.go @@ -79,5 +79,5 @@ func (g *Gauge) Buffer() []Point { } ps = append(ps, p) } - return ps + return g.Block.chopOverflow(ps) } diff --git a/list.go b/list.go index d670aa6..0640932 100644 --- a/list.go +++ b/list.go @@ -100,5 +100,5 @@ func (l *List) Buffer() []Point { } } } - return ps + return l.Block.chopOverflow(ps) } diff --git a/p.go b/p.go index 22332ed..e327d74 100644 --- a/p.go +++ b/p.go @@ -67,5 +67,5 @@ func (p *Par) Buffer() []Point { k++ j += w } - return ps + return p.Block.chopOverflow(ps) } diff --git a/sparkline.go b/sparkline.go index 2a8cc7b..e0725d5 100644 --- a/sparkline.go +++ b/sparkline.go @@ -152,5 +152,5 @@ func (sl *Sparklines) Buffer() []Point { oftY += l.displayHeight } - return ps + return sl.Block.chopOverflow(ps) }