forked from jpbruinsslot/slack-term
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode.go
106 lines (86 loc) · 1.78 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package components
import (
"github.com/erroneousboat/termui"
)
const (
CommandMode = "NORMAL"
InsertMode = "INSERT"
SearchMode = "SEARCH"
)
// Mode is the definition of Mode component
type Mode struct {
Par *termui.Par
}
// CreateMode is the constructor of the Mode struct
func CreateModeComponent() *Mode {
mode := &Mode{
Par: termui.NewPar(CommandMode),
}
mode.Par.Height = 3
mode.SetCommandMode()
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)
}
func (m *Mode) SetInsertMode() {
m.Par.Text = InsertMode
termui.Render(m)
}
func (m *Mode) SetCommandMode() {
m.Par.Text = CommandMode
termui.Render(m)
}
func (m *Mode) SetSearchMode() {
m.Par.Text = SearchMode
termui.Render(m)
}