tui/views/borderview.go

94 lines
2.2 KiB
Go
Raw Permalink Normal View History

2022-04-02 13:01:41 +02:00
package views
import "git.tordarus.net/Tordarus/tui"
// BorderView is a tui.Wrapper which draws an ASCII border around its view.
// Be aware that box drawing characters must share the same tui.Style with the next character.
// This can lead to color artifacts when using BorderView with colored styles.
type BorderView struct {
tui.WrapperTmpl
Border BorderBox
}
var _ tui.View = &BorderView{}
func NewBorderView(view tui.View) *BorderView {
v := new(BorderView)
v.SetView(view)
v.Border = ThinBorder()
return v
}
func (g *BorderView) Draw(buf *tui.ViewBuffer) {
g.View().Draw(buf.Sub(1, 1, buf.Width()-2, buf.Height()-2))
buf.Set(0, 0, tui.Rune{Rn: g.Border.TopLeft, Style: g.Style()})
buf.Set(buf.Width()-1, 0, tui.Rune{Rn: g.Border.TopRight, Style: g.Style()})
buf.Set(0, buf.Height()-1, tui.Rune{Rn: g.Border.BottomLeft, Style: g.Style()})
buf.Set(buf.Width()-1, buf.Height()-1, tui.Rune{Rn: g.Border.BottomRight, Style: g.Style()})
for x := 1; x < buf.Width()-1; x++ {
buf.Set(x, 0, tui.Rune{Rn: g.Border.Horizontal, Style: g.Style()})
buf.Set(x, buf.Height()-1, tui.Rune{Rn: g.Border.Horizontal, Style: g.Style()})
}
for y := 1; y < buf.Height()-1; y++ {
buf.Set(0, y, tui.Rune{Rn: g.Border.Vertical, Style: g.Style()})
buf.Set(buf.Width()-1, y, tui.Rune{Rn: g.Border.Vertical, Style: g.Style()})
}
}
func (v *BorderView) Layout() (prefWidth, prefHeight int) {
w, h := v.View().Layout()
w = iff(w > 0, w+2, w)
h = iff(h > 0, h+2, h)
return w, h
}
func (v *BorderView) Style() tui.Style {
return v.ViewTmpl.Style()
}
type BorderBox struct {
TopLeft rune
TopRight rune
BottomLeft rune
BottomRight rune
Horizontal rune
Vertical rune
}
func ThickBorder() BorderBox {
return BorderBox{
TopLeft: '┏',
TopRight: '┓',
BottomLeft: '┗',
BottomRight: '┛',
Horizontal: '━',
Vertical: '┃',
}
}
func ThinBorder() BorderBox {
return BorderBox{
TopLeft: '┌',
TopRight: '┐',
BottomLeft: '└',
BottomRight: '┘',
Horizontal: '─',
Vertical: '│',
}
}
func DoubleBorder() BorderBox {
return BorderBox{
TopLeft: '╔',
TopRight: '╗',
BottomLeft: '╚',
BottomRight: '╝',
Horizontal: '═',
Vertical: '║',
}
}