107 lines
1.8 KiB
Go
Raw Normal View History

2016-09-25 22:34:02 +02:00
package components
import (
"github.com/erroneousboat/termui"
)
const (
CommandMode = "NORMAL"
InsertMode = "INSERT"
SearchMode = "SEARCH"
)
2016-09-25 22:34:02 +02:00
2016-10-02 14:53:00 +02:00
// Mode is the definition of Mode component
2016-09-25 22:34:02 +02:00
type Mode struct {
Par *termui.Par
}
2016-10-02 14:53:00 +02:00
// CreateMode is the constructor of the Mode struct
2017-12-01 23:52:25 +01:00
func CreateModeComponent() *Mode {
2016-09-25 22:34:02 +02:00
mode := &Mode{
Par: termui.NewPar(CommandMode),
2016-09-25 22:34:02 +02:00
}
mode.Par.Height = 3
mode.SetCommandMode()
2016-09-25 22:34:02 +02:00
return mode
}
// Buffer implements interface termui.Bufferer
func (m *Mode) Buffer() termui.Buffer {
buf := m.Par.Buffer()
2016-10-02 14:53:00 +02:00
// 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 {
2016-10-11 19:28:37 +02:00
buf.Set(
x, m.Par.InnerY(),
termui.Cell{
Ch: ' ',
Fg: m.Par.TextFgColor,
Bg: m.Par.TextBgColor,
},
)
2016-10-02 14:53:00 +02:00
x++
i++
} else {
if j < len(cells) {
buf.Set(x, m.Par.InnerY(), cells[j])
i++
j++
}
x++
}
}
2016-09-25 22:34:02 +02:00
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)
}
2017-12-02 11:09:01 +01:00
func (m *Mode) SetInsertMode() {
m.Par.Text = InsertMode
2017-12-02 11:09:01 +01:00
termui.Render(m)
}
func (m *Mode) SetCommandMode() {
m.Par.Text = CommandMode
2017-12-02 11:09:01 +01:00
termui.Render(m)
}
func (m *Mode) SetSearchMode() {
m.Par.Text = SearchMode
2017-12-02 11:09:01 +01:00
termui.Render(m)
}