From dd0e69cb64161471084f4896ff302b543ae16747 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sun, 2 Oct 2016 14:53:00 +0200 Subject: [PATCH] Center text in mode component --- src/components/channels.go | 1 + src/components/chat.go | 1 + src/components/input.go | 2 +- src/components/mode.go | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/components/channels.go b/src/components/channels.go index cae31af..168d03b 100644 --- a/src/components/channels.go +++ b/src/components/channels.go @@ -9,6 +9,7 @@ import ( "github.com/erroneousboat/slack-term/src/service" ) +// Channels is the definition of a Channels component type Channels struct { List *termui.List SelectedChannel int diff --git a/src/components/chat.go b/src/components/chat.go index 951b280..b187093 100644 --- a/src/components/chat.go +++ b/src/components/chat.go @@ -8,6 +8,7 @@ import ( "github.com/erroneousboat/slack-term/src/service" ) +// Chat is the definition of a Chat component type Chat struct { List *termui.List Offset int diff --git a/src/components/input.go b/src/components/input.go index a818764..49c058f 100644 --- a/src/components/input.go +++ b/src/components/input.go @@ -6,7 +6,7 @@ import ( "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 { Par *termui.Par CursorPosition int diff --git a/src/components/mode.go b/src/components/mode.go index 7c2cd2c..08f2dd5 100644 --- a/src/components/mode.go +++ b/src/components/mode.go @@ -2,10 +2,12 @@ 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"), @@ -19,6 +21,36 @@ func CreateMode() *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: ' '}) + x++ + i++ + } else { + if j < len(cells) { + buf.Set(x, m.Par.InnerY(), cells[j]) + i++ + j++ + } + x++ + } + } + return buf }