Show messages in message view while sending

This commit is contained in:
Tulir Asokan 2018-03-20 19:14:39 +02:00
parent 59cd900301
commit 095b982603
6 changed files with 93 additions and 66 deletions

View File

@ -28,10 +28,7 @@ type MatrixContainer interface {
Login(user, password string) error Login(user, password string) error
Start() Start()
Stop() Stop()
// HandleMessage(evt *gomatrix.Event) SendMessage(roomID, message string) (string, error)
// HandleMembership(evt *gomatrix.Event)
// HandleTyping(evt *gomatrix.Event)
SendMessage(roomID, message string)
SendTyping(roomID string, typing bool) SendTyping(roomID string, typing bool)
JoinRoom(roomID string) error JoinRoom(roomID string) error
LeaveRoom(roomID string) error LeaveRoom(roomID string) error

View File

@ -213,10 +213,14 @@ func (c *Container) HandleTyping(evt *gomatrix.Event) {
c.ui.MainView().SetTyping(evt.RoomID, strUsers) c.ui.MainView().SetTyping(evt.RoomID, strUsers)
} }
func (c *Container) SendMessage(roomID, message string) { func (c *Container) SendMessage(roomID, text string) (string, error) {
defer c.gmx.Recover() defer c.gmx.Recover()
c.SendTyping(roomID, false) c.SendTyping(roomID, false)
c.client.SendText(roomID, message) resp, err := c.client.SendText(roomID, text)
if err != nil {
return "", err
}
return resp.EventID, nil
} }
func (c *Container) SendTyping(roomID string, typing bool) { func (c *Container) SendTyping(roomID string, typing bool) {
@ -250,8 +254,6 @@ func (c *Container) JoinRoom(roomID string) error {
return err return err
} }
// TODO probably safe to remove
// c.ui.MainView().AddRoom(resp.RoomID)
return nil return nil
} }

View File

@ -25,24 +25,26 @@ import (
) )
type Message struct { type Message struct {
BasicMeta
ID string ID string
Sender string
Text string Text string
Timestamp string
Date string
Buffer []string Buffer []string
SenderColor tcell.Color prevBufferWidth int
} }
func NewMessage(id, sender, text, timestamp, date string, senderColor tcell.Color) *Message { func NewMessage(id, sender, text, timestamp, date string, senderColor tcell.Color) *Message {
return &Message{ return &Message{
ID: id, BasicMeta: BasicMeta{
Sender: sender, Sender: sender,
Text: text,
Timestamp: timestamp, Timestamp: timestamp,
Date: date, Date: date,
SenderColor: senderColor, SenderColor: senderColor,
TextColor: tcell.ColorDefault,
TimestampColor: tcell.ColorDefault,
},
Text: text,
ID: id,
prevBufferWidth: 0,
} }
} }
@ -51,6 +53,13 @@ var (
spacePattern = regexp.MustCompile(`\s+`) spacePattern = regexp.MustCompile(`\s+`)
) )
func (message *Message) CopyTo(to *Message) {
to.BasicMeta = message.BasicMeta
to.ID = message.ID
to.Text = message.Text
to.RecalculateBuffer()
}
func (message *Message) CalculateBuffer(width int) { func (message *Message) CalculateBuffer(width int) {
if width < 1 { if width < 1 {
return return
@ -82,28 +91,9 @@ func (message *Message) CalculateBuffer(width int) {
str = str[len(extract):] str = str[len(extract):]
} }
} }
message.prevBufferWidth = width
} }
func (message *Message) GetSender() string { func (message *Message) RecalculateBuffer() {
return message.Sender message.CalculateBuffer(message.prevBufferWidth)
}
func (message *Message) GetSenderColor() tcell.Color {
return message.SenderColor
}
func (message *Message) GetTimestamp() string {
return message.Timestamp
}
func (message *Message) GetDate() string {
return message.Date
}
func (message *Message) GetTextColor() tcell.Color {
return tcell.ColorDefault
}
func (message *Message) GetTimestampColor() tcell.Color {
return tcell.ColorDefault
} }

View File

