termui/box.go

115 lines
2.3 KiB
Go
Raw Normal View History

2015-02-03 14:07:31 +00:00
package termui
const TOP_RIGHT = '┐'
const VERTICAL_LINE = '│'
const HORIZONTAL_LINE = '─'
const TOP_LEFT = '┌'
const BOTTOM_RIGHT = '┘'
const BOTTOM_LEFT = '└'
2015-02-04 01:56:49 +00:00
type border struct {
2015-02-03 19:13:51 +00:00
X int
Y int
Width int
Height int
2015-02-04 01:56:49 +00:00
FgColor Attribute
BgColor Attribute
2015-02-03 14:07:31 +00:00
}
2015-02-04 01:56:49 +00:00
type hline struct {
2015-02-03 19:13:51 +00:00
X int
Y int
Length int
2015-02-04 01:56:49 +00:00
FgColor Attribute
BgColor Attribute
2015-02-03 14:07:31 +00:00
}
2015-02-04 01:56:49 +00:00
type vline struct {
2015-02-03 19:13:51 +00:00
X int
Y int
Length int
2015-02-04 01:56:49 +00:00
FgColor Attribute
BgColor Attribute
2015-02-03 14:07:31 +00:00
}
2015-02-04 01:56:49 +00:00
func (l hline) Buffer() []Point {
2015-02-03 19:13:51 +00:00
pts := make([]Point, l.Length)
for i := 0; i < l.Length; i++ {
pts[i].X = l.X + i
2015-02-03 14:07:31 +00:00
pts[i].Y = l.Y
2015-03-03 18:28:09 +00:00
pts[i].Ch = HORIZONTAL_LINE
pts[i].Bg = l.BgColor
pts[i].Fg = l.FgColor
2015-02-03 14:07:31 +00:00
}
return pts
}
2015-02-04 01:56:49 +00:00
func (l vline) Buffer() []Point {
2015-02-03 19:13:51 +00:00
pts := make([]Point, l.Length)
for i := 0; i < l.Length; i++ {
2015-02-03 14:07:31 +00:00
pts[i].X = l.X
2015-02-03 19:13:51 +00:00
pts[i].Y = l.Y + i
2015-03-03 18:28:09 +00:00
pts[i].Ch = VERTICAL_LINE
pts[i].Bg = l.BgColor
pts[i].Fg = l.FgColor
2015-02-03 14:07:31 +00:00
}
return pts
}
2015-02-04 01:56:49 +00:00
func (b border) Buffer() []Point {
2015-02-03 19:13:51 +00:00
if b.Width < 2 || b.Height < 2 {
2015-02-03 14:07:31 +00:00
return nil
}
2015-02-03 19:13:51 +00:00
pts := make([]Point, 2*b.Width+2*b.Height-4)
2015-02-03 14:07:31 +00:00
pts[0].X = b.X
pts[0].Y = b.Y
2015-03-03 18:28:09 +00:00
pts[0].Fg = b.FgColor
pts[0].Bg = b.BgColor
pts[0].Ch = TOP_LEFT
2015-02-03 14:07:31 +00:00
2015-02-03 19:13:51 +00:00
pts[1].X = b.X + b.Width - 1
2015-02-03 14:07:31 +00:00
pts[1].Y = b.Y
2015-03-03 18:28:09 +00:00
pts[1].Fg = b.FgColor
pts[1].Bg = b.BgColor
pts[1].Ch = TOP_RIGHT
2015-02-03 14:07:31 +00:00
pts[2].X = b.X
2015-02-03 19:13:51 +00:00
pts[2].Y = b.Y + b.Height - 1
2015-03-03 18:28:09 +00:00
pts[2].Fg = b.FgColor
pts[2].Bg = b.BgColor
pts[2].Ch = BOTTOM_LEFT
2015-02-03 14:07:31 +00:00
2015-02-03 19:13:51 +00:00
pts[3].X = b.X + b.Width - 1
pts[3].Y = b.Y + b.Height - 1
2015-03-03 18:28:09 +00:00
pts[3].Fg = b.FgColor
pts[3].Bg = b.BgColor
pts[3].Ch = BOTTOM_RIGHT
2015-02-03 14:07:31 +00:00
2015-02-04 01:56:49 +00: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 19:13:51 +00:00
2015-02-03 14:07:31 +00:00
return pts
}
2015-02-03 19:13:51 +00:00
2015-02-04 01:56:49 +00:00
type labeledBorder struct {
border
2015-02-03 19:13:51 +00:00
Label string
2015-02-04 01:56:49 +00:00
LabelFgColor Attribute
LabelBgColor Attribute
2015-02-03 19:13:51 +00:00
}
2015-02-04 01:56:49 +00:00
func (lb labeledBorder) Buffer() []Point {
ps := lb.border.Buffer()
2015-02-03 19:13:51 +00:00
maxTxtW := lb.Width - 2
rs := trimStr2Runes(lb.Label, maxTxtW)
for i := 0; i < len(rs); i++ {
2015-03-03 18:28:09 +00:00
ps = append(ps, newPointWithAttrs(rs[i], lb.X+1+i, lb.Y, lb.LabelFgColor, lb.LabelBgColor))
2015-02-03 19:13:51 +00:00
}
return ps
}