| 1 |
package ui |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/mattn/go-runewidth" |
| 5 |
"go.rockorager.dev/vaxis" |
| 6 |
) |
| 7 |
|
| 8 |
const ( |
| 9 |
TEXT_LEFT = iota |
| 10 |
TEXT_CENTER = iota |
| 11 |
TEXT_RIGHT = iota |
| 12 |
) |
| 13 |
|
| 14 |
type Text struct { |
| 15 |
text string |
| 16 |
strategy uint |
| 17 |
style vaxis.Style |
| 18 |
} |
| 19 |
|
| 20 |
func NewText(text string, style vaxis.Style) *Text { |
| 21 |
return &Text{ |
| 22 |
text: text, |
| 23 |
style: style, |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func (t *Text) Text(text string) *Text { |
| 28 |
t.text = text |
| 29 |
t.Invalidate() |
| 30 |
return t |
| 31 |
} |
| 32 |
|
| 33 |
func (t *Text) Strategy(strategy uint) *Text { |
| 34 |
t.strategy = strategy |
| 35 |
t.Invalidate() |
| 36 |
return t |
| 37 |
} |
| 38 |
|
| 39 |
func (t *Text) Draw(ctx *Context) { |
| 40 |
size := runewidth.StringWidth(t.text) |
| 41 |
x := 0 |
| 42 |
if t.strategy == TEXT_CENTER { |
| 43 |
x = (ctx.Width() - size) / 2 |
| 44 |
} |
| 45 |
if t.strategy == TEXT_RIGHT { |
| 46 |
x = ctx.Width() - size |
| 47 |
} |
| 48 |
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', t.style) |
| 49 |
ctx.Printf(x, 0, t.style, "%s", t.text) |
| 50 |
} |
| 51 |
|
| 52 |
func (t *Text) Invalidate() { |
| 53 |
Invalidate() |
| 54 |
} |