forked from jpbruinsslot/slack-term
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mode.go
82 lines (66 loc) · 1.42 KB
/
mode.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
package components
import "github.com/gizak/termui"
// Mode is the definition of Mode component
type Mode struct {
Par *termui.Par
}
// CreateMode is the constructor of the Mode struct
func CreateMode() *Mode {
mode := &Mode{
Par: termui.NewPar("NORMAL"),
}
mode.Par.Height = 3
return mode
}
// Buffer implements interface termui.Bufferer
func (m *Mode) Buffer() termui.Buffer {
buf := m.Par.Buffer()
// Center text
space := m.Par.InnerWidth()
word := len(m.Par.Text)
midSpace := space / 2
midWord := word / 2
start := midSpace - midWord
cells := termui.DefaultTxBuilder.Build(
m.Par.Text, m.Par.TextFgColor, m.Par.TextBgColor)
i, j := 0, 0
x := m.Par.InnerBounds().Min.X
for x < m.Par.InnerBounds().Max.X {
if i < start {
buf.Set(
x, m.Par.InnerY(),
termui.Cell{
Ch: ' ',
Fg: m.Par.TextFgColor,
Bg: m.Par.TextBgColor,
},
)
x++
i++
} else {
if j < len(cells) {
buf.Set(x, m.Par.InnerY(), cells[j])
i++
j++
}
x++
}
}
return buf
}
// GetHeight implements interface termui.GridBufferer
func (m *Mode) GetHeight() int {
return m.Par.Block.GetHeight()
}
// SetWidth implements interface termui.GridBufferer
func (m *Mode) SetWidth(w int) {
m.Par.SetWidth(w)
}
// SetX implements interface termui.GridBufferer
func (m *Mode) SetX(x int) {
m.Par.SetX(x)
}
// SetY implements interface termui.GridBufferer
func (m *Mode) SetY(y int) {
m.Par.SetY(y)
}