2018-02-18 02:21:33 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2020-11-30 23:07:03 +01:00
|
|
|
"github.com/gdamore/tcell/v2"
|
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 {
|
2019-04-27 18:47:59 +02:00
|
|
|
Invalidatable
|
|
|
|
text string
|
|
|
|
strategy uint
|
2020-07-27 10:03:55 +02:00
|
|
|
style tcell.Style
|
2018-02-18 02:21:33 +01:00
|
|
|
}
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
func NewText(text string, style tcell.Style) *Text {
|
2019-03-17 22:23:53 +01:00
|
|
|
return &Text{
|
2020-07-27 10:03:55 +02:00
|
|
|
text: text,
|
|
|
|
style: style,
|
2019-03-17 22:23:53 +01:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-27 10:03:55 +02:00
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', t.style)
|
|
|
|
ctx.Printf(x, 0, t.style, "%s", t.text)
|
2018-02-18 02:21:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Text) Invalidate() {
|
2019-04-27 18:47:59 +02:00
|
|
|
t.DoInvalidate(t)
|
2018-02-18 02:21:33 +01:00
|
|
|
}
|