summaryrefslogtreecommitdiff
path: root/lib/ui/text.go
blob: 6164837469348e49b9256870420594eae831ba09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ui

import (
	"github.com/mattn/go-runewidth"
	tb "github.com/nsf/termbox-go"
)

const (
	TEXT_LEFT   = iota
	TEXT_CENTER = iota
	TEXT_RIGHT  = iota
)

type Text struct {
	text         string
	strategy     uint
	fg           tb.Attribute
	bg           tb.Attribute
	onInvalidate func(d Drawable)
}

func NewText(text string) *Text {
	return &Text{text: text}
}

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) Color(fg tb.Attribute, bg tb.Attribute) *Text {
	t.fg = fg
	t.bg = bg
	t.Invalidate()
	return t
}

func (t *Text) Draw(ctx *Context) {
	size := runewidth.StringWidth(t.text)
	cell := tb.Cell{
		Ch: ' ',
		Fg: t.fg,
		Bg: t.bg,
	}
	x := 0
	if t.strategy == TEXT_CENTER {
		x = (ctx.Width() - size) / 2
	}
	if t.strategy == TEXT_RIGHT {
		x = ctx.Width() - size
	}
	ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell)
	ctx.Printf(x, 0, cell, "%s", t.text)
}

func (t *Text) OnInvalidate(onInvalidate func(d Drawable)) {
	t.onInvalidate = onInvalidate
}

func (t *Text) Invalidate() {
	if t.onInvalidate != nil {
		t.onInvalidate(t)
	}
}