2018-02-18 02:21:33 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2018-06-01 09:58:00 +02:00
|
|
|
"github.com/gdamore/tcell"
|
2018-06-12 02:04:21 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
2018-02-18 02:21:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TEXT_LEFT = iota
|
|
|
|
TEXT_CENTER = iota
|
|
|
|
TEXT_RIGHT = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type Text struct {
|
|
|
|
text string
|
|
|
|
strategy uint
|
2018-06-01 09:58:00 +02:00
|
|
|
fg tcell.Color
|
|
|
|
bg tcell.Color
|
2019-03-30 19:12:04 +01:00
|
|
|
bold bool
|
2019-03-30 17:59:18 +01:00
|
|
|
reverse bool
|
2018-02-18 02:21:33 +01:00
|
|
|
onInvalidate func(d Drawable)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewText(text string) *Text {
|
2019-03-17 22:23:53 +01:00
|
|
|
return &Text{
|
|
|
|
bg: tcell.ColorDefault,
|
|
|
|
fg: tcell.ColorDefault,
|
|
|
|
text: text,
|
|
|
|
}
|
2018-02-18 02:21:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Text) Text(text string) *Text {
|
|
|
|
t.text = text
|
|
|
|
t.Invalidate()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Text) Strategy(strategy uint) *Text {
|
|
|
|
t.strategy = strategy
|
|
|
|
t.Invalidate()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2019-03-30 19:12:04 +01:00
|
|
|
func (t *Text) Bold(bold bool) *Text {
|
|
|
|
t.bold = bold
|
|
|
|
t.Invalidate()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2018-06-01 09:58:00 +02:00
|
|
|
func (t *Text) Color(fg tcell.Color, bg tcell.Color) *Text {
|
2018-02-18 02:21:33 +01:00
|
|
|
t.fg = fg
|
|
|
|
t.bg = bg
|
|
|
|
t.Invalidate()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2019-03-30 17:59:18 +01:00
|
|
|
func (t *Text) Reverse(reverse bool) *Text {
|
|
|
|
t.reverse = reverse
|
|
|
|
t.Invalidate()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2018-02-18 02:21:33 +01:00
|
|
|
func (t *Text) Draw(ctx *Context) {
|
|
|
|
size := runewidth.StringWidth(t.text)
|
|
|
|
x := 0
|
|
|
|
if t.strategy == TEXT_CENTER {
|
|
|
|
x = (ctx.Width() - size) / 2
|
|
|
|
}
|
|
|
|
if t.strategy == TEXT_RIGHT {
|
|
|
|
x = ctx.Width() - size
|
|
|
|
}
|
2018-06-01 09:58:00 +02:00
|
|
|
style := tcell.StyleDefault.Background(t.bg).Foreground(t.fg)
|
2019-03-30 19:12:04 +01:00
|
|
|
if t.bold {
|
|
|
|
style = style.Bold(true)
|
|
|
|
}
|
2019-03-30 17:59:18 +01:00
|
|
|
if t.reverse {
|
|
|
|
style = style.Reverse(true)
|
|
|
|
}
|
2018-06-01 09:58:00 +02:00
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', style)
|
|
|
|
ctx.Printf(x, 0, style, t.text)
|
2018-02-18 02:21:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Text) OnInvalidate(onInvalidate func(d Drawable)) {
|
|
|
|
t.onInvalidate = onInvalidate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Text) Invalidate() {
|
|
|
|
if t.onInvalidate != nil {
|
|
|
|
t.onInvalidate(t)
|
|
|
|
}
|
|
|
|
}
|