2016-09-11 17:55:19 +02:00
|
|
|
package components
|
|
|
|
|
2016-09-24 20:18:09 +02:00
|
|
|
import (
|
2016-10-30 14:26:12 +01:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
2016-09-24 20:18:09 +02:00
|
|
|
"strings"
|
2017-12-03 20:40:46 +01:00
|
|
|
"time"
|
2016-09-24 20:18:09 +02:00
|
|
|
|
2017-09-23 13:56:45 +02:00
|
|
|
"github.com/erroneousboat/termui"
|
2018-08-25 11:18:20 +02:00
|
|
|
runewidth "github.com/mattn/go-runewidth"
|
2016-10-01 14:07:42 +02:00
|
|
|
|
2016-10-30 14:26:12 +01:00
|
|
|
"github.com/erroneousboat/slack-term/config"
|
2016-09-24 20:18:09 +02:00
|
|
|
)
|
|
|
|
|
2016-10-02 14:53:00 +02:00
|
|
|
// Chat is the definition of a Chat component
|
2016-09-24 20:18:09 +02:00
|
|
|
type Chat struct {
|
2018-08-11 12:37:58 +02:00
|
|
|
List *termui.List
|
2018-12-24 14:37:12 +01:00
|
|
|
Messages map[string]Message
|
2018-08-11 12:37:58 +02:00
|
|
|
Offset int
|
2016-09-24 20:18:09 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 10:21:51 +02:00
|
|
|
// CreateChatComponent is the constructor for the Chat struct
|
2017-12-01 23:52:25 +01:00
|
|
|
func CreateChatComponent(inputHeight int) *Chat {
|
2016-09-24 20:18:09 +02:00
|
|
|
chat := &Chat{
|
2018-12-24 14:37:12 +01:00
|
|
|
List: termui.NewList(),
|
|
|
|
Messages: make(map[string]Message),
|
|
|
|
Offset: 0,
|
2016-09-24 20:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
chat.List.Height = termui.TermHeight() - inputHeight
|
|
|
|
chat.List.Overflow = "wrap"
|
2016-09-28 22:10:04 +02:00
|
|
|
|
2016-09-24 20:18:09 +02:00
|
|
|
return chat
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer implements interface termui.Bufferer
|
|
|
|
func (c *Chat) Buffer() termui.Buffer {
|
2018-12-24 14:37:12 +01:00
|
|
|
// Convert Messages into termui.Cell
|
|
|
|
cells := c.MessagesToCells(c.Messages)
|
2016-09-24 20:18:09 +02:00
|
|
|
|
2016-09-25 12:54:24 +02:00
|
|
|
// We will create an array of Line structs, this allows us
|
|
|
|
// to more easily render the items in a list. We will range
|
|
|
|
// over the cells we've created and create a Line within
|
|
|
|
// the bounds of the Chat pane
|
2016-09-24 20:18:09 +02:00
|
|
|
type Line struct {
|
|
|
|
cells []termui.Cell
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := []Line{}
|
|
|
|
line := Line{}
|
|
|
|
|
2018-08-25 11:18:20 +02:00
|
|
|
// When we encounter a newline or, are at the bounds of the chat view we
|
2018-02-24 12:10:30 +01:00
|
|
|
// stop iterating over the cells and add the line to the line array
|
2016-09-24 20:18:09 +02:00
|
|
|
x := 0
|
|
|
|
for _, cell := range cells {
|
|
|
|
|
2018-02-24 12:10:30 +01:00
|
|
|
// When we encounter a newline we add the line to the array
|
2016-09-24 20:18:09 +02:00
|
|
|
if cell.Ch == '\n' {
|
|
|
|
lines = append(lines, line)
|
2018-02-03 20:32:36 +01:00
|
|
|
|
|
|
|
// Reset for new line
|
2016-09-24 20:18:09 +02:00
|
|
|
line = Line{}
|
|
|
|
x = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if x+cell.Width() > c.List.InnerBounds().Dx() {
|
|
|
|
lines = append(lines, line)
|
2018-02-03 20:32:36 +01:00
|
|
|
|
|
|
|
// Reset for new line
|
2016-09-24 20:18:09 +02:00
|
|
|
line = Line{}
|
|
|
|
x = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
line.cells = append(line.cells, cell)
|
2018-08-25 11:18:20 +02:00
|
|
|
x += cell.Width()
|
2016-09-24 20:18:09 +02:00
|
|
|
}
|
|
|
|
|
2018-02-24 12:10:30 +01:00
|
|
|
// Append the last line to the array when we didn't encounter any
|
|
|
|
// newlines or were at the bounds of the chat view
|
|
|
|
lines = append(lines, line)
|
|
|
|
|
2016-09-25 12:54:24 +02:00
|
|
|
// We will print lines bottom up, it will loop over the lines
|
2016-09-30 16:36:41 +02:00
|
|
|
// backwards and for every line it'll set the cell in that line.
|
|
|
|
// Offset is the number which allows us to begin printing the
|
|
|
|
// line above the last line.
|
2016-09-24 20:18:09 +02:00
|
|
|
buf := c.List.Buffer()
|
|
|
|
linesHeight := len(lines)
|
2016-09-25 12:54:24 +02:00
|
|
|
paneMinY := c.List.InnerBounds().Min.Y
|
|
|
|
paneMaxY := c.List.InnerBounds().Max.Y
|
2016-09-24 20:18:09 +02:00
|
|
|
|
2016-09-25 12:54:24 +02:00
|
|
|
currentY := paneMaxY - 1
|
2016-09-30 16:36:41 +02:00
|
|
|
for i := (linesHeight - 1) - c.Offset; i >= 0; i-- {
|
2018-02-03 20:32:36 +01:00
|
|
|
|
2016-09-25 12:54:24 +02:00
|
|
|
if currentY < paneMinY {
|
2016-09-24 20:18:09 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
x := c.List.InnerBounds().Min.X
|
|
|
|
for _, cell := range lines[i].cells {
|
|
|
|
buf.Set(x, currentY, cell)
|
|
|
|
x += cell.Width()
|
|
|
|
}
|
|
|
|
|
2016-09-25 12:54:24 +02:00
|
|
|
// When we're not at the end of the pane, fill it up
|
|
|
|
// with empty characters
|
|
|
|
for x < c.List.InnerBounds().Max.X {
|
2016-10-11 19:28:37 +02:00
|
|
|
buf.Set(
|
|
|
|
x, currentY,
|
|
|
|
termui.Cell{
|
|
|
|
Ch: ' ',
|
|
|
|
Fg: c.List.ItemFgColor,
|
|
|
|
Bg: c.List.ItemBgColor,
|
|
|
|
},
|
|
|
|
)
|
2018-08-25 11:18:20 +02:00
|
|
|
x += runewidth.RuneWidth(' ')
|
2016-09-25 12:54:24 +02:00
|
|
|
}
|
|
|
|
currentY--
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the space above currentY is empty we need to fill
|
|
|
|
// it up with blank lines, otherwise the List object will
|
|
|
|
// render the items top down, and the result will mix.
|
|
|
|
for currentY >= paneMinY {
|
|
|
|
x := c.List.InnerBounds().Min.X
|
|
|
|
for x < c.List.InnerBounds().Max.X {
|
2016-10-11 19:28:37 +02:00
|
|
|
buf.Set(
|
|
|
|
x, currentY,
|
|
|
|
termui.Cell{
|
|
|
|
Ch: ' ',
|
|
|
|
Fg: c.List.ItemFgColor,
|
|
|
|
Bg: c.List.ItemBgColor,
|
|
|
|
},
|
|
|
|
)
|
2018-08-25 11:18:20 +02:00
|
|
|
x += runewidth.RuneWidth(' ')
|
2016-09-25 12:54:24 +02:00
|
|
|
}
|
2016-09-24 20:18:09 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-12-01 23:52:25 +01:00
|
|
|
// GetMaxItems return the maximal amount of items can fit in the Chat
|
|
|
|
// component
|
|
|
|
func (c *Chat) GetMaxItems() int {
|
|
|
|
return c.List.InnerBounds().Max.Y - c.List.InnerBounds().Min.Y
|
|
|
|
}
|
2016-09-24 20:18:09 +02:00
|
|
|
|
2018-08-11 12:37:58 +02:00
|
|
|
// SetMessages will put the provided messages into the Messages field of the
|
2017-12-01 23:52:25 +01:00
|
|
|
// Chat view
|
2018-08-11 12:37:58 +02:00
|
|
|
func (c *Chat) SetMessages(messages []Message) {
|
2018-02-03 20:32:36 +01:00
|
|
|
// Reset offset first, when scrolling in view and changing channels we
|
|
|
|
// want the offset to be 0 when loading new messages
|
|
|
|
c.Offset = 0
|
2018-12-24 14:37:12 +01:00
|
|
|
for _, msg := range messages {
|
|
|
|
c.Messages[msg.ID] = msg
|
|
|
|
}
|
2016-09-24 20:18:09 +02:00
|
|
|
}
|
|
|
|
|
2018-08-11 12:37:58 +02:00
|
|
|
// AddMessage adds a single message to Messages
|
|
|
|
func (c *Chat) AddMessage(message Message) {
|
2018-12-24 14:37:12 +01:00
|
|
|
c.Messages[message.ID] = message
|
2016-09-24 20:18:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 22:45:36 +01:00
|
|
|
// AddReply adds a single reply to a parent thread, it also sets
|
|
|
|
// the thread separator
|
2018-12-25 15:56:27 +01:00
|
|
|
func (c *Chat) AddReply(parentID string, message Message) {
|
2019-06-15 12:45:01 +02:00
|
|
|
// It is possible that a message is received but the parent is not
|
|
|
|
// present in the chat view
|
|
|
|
if _, ok := c.Messages[parentID]; ok {
|
|
|
|
message.Thread = " "
|
|
|
|
c.Messages[parentID].Messages[message.ID] = message
|
|
|
|
} else {
|
|
|
|
c.AddMessage(message)
|
|
|
|
}
|
2018-12-25 15:56:27 +01:00
|
|
|
}
|
|
|
|
|
2019-10-05 12:40:12 +02:00
|
|
|
// IsNewThread check whether a message that is going to be added as
|
|
|
|
// a child to a parent message, is the first one or not
|
|
|
|
func (c *Chat) IsNewThread(parentID string) bool {
|
|
|
|
if parent, ok := c.Messages[parentID]; ok {
|
|
|
|
if len(parent.Messages) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-13 18:34:18 +02:00
|
|
|
// ClearMessages clear the c.Messages
|
2016-09-29 19:09:30 +02:00
|
|
|
func (c *Chat) ClearMessages() {
|
2018-12-24 14:37:12 +01:00
|
|
|
c.Messages = make(map[string]Message)
|
2016-09-29 19:09:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 16:36:41 +02:00
|
|
|
// ScrollUp will render the chat messages based on the Offset of the Chat
|
|
|
|
// pane.
|
|
|
|
//
|
|
|
|
// Offset is 0 when scrolled down. (we loop backwards over the array, so we
|
|
|
|
// start with rendering last item in the list at the maximum y of the Chat
|
|
|
|
// pane). Increasing the Offset will thus result in substracting the offset
|
2018-10-13 18:34:18 +02:00
|
|
|
// from the len(Chat.Messages).
|
2016-09-24 20:18:09 +02:00
|
|
|
func (c *Chat) ScrollUp() {
|
2016-09-30 16:36:41 +02:00
|
|
|
c.Offset = c.Offset + 10
|
|
|
|
|
|
|
|
// Protect overscrolling
|
2018-10-13 18:34:18 +02:00
|
|
|
if c.Offset > len(c.Messages) {
|
|
|
|
c.Offset = len(c.Messages)
|
2016-09-30 16:36:41 +02:00
|
|
|
}
|
2016-09-24 20:18:09 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 16:36:41 +02:00
|
|
|
// ScrollDown will render the chat messages based on the Offset of the Chat
|
|
|
|
// pane.
|
|
|
|
//
|
|
|
|
// Offset is 0 when scrolled down. (we loop backwards over the array, so we
|
|
|
|
// start with rendering last item in the list at the maximum y of the Chat
|
|
|
|
// pane). Increasing the Offset will thus result in substracting the offset
|
2018-10-13 18:34:18 +02:00
|
|
|
// from the len(Chat.Messages).
|
2016-09-30 16:36:41 +02:00
|
|
|
func (c *Chat) ScrollDown() {
|
|
|
|
c.Offset = c.Offset - 10
|
|
|
|
|
|
|
|
// Protect overscrolling
|
|
|
|
if c.Offset < 0 {
|
|
|
|
c.Offset = 0
|
|
|
|
}
|
|
|
|
}
|
2016-09-28 22:10:04 +02:00
|
|
|
|
2016-09-29 19:09:30 +02:00
|
|
|
// SetBorderLabel will set Label of the Chat pane to the specified string
|
2017-12-03 20:40:46 +01:00
|
|
|
func (c *Chat) SetBorderLabel(channelName string) {
|
2016-10-30 16:02:11 +01:00
|
|
|
c.List.BorderLabel = channelName
|
2016-09-28 22:10:04 +02:00
|
|
|
}
|
2016-10-30 14:26:12 +01:00
|
|
|
|
2018-12-24 14:37:12 +01:00
|
|
|
// MessagesToCells is a wrapper around MessageToCells to use for a slice of
|
|
|
|
// of type Message
|
|
|
|
func (c *Chat) MessagesToCells(msgs map[string]Message) []termui.Cell {
|
|
|
|
cells := make([]termui.Cell, 0)
|
|
|
|
sortedMessages := SortMessages(msgs)
|
|
|
|
|
|
|
|
for i, msg := range sortedMessages {
|
|
|
|
cells = append(cells, c.MessageToCells(msg)...)
|
|
|
|
|
|
|
|
if len(msg.Messages) > 0 {
|
|
|
|
cells = append(cells, termui.Cell{Ch: '\n'})
|
|
|
|
cells = append(cells, c.MessagesToCells(msg.Messages)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a newline after every message
|
|
|
|
if i < len(sortedMessages)-1 {
|
|
|
|
cells = append(cells, termui.Cell{Ch: '\n'})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cells
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessageToCells will convert a Message struct to termui.Cell
|
|
|
|
//
|
|
|
|
// We're building parts of the message individually, or else DefaultTxBuilder
|
|
|
|
// will interpret potential markdown usage in a message as well.
|
|
|
|
func (c *Chat) MessageToCells(msg Message) []termui.Cell {
|
|
|
|
cells := make([]termui.Cell, 0)
|
|
|
|
|
|
|
|
// When msg.Time and msg.Name are empty (in the case of attachments)
|
|
|
|
// don't add the time and name parts.
|
|
|
|
if (msg.Time != time.Time{} && msg.Name != "") {
|
|
|
|
// Time
|
|
|
|
cells = append(cells, termui.DefaultTxBuilder.Build(
|
|
|
|
msg.GetTime(),
|
|
|
|
termui.ColorDefault, termui.ColorDefault)...,
|
|
|
|
)
|
|
|
|
|
2019-02-16 22:45:36 +01:00
|
|
|
// Thread
|
|
|
|
cells = append(cells, termui.DefaultTxBuilder.Build(
|
|
|
|
msg.GetThread(),
|
|
|
|
termui.ColorDefault, termui.ColorDefault)...,
|
|
|
|
)
|
|
|
|
|
2018-12-24 14:37:12 +01:00
|
|
|
// Name
|
|
|
|
cells = append(cells, termui.DefaultTxBuilder.Build(
|
|
|
|
msg.GetName(),
|
|
|
|
termui.ColorDefault, termui.ColorDefault)...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hack, in order to get the correct fg and bg attributes. This is
|
|
|
|
// because the readAttr function in termui is unexported.
|
|
|
|
txCells := termui.DefaultTxBuilder.Build(
|
|
|
|
msg.GetContent(),
|
|
|
|
termui.ColorDefault, termui.ColorDefault,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Text
|
|
|
|
for _, r := range msg.Content {
|
|
|
|
cells = append(
|
|
|
|
cells,
|
|
|
|
termui.Cell{
|
|
|
|
Ch: r,
|
|
|
|
Fg: txCells[0].Fg,
|
|
|
|
Bg: txCells[0].Bg,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cells
|
|
|
|
}
|
|
|
|
|
2016-10-30 14:26:12 +01:00
|
|
|
// Help shows the usage and key bindings in the chat pane
|
2018-10-13 18:34:18 +02:00
|
|
|
func (c *Chat) Help(usage string, cfg *config.Config) {
|
2019-05-29 10:21:51 +02:00
|
|
|
msgUsage := Message{
|
|
|
|
ID: fmt.Sprintf("%d", time.Now().UnixNano()),
|
|
|
|
Content: usage,
|
2016-10-30 14:26:12 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 10:21:51 +02:00
|
|
|
c.Messages[msgUsage.ID] = msgUsage
|
|
|
|
|
2016-10-30 14:26:12 +01:00
|
|
|
for mode, mapping := range cfg.KeyMap {
|
2019-05-29 10:21:51 +02:00
|
|
|
msgMode := Message{
|
|
|
|
ID: fmt.Sprintf("%d", time.Now().UnixNano()),
|
|
|
|
Content: fmt.Sprintf("%s", strings.ToUpper(mode)),
|
|
|
|
}
|
|
|
|
c.Messages[msgMode.ID] = msgMode
|
2018-10-13 18:34:18 +02:00
|
|
|
|
2019-05-29 10:21:51 +02:00
|
|
|
msgNewline := Message{
|
|
|
|
ID: fmt.Sprintf("%d", time.Now().UnixNano()),
|
|
|
|
Content: "",
|
|
|
|
}
|
|
|
|
c.Messages[msgNewline.ID] = msgNewline
|
2016-10-30 14:26:12 +01:00
|
|
|
|
|
|
|
var keys []string
|
|
|
|
for k := range mapping {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, k := range keys {
|
2019-05-29 10:21:51 +02:00
|
|
|
msgKey := Message{
|
|
|
|
ID: fmt.Sprintf("%d", time.Now().UnixNano()),
|
|
|
|
Content: fmt.Sprintf(" %-12s%-15s", k, mapping[k]),
|
|
|
|
}
|
|
|
|
c.Messages[msgKey.ID] = msgKey
|
2016-10-30 14:26:12 +01:00
|
|
|
}
|
2018-10-13 18:34:18 +02:00
|
|
|
|
2019-05-29 10:21:51 +02:00
|
|
|
msgNewline.ID = fmt.Sprintf("%d", time.Now().UnixNano())
|
|
|
|
c.Messages[msgNewline.ID] = msgNewline
|
2018-12-24 14:37:12 +01:00
|
|
|
}
|
2016-10-30 14:26:12 +01:00
|
|
|
}
|