termui/box.go

123 lines
2.5 KiB
Go
Raw Normal View History

2015-02-03 07:07:31 -07:00
package termui
import tm "github.com/nsf/termbox-go"
const TOP_RIGHT = '┐'
const VERTICAL_LINE = '│'
const HORIZONTAL_LINE = '─'
const TOP_LEFT = '┌'
const BOTTOM_RIGHT = '┘'
const BOTTOM_LEFT = '└'
type Box struct {
2015-02-03 12:13:51 -07:00
X int
Y int
Width int
Height int
2015-02-03 07:07:31 -07:00
FgColor tm.Attribute
BgColor tm.Attribute
}
type HLine struct {
2015-02-03 12:13:51 -07:00
X int
Y int
Length int
2015-02-03 07:07:31 -07:00
FgColor tm.Attribute
BgColor tm.Attribute
}
type VLine struct {
2015-02-03 12:13:51 -07:00
X int
Y int
Length int
2015-02-03 07:07:31 -07:00
FgColor tm.Attribute
BgColor tm.Attribute
}
2015-02-03 12:13:51 -07:00
func (l HLine) Buffer() []Point {
pts := make([]Point, l.Length)
for i := 0; i < l.Length; i++ {
pts[i].X = l.X + i
2015-02-03 07:07:31 -07:00
pts[i].Y = l.Y
pts[i].Code.Ch = HORIZONTAL_LINE
pts[i].Code.Bg = l.BgColor
pts[i].Code.Fg = l.FgColor
}
return pts
}
2015-02-03 12:13:51 -07:00
func (l VLine) Buffer() []Point {
pts := make([]Point, l.Length)
for i := 0; i < l.Length; i++ {
2015-02-03 07:07:31 -07:00
pts[i].X = l.X
2015-02-03 12:13:51 -07:00
pts[i].Y = l.Y + i
2015-02-03 07:07:31 -07:00
pts[i].Code.Ch = VERTICAL_LINE
pts[i].Code.Bg = l.BgColor
pts[i].Code.Fg = l.FgColor
}
return pts
}
2015-02-03 12:13:51 -07:00
func (b Box) Buffer() []Point {
if b.Width < 2 || b.Height < 2 {
2015-02-03 07:07:31 -07:00
return nil
}
2015-02-03 12:13:51 -07:00
pts := make([]Point, 2*b.Width+2*b.Height-4)
2015-02-03 07:07:31 -07:00
pts[0].X = b.X
pts[0].Y = b.Y
pts[0].Code.Fg = b.FgColor
pts[0].Code.Bg = b.BgColor
pts[0].Code.Ch = TOP_LEFT
2015-02-03 12:13:51 -07:00
pts[1].X = b.X + b.Width - 1
2015-02-03 07:07:31 -07:00
pts[1].Y = b.Y
pts[1].Code.Fg = b.FgColor
pts[1].Code.Bg = b.BgColor
pts[1].Code.Ch = TOP_RIGHT
pts[2].X = b.X
2015-02-03 12:13:51 -07:00
pts[2].Y = b.Y + b.Height - 1
2015-02-03 07:07:31 -07:00
pts[2].Code.Fg = b.FgColor
pts[2].Code.Bg = b.BgColor
pts[2].Code.Ch = BOTTOM_LEFT
2015-02-03 12:13:51 -07:00
pts[3].X = b.X + b.Width - 1
pts[3].Y = b.Y + b.Height - 1
2015-02-03 07:07:31 -07:00
pts[3].Code.Fg = b.FgColor
pts[3].Code.Bg = b.BgColor
pts[3].Code.Ch = BOTTOM_RIGHT
2015-02-03 12:13:51 -07:00
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())
copy(pts[4+2*b.Width-4:], (VLine{b.X, b.Y + 1, b.Height - 2, b.FgColor, b.BgColor}).Buffer())
copy(pts[4+2*b.Width-4+b.Height-2:], (VLine{b.X + b.Width - 1, b.Y + 1, b.Height - 2, b.FgColor, b.BgColor}).Buffer())
2015-02-03 07:07:31 -07:00
return pts
}
2015-02-03 12:13:51 -07:00
type LabeledBox struct {
Box
Label string
LabelFgColor tm.Attribute
LabelBgColor tm.Attribute
}
func (lb LabeledBox) Buffer() []Point {
ps := lb.Box.Buffer()
maxTxtW := lb.Width - 2
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 = lb.LabelFgColor
p.Code.Bg = lb.LabelBgColor
ps = append(ps, p)
}
return ps
}