@ -19,6 +19,7 @@ package ui
import ( import (
"fmt" "fmt"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
"unicode" "unicode"
@ -122,7 +123,7 @@ func (view *MainView) InputTabComplete(text string, cursorOffset int) string {
word := findWordToTabComplete(str) word := findWordToTabComplete(str)
userCompletions := roomView.AutocompleteUser(word) userCompletions := roomView.AutocompleteUser(word)
if len(userCompletions) == 1 { if len(userCompletions) == 1 {
startIndex := len(str)-len(word) startIndex := len(str) - len(word)
completion := userCompletions[0] completion := userCompletions[0]
if startIndex == 0 { if startIndex == 0 {
completion = completion + ": " completion = completion + ": "
@ -136,7 +137,9 @@ func (view *MainView) InputTabComplete(text string, cursorOffset int) string {
} }
func (view *MainView) InputDone(key tcell.Key) { func (view *MainView) InputDone(key tcell.Key) {
if key == tcell.KeyEnter { if key != tcell.KeyEnter {
return
}
room, text := view.CurrentRoomID(), view.input.GetText() room, text := view.CurrentRoomID(), view.input.GetText()
if len(text) == 0 { if len(text) == 0 {
return return
@ -146,10 +149,34 @@ func (view *MainView) InputDone(key tcell.Key) {
args = args[1:] args = args[1:]
go view.HandleCommand(room, command, args) go view.HandleCommand(room, command, args)
} else { } else {
go view.matrix.SendMessage(room, text) view.SendMessage(room, text)
} }
view.input.SetText("") view.input.SetText("")
}
func (view *MainView) SendMessage(room, text string) {
now := time.Now()
roomView := view.GetRoom(room)
tempMessage := roomView.NewMessage(
strconv.FormatInt(now.UnixNano(), 10),
"Sending...", text, now)
tempMessage.TimestampColor = tcell.ColorGray
tempMessage.TextColor = tcell.ColorGray
tempMessage.SenderColor = tcell.ColorGray
roomView.AddMessage(tempMessage, widget.AppendMessage)
go func() {
defer view.gmx.Recover()
eventID, err := view.matrix.SendMessage(room, text)
if err != nil {
tempMessage.TextColor = tcell.ColorRed
tempMessage.TimestampColor = tcell.ColorRed
tempMessage.SenderColor = tcell.ColorRed
tempMessage.Sender = "Error"
roomView.SetStatus(fmt.Sprintf("Failed to send message: %s", err))
} else {
roomView.MessageView().UpdateMessageID(tempMessage, eventID)
} }
}()
} }
func (view *MainView) HandleCommand(room, command string, args []string) { func (view *MainView) HandleCommand(room, command string, args []string) {
@ -418,6 +445,7 @@ func (view *MainView) ProcessMembershipEvent(evt *gomatrix.Event, new bool) (roo
return return
} }
message = room.NewMessage(evt.ID, sender, text, unixToTime(evt.Timestamp)) message = room.NewMessage(evt.ID, sender, text, unixToTime(evt.Timestamp))
message.TextColor = tcell.ColorGreen
} }
return return
} }

View File

@ -42,7 +42,7 @@ type MessageView struct {
prevHeight int prevHeight int
prevMsgCount int prevMsgCount int
messageIDs map[string]bool messageIDs map[string]*types.Message
messages []*types.Message messages []*types.Message
textBuffer []string textBuffer []string
@ -60,7 +60,7 @@ func NewMessageView() *MessageView {
ScrollOffset: 0, ScrollOffset: 0,
messages: make([]*types.Message, 0), messages: make([]*types.Message, 0),
messageIDs: make(map[string]bool), messageIDs: make(map[string]*types.Message),
textBuffer: make([]string, 0), textBuffer: make([]string, 0),
metaBuffer: make([]types.MessageMeta, 0), metaBuffer: make([]types.MessageMeta, 0),
@ -87,15 +87,25 @@ func (view *MessageView) updateWidestSender(sender string) {
} }
} }
type MessageDirection int
const ( const (
AppendMessage = iota AppendMessage MessageDirection = iota
PrependMessage PrependMessage
IgnoreMessage
) )
func (view *MessageView) AddMessage(message *types.Message, direction int) { func (view *MessageView) UpdateMessageID(message *types.Message, newID string) {
_, messageExists := view.messageIDs[message.ID] delete(view.messageIDs, message.ID)
message.ID = newID
view.messageIDs[message.ID] = message
}
func (view *MessageView) AddMessage(message *types.Message, direction MessageDirection) {
msg, messageExists := view.messageIDs[message.ID]
if messageExists { if messageExists {
return message.CopyTo(msg)
direction = IgnoreMessage
} }
view.updateWidestSender(message.Sender) view.updateWidestSender(message.Sender)
@ -114,7 +124,7 @@ func (view *MessageView) AddMessage(message *types.Message, direction int) {
view.messages = append([]*types.Message{message}, view.messages...) view.messages = append([]*types.Message{message}, view.messages...)
} }
view.messageIDs[message.ID] = true view.messageIDs[message.ID] = message
} }
func (view *MessageView) appendBuffer(message *types.Message) { func (view *MessageView) appendBuffer(message *types.Message) {

View File

@ -147,6 +147,6 @@ func (view *RoomView) NewMessage(id, sender, text string, timestamp time.Time) *
return view.content.NewMessage(id, sender, text, timestamp) return view.content.NewMessage(id, sender, text, timestamp)
} }
func (view *RoomView) AddMessage(message *types.Message, direction int) { func (view *RoomView) AddMessage(message *types.Message, direction MessageDirection) {
view.content.AddMessage(message, direction) view.content.AddMessage(message, direction)
} }