2018-02-18 01:21:33 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2018-06-01 07:58:00 +00:00
|
|
|
"github.com/gdamore/tcell"
|
2018-06-12 00:04:21 +00:00
|
|
|
"github.com/mattn/go-runewidth"
|
2018-02-18 01:21:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TEXT_LEFT = iota
|
|
|
|
TEXT_CENTER = iota
|
|
|
|
TEXT_RIGHT = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type Text struct {
|
2019-04-27 16:47:59 +00:00
|
|
|
Invalidatable
|
|
|
|
text string
|
|
|
|
strategy uint
|
2020-05-28 14:32:42 +00:00
|
|
|
fg tcell.Color
|
|
|
|
bg tcell.Color
|
|
|
|
bold bool
|
|
|
|
reverse bool
|
2018-02-18 01:21:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:42 +00:00
|
|
|
func NewText(text string) *Text {
|
2019-03-17 21:23:53 +00:00
|
|
|
return &Text{
|
2020-05-28 14:32:42 +00:00
|
|
|
bg: tcell.ColorDefault,
|
|
|
|
fg: tcell.ColorDefault,
|
|
|
|
text: text,
|
2019-03-17 21:23:53 +00:00
|
|
|
}
|
2018-02-18 01:21:33 +00: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
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:32:42 +00:00
|
|
|
func (t *Text) Bold(bold bool) *Text {
|
|
|
|
t.bold = bold
|
|
|
|
t.Invalidate()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Text) Color(fg tcell.Color, bg tcell.Color) *Text {
|
|
|
|
t.fg = fg
|
|
|
|
t.bg = bg
|
|
|
|
t.Invalidate()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Text) Reverse(reverse bool) *Text {
|
|
|
|
t.reverse = reverse
|
|
|
|
t.Invalidate()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:21:33 +00: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
|
|
|
|
}
|
2020-05-28 14:32:42 +00:00
|
|
|
style := tcell.StyleDefault.Background(t.bg).Foreground(t.fg)
|
|
|
|
if t.bold {
|
|
|
|
style = style.Bold(true)
|
|
|
|
}
|
|
|
|
if t.reverse {
|
|
|
|
style = style.Reverse(true)
|
|
|
|
}
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', style)
|
|
|
|
ctx.Printf(x, 0, style, "%s", t.text)
|
2018-02-18 01:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Text) Invalidate() {
|
2019-04-27 16:47:59 +00:00
|
|
|
t.DoInvalidate(t)
|
2018-02-18 01:21:33 +00:00
|
|
|
}
|