Re-construct Point

This commit is contained in:
gizak 2015-03-03 13:28:09 -05:00
parent 7379bff790
commit affd0d9c07
11 changed files with 100 additions and 90 deletions

18
bar.go
View File

@ -60,8 +60,8 @@ func (bc *BarChart) Buffer() []Point {
for j := 0; j < bc.BarWidth; j++ {
for k := 0; k < h; k++ {
p := Point{}
p.Code.Ch = ' '
p.Code.Bg = toTmAttr(bc.BarColor)
p.Ch = ' '
p.Bg = bc.BarColor
p.X = bc.innerX + i*(bc.BarWidth+bc.BarGap) + j
p.Y = bc.innerY + bc.innerHeight - 2 - k
ps = append(ps, p)
@ -70,9 +70,9 @@ func (bc *BarChart) Buffer() []Point {
// plot text
for j := 0; j < len(bc.labels[i]); j++ {
p := Point{}
p.Code.Ch = bc.labels[i][j]
p.Code.Bg = toTmAttr(bc.BgColor)
p.Code.Fg = toTmAttr(bc.TextColor)
p.Ch = bc.labels[i][j]
p.Bg = bc.BgColor
p.Fg = bc.TextColor
p.Y = bc.innerY + bc.innerHeight - 1
p.X = bc.innerX + oftX + j
ps = append(ps, p)
@ -80,11 +80,11 @@ func (bc *BarChart) Buffer() []Point {
// plot num
for j := 0; j < len(bc.dataNum[i]); j++ {
p := Point{}
p.Code.Ch = bc.dataNum[i][j]
p.Code.Fg = toTmAttr(bc.NumColor)
p.Code.Bg = toTmAttr(bc.BarColor)
p.Ch = bc.dataNum[i][j]
p.Fg = bc.NumColor
p.Bg = bc.BarColor
if h == 0 {
p.Code.Bg = toTmAttr(bc.BgColor)
p.Bg = bc.BgColor
}
p.X = bc.innerX + oftX + (bc.BarWidth-len(bc.dataNum[i]))/2 + j
p.Y = bc.innerY + bc.innerHeight - 2

View File

@ -28,6 +28,7 @@ func NewBlock() *Block {
return &d
}
// compute box model
func (d *Block) align() {
d.innerWidth = d.Width - d.PaddingLeft - d.PaddingRight
d.innerHeight = d.Height - d.PaddingTop - d.PaddingBottom
@ -63,8 +64,8 @@ func (d *Block) Buffer() []Point {
p := Point{}
p.X = d.X + 1 + i
p.Y = d.Y + 1 + j
p.Code.Ch = ' '
p.Code.Bg = toTmAttr(d.BgColor)
p.Ch = ' '
p.Bg = d.BgColor
ps = append(ps, p)
}
}

44
box.go
View File

@ -37,9 +37,9 @@ func (l hline) Buffer() []Point {
for i := 0; i < l.Length; i++ {
pts[i].X = l.X + i
pts[i].Y = l.Y
pts[i].Code.Ch = HORIZONTAL_LINE
pts[i].Code.Bg = toTmAttr(l.BgColor)
pts[i].Code.Fg = toTmAttr(l.FgColor)
pts[i].Ch = HORIZONTAL_LINE
pts[i].Bg = l.BgColor
pts[i].Fg = l.FgColor
}
return pts
}
@ -49,9 +49,9 @@ func (l vline) Buffer() []Point {
for i := 0; i < l.Length; i++ {
pts[i].X = l.X
pts[i].Y = l.Y + i
pts[i].Code.Ch = VERTICAL_LINE
pts[i].Code.Bg = toTmAttr(l.BgColor)
pts[i].Code.Fg = toTmAttr(l.FgColor)
pts[i].Ch = VERTICAL_LINE
pts[i].Bg = l.BgColor
pts[i].Fg = l.FgColor
}
return pts
}
@ -64,27 +64,27 @@ func (b border) Buffer() []Point {
pts[0].X = b.X
pts[0].Y = b.Y
pts[0].Code.Fg = toTmAttr(b.FgColor)
pts[0].Code.Bg = toTmAttr(b.BgColor)
pts[0].Code.Ch = TOP_LEFT
pts[0].Fg = b.FgColor
pts[0].Bg = b.BgColor
pts[0].Ch = TOP_LEFT
pts[1].X = b.X + b.Width - 1
pts[1].Y = b.Y
pts[1].Code.Fg = toTmAttr(b.FgColor)
pts[1].Code.Bg = toTmAttr(b.BgColor)
pts[1].Code.Ch = TOP_RIGHT
pts[1].Fg = b.FgColor
pts[1].Bg = b.BgColor
pts[1].Ch = TOP_RIGHT
pts[2].X = b.X
pts[2].Y = b.Y + b.Height - 1
pts[2].Code.Fg = toTmAttr(b.FgColor)
pts[2].Code.Bg = toTmAttr(b.BgColor)
pts[2].Code.Ch = BOTTOM_LEFT
pts[2].Fg = b.FgColor
pts[2].Bg = b.BgColor
pts[2].Ch = BOTTOM_LEFT
pts[3].X = b.X + b.Width - 1
pts[3].Y = b.Y + b.Height - 1
pts[3].Code.Fg = toTmAttr(b.FgColor)
pts[3].Code.Bg = toTmAttr(b.BgColor)
pts[3].Code.Ch = BOTTOM_RIGHT
pts[3].Fg = b.FgColor
pts[3].Bg = b.BgColor
pts[3].Ch = BOTTOM_RIGHT
copy(pts[4:], (hline{b.X + 1, b.Y, b.Width - 2, b.FgColor, b.BgColor}).Buffer())
copy(pts[4+b.Width-2:], (hline{b.X + 1, b.Y + b.Height - 1, b.Width - 2, b.FgColor, b.BgColor}).Buffer())
@ -107,13 +107,7 @@ func (lb labeledBorder) Buffer() []Point {
rs := trimStr2Runes(lb.Label, maxTxtW)
for i := 0; i < len(rs); i++ {
p := Point{}
p.X = lb.X + 1 + i
p.Y = lb.Y
p.Code.Ch = rs[i]
p.Code.Fg = toTmAttr(lb.LabelFgColor)
p.Code.Bg = toTmAttr(lb.LabelBgColor)
ps = append(ps, p)
ps = append(ps, newPointWithAttrs(rs[i], lb.X+1+i, lb.Y, lb.LabelFgColor, lb.LabelBgColor))
}
return ps

View File

@ -1,7 +1,6 @@
package termui
import "fmt"
import tm "github.com/nsf/termbox-go"
const VDASH = '┊'
const HDASH = '┈'
@ -83,9 +82,9 @@ func (lc *LineChart) renderBraille() []Point {
}
p := Point{}
p.Code.Ch = braillePatterns[[2]int{m0, m1}]
p.Code.Bg = toTmAttr(lc.BgColor)
p.Code.Fg = toTmAttr(lc.LineColor)
p.Ch = braillePatterns[[2]int{m0, m1}]
p.Bg = lc.BgColor
p.Fg = lc.LineColor
p.Y = lc.innerY + lc.innerHeight - 3 - b0
p.X = lc.innerX + lc.labelYSpace + 1 + i/2
ps = append(ps, p)
@ -97,9 +96,9 @@ func (lc *LineChart) renderDot() []Point {
ps := []Point{}
for i := 0; i < len(lc.Data) && i < lc.axisXWidth; i++ {
p := Point{}
p.Code.Ch = lc.DotStyle
p.Code.Fg = toTmAttr(lc.LineColor)
p.Code.Bg = toTmAttr(lc.BgColor)
p.Ch = lc.DotStyle
p.Fg = lc.LineColor
p.Bg = lc.BgColor
p.X = lc.innerX + lc.labelYSpace + 1 + i
p.Y = lc.innerY + lc.innerHeight - 3 - int((lc.Data[i]-lc.minY)/lc.scale+0.5)
ps = append(ps, p)
@ -206,17 +205,15 @@ func (lc *LineChart) plotAxes() []Point {
origY := lc.innerY + lc.innerHeight - 2
origX := lc.innerX + lc.labelYSpace
ps := []Point{Point{Code: tm.Cell{Ch: ORIGIN, Bg: toTmAttr(lc.BgColor), Fg: toTmAttr(lc.AxesColor)},
X: origX,
Y: origY}}
ps := []Point{newPointWithAttrs(ORIGIN, origX, origY, lc.AxesColor, lc.BgColor)}
for x := origX + 1; x < origX+lc.axisXWidth; x++ {
p := Point{}
p.X = x
p.Y = origY
p.Code.Bg = toTmAttr(lc.BgColor)
p.Code.Fg = toTmAttr(lc.AxesColor)
p.Code.Ch = HDASH
p.Bg = lc.BgColor
p.Fg = lc.AxesColor
p.Ch = HDASH
ps = append(ps, p)
}
@ -224,9 +221,9 @@ func (lc *LineChart) plotAxes() []Point {
p := Point{}
p.X = origX
p.Y = origY - dy
p.Code.Bg = toTmAttr(lc.BgColor)
p.Code.Fg = toTmAttr(lc.AxesColor)
p.Code.Ch = VDASH
p.Bg = lc.BgColor
p.Fg = lc.AxesColor
p.Ch = VDASH
ps = append(ps, p)
}
@ -238,9 +235,9 @@ func (lc *LineChart) plotAxes() []Point {
}
for j, r := range rs {
p := Point{}
p.Code.Ch = r
p.Code.Fg = toTmAttr(lc.AxesColor)
p.Code.Bg = toTmAttr(lc.BgColor)
p.Ch = r
p.Fg = lc.AxesColor
p.Bg = lc.BgColor
p.X = origX + oft + j
p.Y = lc.innerY + lc.innerHeight - 1
ps = append(ps, p)
@ -252,9 +249,9 @@ func (lc *LineChart) plotAxes() []Point {
for i, rs := range lc.labelY {
for j, r := range rs {
p := Point{}
p.Code.Ch = r
p.Code.Fg = toTmAttr(lc.AxesColor)
p.Code.Bg = toTmAttr(lc.BgColor)
p.Ch = r
p.Fg = lc.AxesColor
p.Bg = lc.BgColor
p.X = lc.innerX + j
p.Y = origY - i*(lc.axisYLebelGap+1)
ps = append(ps, p)

View File

@ -32,8 +32,8 @@ func (g *Gauge) Buffer() []Point {
p := Point{}
p.X = g.innerX + j
p.Y = g.innerY + i
p.Code.Ch = ' '
p.Code.Bg = toTmAttr(g.BarColor)
p.Ch = ' '
p.Bg = g.BarColor
ps = append(ps, p)
}
}
@ -43,12 +43,12 @@ func (g *Gauge) Buffer() []Point {
p := Point{}
p.X = prx + i
p.Y = pry
p.Code.Ch = v
p.Code.Fg = toTmAttr(g.PercentColor)
p.Ch = v
p.Fg = g.PercentColor
if w > g.innerWidth/2-1+i {
p.Code.Bg = toTmAttr(g.BarColor)
p.Bg = g.BarColor
} else {
p.Code.Bg = toTmAttr(g.Block.BgColor)
p.Bg = g.Block.BgColor
}
ps = append(ps, p)
}

View File

@ -4,6 +4,8 @@ import "unicode/utf8"
import "strings"
import tm "github.com/nsf/termbox-go"
/* ---------------Port from termbox-go --------------------- */
type Attribute uint16
const (
@ -24,6 +26,8 @@ const (
AttrReverse
)
/* ----------------------- End ----------------------------- */
func toTmAttr(x Attribute) tm.Attribute {
return tm.Attribute(x)
}

12
list.go
View File

@ -35,9 +35,9 @@ func (l *List) Buffer() []Point {
pi.X = l.innerX + j
pi.Y = l.innerY + i
pi.Code.Ch = rs[k]
pi.Code.Bg = toTmAttr(l.ItemBgColor)
pi.Code.Fg = toTmAttr(l.ItemFgColor)
pi.Ch = rs[k]
pi.Bg = l.ItemBgColor
pi.Fg = l.ItemFgColor
ps = append(ps, pi)
k++
@ -58,9 +58,9 @@ func (l *List) Buffer() []Point {
p.X = l.innerX + j
p.Y = l.innerY + i
p.Code.Ch = vv
p.Code.Bg = toTmAttr(l.ItemBgColor)
p.Code.Fg = toTmAttr(l.ItemFgColor)
p.Ch = vv
p.Bg = l.ItemBgColor
p.Fg = l.ItemFgColor
ps = append(ps, p)
j++

6
p.go
View File

@ -29,9 +29,9 @@ func (p *P) Buffer() []Point {
pi.X = p.innerX + j
pi.Y = p.innerY + i
pi.Code.Ch = rs[k]
pi.Code.Bg = toTmAttr(p.TextBgColor)
pi.Code.Fg = toTmAttr(p.TextFgColor)
pi.Ch = rs[k]
pi.Bg = p.TextBgColor
pi.Fg = p.TextFgColor
ps = append(ps, pi)
k++

View File

@ -1,9 +1,23 @@
package termui
import tm "github.com/nsf/termbox-go"
type Point struct {
Code tm.Cell
Ch rune
Bg Attribute
Fg Attribute
X int
Y int
}
func newPoint(c rune, x, y int) (p Point) {
p.Ch = c
p.X = x
p.Y = y
return
}
func newPointWithAttrs(c rune, x, y int, fg, bg Attribute) Point {
p := newPoint(c, x, y)
p.Bg = bg
p.Fg = fg
return p
}

View File

@ -2,7 +2,7 @@ package termui
import tm "github.com/nsf/termbox-go"
type Renderer interface {
type Bufferer interface {
Buffer() []Point
}
@ -14,11 +14,11 @@ func Close() {
tm.Close()
}
func Render(rs ...Renderer) {
func Render(rs ...Bufferer) {
for _, r := range rs {
buf := r.Buffer()
for _, v := range buf {
tm.SetCell(v.X, v.Y, v.Code.Ch, v.Code.Fg, v.Code.Bg)
tm.SetCell(v.X, v.Y, v.Ch, toTmAttr(v.Fg), toTmAttr(v.Bg))
}
}
tm.Flush()

View File

@ -84,9 +84,9 @@ func (sl *Sparklines) Buffer() []Point {
rs := trimStr2Runes(l.Title, sl.innerWidth)
for oftX, v := range rs {
p := Point{}
p.Code.Ch = v
p.Code.Fg = toTmAttr(l.TitleColor)
p.Code.Bg = toTmAttr(sl.BgColor)
p.Ch = v
p.Fg = l.TitleColor
p.Bg = sl.BgColor
p.X = sl.innerX + oftX
p.Y = sl.innerY + oftY
ps = append(ps, p)
@ -101,18 +101,18 @@ func (sl *Sparklines) Buffer() []Point {
p := Point{}
p.X = sl.innerX + j
p.Y = sl.innerY + oftY + l.Height - jj
p.Code.Ch = sparks[7]
p.Code.Fg = toTmAttr(l.LineColor)
p.Code.Bg = toTmAttr(sl.BgColor)
p.Ch = sparks[7]
p.Fg = l.LineColor
p.Bg = sl.BgColor
ps = append(ps, p)
}
if barMod != 0 {
p := Point{}
p.X = sl.innerX + j
p.Y = sl.innerY + oftY + l.Height - barCnt
p.Code.Ch = sparks[barMod-1]
p.Code.Fg = toTmAttr(l.LineColor)
p.Code.Bg = toTmAttr(sl.BgColor)
p.Ch = sparks[barMod-1]
p.Fg = l.LineColor
p.Bg = sl.BgColor
ps = append(ps, p)
}
}