-
Notifications
You must be signed in to change notification settings - Fork 81
/
text.go
88 lines (77 loc) · 1.85 KB
/
text.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package termloop
// Text represents a string that can be drawn to the screen.
type Text struct {
x int
y int
fg Attr
bg Attr
text []rune
canvas []Cell
}
// NewText creates a new Text, at position (x, y). It sets the Text's
// background and foreground colors to fg and bg respectively, and sets the
// Text's text to be text.
// Returns a pointer to the new Text.
func NewText(x, y int, text string, fg, bg Attr) *Text {
str := []rune(text)
c := make([]Cell, len(str))
for i := range c {
c[i] = Cell{Ch: str[i], Fg: fg, Bg: bg}
}
return &Text{
x: x,
y: y,
fg: fg,
bg: bg,
text: str,
canvas: c,
}
}
func (t *Text) Tick(ev Event) {}
// Draw draws the Text to the Screen s.
func (t *Text) Draw(s *Screen) {
w, _ := t.Size()
for i := 0; i < w; i++ {
s.RenderCell(t.x+i, t.y, &t.canvas[i])
}
}
// Position returns the (x, y) coordinates of the Text.
func (t *Text) Position() (int, int) {
return t.x, t.y
}
// Size returns the width and height of the Text.
func (t *Text) Size() (int, int) {
return len(t.text), 1
}
// SetPosition sets the coordinates of the Text to be (x, y).
func (t *Text) SetPosition(x, y int) {
t.x = x
t.y = y
}
// Text returns the text of the Text.
func (t *Text) Text() string {
return string(t.text)
}
// SetText sets the text of the Text to be text.
func (t *Text) SetText(text string) {
t.text = []rune(text)
c := make([]Cell, len(t.text))
for i := range c {
c[i] = Cell{Ch: t.text[i], Fg: t.fg, Bg: t.bg}
}
t.canvas = c
}
// Color returns the (foreground, background) colors of the Text.
func (t *Text) Color() (Attr, Attr) {
return t.fg, t.bg
}
// SetColor sets the (foreground, background) colors of the Text
// to fg, bg respectively.
func (t *Text) SetColor(fg, bg Attr) {
t.fg = fg
t.bg = bg
for i := range t.canvas {
t.canvas[i].Fg = fg
t.canvas[i].Bg = bg
}
}