Start with creating Chat window

This commit is contained in:
erroneousboat 2016-09-24 20:18:09 +02:00
parent 7de84017bf
commit c9639080c6
4 changed files with 129 additions and 6 deletions

View File

@ -1,3 +1,124 @@
package components
type Chat struct{}
import (
"strings"
"github.com/gizak/termui"
)
type Chat struct {
List *termui.List
}
func CreateChat(inputHeight int) *Chat {
chat := &Chat{
List: termui.NewList(),
}
chat.List.Height = termui.TermHeight() - inputHeight
chat.List.Overflow = "wrap"
chat.LoadMessages()
return chat
}
// Buffer implements interface termui.Bufferer
func (c *Chat) Buffer() termui.Buffer {
// Build cells, after every item put a newline
cells := termui.DefaultTxBuilder.Build(
strings.Join(c.List.Items, "\n"),
c.List.ItemFgColor, c.List.ItemBgColor,
)
type Line struct {
cells []termui.Cell
}
// Uncover how many lines there are in total for all items
lines := []Line{}
line := Line{}
x := 0
for _, cell := range cells {
if cell.Ch == '\n' {
lines = append(lines, line)
line = Line{}
x = 0
continue
}
if x+cell.Width() > c.List.InnerBounds().Dx() {
lines = append(lines, line)
line = Line{}
x = 0
}
line.cells = append(line.cells, cell)
x++
}
// We will print lines bottom up
buf := c.List.Buffer()
linesHeight := len(lines)
windowMinY := c.List.InnerBounds().Min.Y
windowMaxY := c.List.InnerBounds().Max.Y
currentY := windowMaxY - 1
for i := linesHeight - 1; i >= 0; i-- {
if currentY <= windowMinY {
break
}
x := c.List.InnerBounds().Min.X
for _, cell := range lines[i].cells {
buf.Set(x, currentY, cell)
x += cell.Width()
}
currentY--
}
return buf
}
// GetHeight implements interface termui.GridBufferer
func (c *Chat) GetHeight() int {
return c.List.Block.GetHeight()
}
// SetWidth implements interface termui.GridBufferer
func (c *Chat) SetWidth(w int) {
c.List.SetWidth(w)
}
// SetX implements interface termui.GridBufferer
func (c *Chat) SetX(x int) {
c.List.SetX(x)
}
// SetY implements interface termui.GridBufferer
func (c *Chat) SetY(y int) {
c.List.SetY(y)
}
func (c *Chat) LoadMessages() {
messages := []string{
"[jp] hello world",
"[erroneousboat] foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar",
}
for _, message := range messages {
c.AddMessages(message)
}
}
func (c *Chat) AddMessages(message string) {
c.List.Items = append(c.List.Items, message)
}
func (c *Chat) ScrollUp() {
}
func (c *Chat) ScrollDown() {}

View File

@ -61,8 +61,10 @@ func (i *Input) SetY(y int) {
// Insert will insert a given key at the place of the current CursorPosition
func (i *Input) Insert(key string) {
i.Par.Text = i.Par.Text[0:i.CursorPosition] + key + i.Par.Text[i.CursorPosition:len(i.Par.Text)]
i.MoveCursorRight()
if len(i.Par.Text) < i.Par.InnerBounds().Dx()-1 {
i.Par.Text = i.Par.Text[0:i.CursorPosition] + key + i.Par.Text[i.CursorPosition:len(i.Par.Text)]
i.MoveCursorRight()
}
}
// Remove will remove a character at the place of the current CursorPosition

View File

@ -91,7 +91,7 @@ func actionMoveCursorLeft(view *views.View) {
func actionSend(ctx *context.AppContext) {
if !ctx.View.Input.IsEmpty() {
// FIXME
ctx.View.Chat.Items = append(ctx.View.Chat.Items, ctx.View.Input.Text())
ctx.View.Chat.List.Items = append(ctx.View.Chat.List.Items, ctx.View.Input.Text())
ctx.View.Input.Clear()
ctx.View.Refresh()
}

View File

@ -8,7 +8,7 @@ import (
type View struct {
Input *components.Input
Chat *termui.List
Chat *components.Chat
Channels *termui.List
Mode *termui.Par
}
@ -16,7 +16,7 @@ type View struct {
func CreateChatView() *View {
input := components.CreateInput()
channels := components.CreateChannelsComponent(input.Par.Height)
chat := components.CreateChatComponent(input.Par.Height)
chat := components.CreateChat(input.Par.Height)
mode := components.CreateModeComponent()
view := &View{