Center text in mode component

This commit is contained in:
erroneousboat 2016-10-02 14:53:00 +02:00
parent 9be15b87f8
commit dd0e69cb64
4 changed files with 35 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/erroneousboat/slack-term/src/service" "github.com/erroneousboat/slack-term/src/service"
) )
// Channels is the definition of a Channels component
type Channels struct { type Channels struct {
List *termui.List List *termui.List
SelectedChannel int SelectedChannel int

View File

@ -8,6 +8,7 @@ import (
"github.com/erroneousboat/slack-term/src/service" "github.com/erroneousboat/slack-term/src/service"
) )
// Chat is the definition of a Chat component
type Chat struct { type Chat struct {
List *termui.List List *termui.List
Offset int Offset int

View File

@ -6,7 +6,7 @@ import (
"github.com/erroneousboat/slack-term/src/service" "github.com/erroneousboat/slack-term/src/service"
) )
// Input is the definition of and input box // Input is the definition of an Input component
type Input struct { type Input struct {
Par *termui.Par Par *termui.Par
CursorPosition int CursorPosition int

View File

@ -2,10 +2,12 @@ package components
import "github.com/gizak/termui" import "github.com/gizak/termui"
// Mode is the definition of Mode component
type Mode struct { type Mode struct {
Par *termui.Par Par *termui.Par
} }
// CreateMode is the constructor of the Mode struct
func CreateMode() *Mode { func CreateMode() *Mode {
mode := &Mode{ mode := &Mode{
Par: termui.NewPar("NORMAL"), Par: termui.NewPar("NORMAL"),
@ -19,6 +21,36 @@ func CreateMode() *Mode {
// Buffer implements interface termui.Bufferer // Buffer implements interface termui.Bufferer
func (m *Mode) Buffer() termui.Buffer { func (m *Mode) Buffer() termui.Buffer {
buf := m.Par.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: ' '})
x++
i++
} else {
if j < len(cells) {
buf.Set(x, m.Par.InnerY(), cells[j])
i++
j++
}
x++
}
}
return buf return buf
} }