about summary refs log tree commit diff stats
path: root/lib/ui/text.go
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2018-02-26 22:54:39 -0500
committerDrew DeVault <sir@cmpwn.com>2018-02-26 22:54:39 -0500
commit1418e1b9dc41d8f69bccb8de0fe0f1fb6835ce11 (patch)
tree4ae8b3373fdadb6dd3e7b8c8789cf938522b8f8a /lib/ui/text.go
parent661e3ec2a4dd97d4a8a8eab4f281b088770a6af2 (diff)
downloadaerc-1418e1b9dc41d8f69bccb8de0fe0f1fb6835ce11.tar.gz
Split UI library and widgets
Diffstat (limited to 'lib/ui/text.go')
-rw-r--r--lib/ui/text.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/ui/text.go b/lib/ui/text.go
new file mode 100644
index 0000000..6164837
--- /dev/null
+++ b/lib/ui/text.go
@@ -0,0 +1,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)
+	}
+}