Start with adding colors for messages

This commit is contained in:
erroneousboat 2017-12-03 21:43:33 +01:00
parent 45b1dac1bf
commit 64c2257df0
4 changed files with 63 additions and 31 deletions

View File

@ -18,15 +18,21 @@ type Message struct {
Content string
}
func (m Message) ToString() string {
return html.UnescapeString(
fmt.Sprintf(
"[%s] <%s> %s",
m.Time.Format("15:04"),
m.Name,
m.Content,
),
)
func (m Message) ToString(timeAttr string, nameAttr string, contentAttr string) string {
if (m.Time != time.Time{} && m.Name != "") {
return html.UnescapeString(
fmt.Sprintf(
"[[%s]](fg-red,fg-bold) [<%s>](fg-blue,fg-bold) %s",
m.Time.Format("15:04"),
m.Name,
m.Content,
),
)
} else {
return html.UnescapeString(
fmt.Sprintf("%s", m.Content),
)
}
}
// Chat is the definition of a Chat component

View File

@ -109,7 +109,9 @@ func messageHandler(ctx *context.AppContext) {
// reverse order of messages, mainly done
// when attachments are added to message
for i := len(msg) - 1; i >= 0; i-- {
ctx.View.Chat.AddMessage(msg[i])
ctx.View.Chat.AddMessage(
msg[i].ToString(),
)
}
termui.Render(ctx.View.Chat)
@ -259,11 +261,17 @@ func actionSearchMode(ctx *context.AppContext) {
}
func actionGetMessages(ctx *context.AppContext) {
messages := ctx.Service.GetMessages(
msgs := ctx.Service.GetMessages(
ctx.Service.Channels[ctx.View.Channels.SelectedChannel],
ctx.View.Chat.GetMaxItems(),
)
ctx.View.Chat.SetMessages(messages)
var strMsgs []string
for _, msg := range msgs {
strMsgs = append(strMsgs, msg.ToString())
}
ctx.View.Chat.SetMessages(strMsgs)
termui.Render(ctx.View.Chat)
}
@ -324,13 +332,18 @@ func actionChangeChannel(ctx *context.AppContext) {
// Get messages of the SelectedChannel, and get the count of messages
// that fit into the Chat component
messages := ctx.Service.GetMessages(
msgs := ctx.Service.GetMessages(
ctx.Service.GetSlackChannel(ctx.View.Channels.SelectedChannel),
ctx.View.Chat.GetMaxItems(),
)
var strMsgs []string
for _, msg := range msgs {
strMsgs = append(strMsgs, msg.ToString())
}
// Set messages for the channel
ctx.View.Chat.SetMessages(messages)
ctx.View.Chat.SetMessages(strMsgs)
// FIXME
// Set channel name for the Chat pane

View File

@ -271,7 +271,7 @@ func (s *SlackService) SendMessage(channelID int, message string) {
// GetMessages will get messages for a channel, group or im channel delimited
// by a count.
func (s *SlackService) GetMessages(channel interface{}, count int) []string {
func (s *SlackService) GetMessages(channel interface{}, count int) []components.Message {
// https://api.slack.com/methods/channels.history
historyParams := slack.HistoryParameters{
Count: count,
@ -301,7 +301,7 @@ func (s *SlackService) GetMessages(channel interface{}, count int) []string {
}
// Construct the messages
var messages []string
var messages []components.Message
for _, message := range history.Messages {
msg := s.CreateMessage(message)
messages = append(messages, msg...)
@ -309,7 +309,7 @@ func (s *SlackService) GetMessages(channel interface{}, count int) []string {
// Reverse the order of the messages, we want the newest in
// the last place
var messagesReversed []string
var messagesReversed []components.Message
for i := len(messages) - 1; i >= 0; i-- {
messagesReversed = append(messagesReversed, messages[i])
}
@ -324,8 +324,8 @@ func (s *SlackService) GetMessages(channel interface{}, count int) []string {
//
// This returns an array of string because we will try to uncover attachments
// associated with messages.
func (s *SlackService) CreateMessage(message slack.Message) []string {
var msgs []string
func (s *SlackService) CreateMessage(message slack.Message) []components.Message {
var msgs []components.Message
var name string
// Get username from cache
@ -377,14 +377,14 @@ func (s *SlackService) CreateMessage(message slack.Message) []string {
Content: parseMessage(s, message.Text),
}
msgs = append(msgs, msg.ToString())
msgs = append(msgs, msg)
return msgs
}
func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent) []string {
func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent) []components.Message {
var msgs []string
var msgs []components.Message
var name string
// Append (edited) when an edited message is received
@ -442,7 +442,7 @@ func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent
Content: parseMessage(s, message.Text),
}
msgs = append(msgs, msg.ToString())
msgs = append(msgs, msg)
return msgs
}
@ -524,25 +524,32 @@ func parseEmoji(msg string) string {
// createMessageFromAttachments will construct a array of string of the Field
// values of Attachments from a Message.
func createMessageFromAttachments(atts []slack.Attachment) []string {
var msgs []string
func createMessageFromAttachments(atts []slack.Attachment) []components.Message {
var msgs []components.Message
for _, att := range atts {
for i := len(att.Fields) - 1; i >= 0; i-- {
msgs = append(msgs,
fmt.Sprintf(
msgs = append(msgs, components.Message{
Content: fmt.Sprintf(
"%s %s",
att.Fields[i].Title,
att.Fields[i].Value,
),
},
)
}
if att.Text != "" {
msgs = append(msgs, att.Text)
msgs = append(
msgs,
components.Message{Content: fmt.Sprintf("%s", att.Text)},
)
}
if att.Title != "" {
msgs = append(msgs, att.Title)
msgs = append(
msgs,
components.Message{Content: fmt.Sprintf("%s", att.Title)},
)
}
}

View File

@ -30,11 +30,17 @@ func CreateView(svc *service.SlackService) *View {
chat := components.CreateChatComponent(input.Par.Height)
// Chat: fill the component
slackMsgs := svc.GetMessages(
msgs := svc.GetMessages(
svc.GetSlackChannel(channels.SelectedChannel),
chat.GetMaxItems(),
)
chat.SetMessages(slackMsgs)
var strMsgs []string
for _, msg := range msgs {
strMsgs = append(strMsgs, msg.ToString())
}
chat.SetMessages(strMsgs)
chat.SetBorderLabel(svc.Channels[channels.SelectedChannel].GetChannelName())
// Debug: create the component