erroneousboat-slack-term/components/message.go

96 lines
1.4 KiB
Go
Raw Permalink Normal View History

2018-12-22 12:33:30 +01:00
package components
import (
2018-12-24 14:37:12 +01:00
"fmt"
"sort"
2018-12-22 12:33:30 +01:00
"strings"
"time"
)
var (
COLORS = []string{
"fg-black",
"fg-red",
"fg-green",
"fg-yellow",
"fg-blue",
"fg-magenta",
"fg-cyan",
"fg-white",
}
)
type Message struct {
2018-12-24 14:37:12 +01:00
ID string
Messages map[string]Message
2018-12-22 12:33:30 +01:00
Time time.Time
2019-02-16 22:45:36 +01:00
Thread string
2018-12-22 12:33:30 +01:00
Name string
Content string
2019-02-16 22:45:36 +01:00
StyleTime string
StyleThread string
StyleName string
StyleText string
2018-12-22 12:33:30 +01:00
FormatTime string
}
2018-12-24 14:37:12 +01:00
func (m Message) GetTime() string {
return fmt.Sprintf(
"[[%s]](%s) ",
m.Time.Format(m.FormatTime),
m.StyleTime,
)
}
2019-02-16 22:45:36 +01:00
func (m Message) GetThread() string {
return fmt.Sprintf("[%s](%s)",
m.Thread,
m.StyleThread,
)
}
2018-12-24 14:37:12 +01:00
func (m Message) GetName() string {
return fmt.Sprintf("[<%s>](%s) ",
m.Name,
m.colorizeName(m.StyleName),
)
}
func (m Message) GetContent() string {
return fmt.Sprintf("[.](%s)", m.StyleText)
}
2018-12-22 12:33:30 +01:00
func (m Message) colorizeName(styleName string) string {
if strings.Contains(styleName, "colorize") {
var sum int
for _, c := range m.Name {
sum = sum + int(c)
}
i := sum % len(COLORS)
return strings.Replace(m.StyleName, "colorize", COLORS[i], -1)
}
return styleName
}
2018-12-24 14:37:12 +01:00
func SortMessages(msgs map[string]Message) []Message {
keys := make([]string, 0)
for k := range msgs {
keys = append(keys, k)
}
sort.Strings(keys)
sortedMessages := make([]Message, 0)
for _, k := range keys {
sortedMessages = append(sortedMessages, msgs[k])
}
return sortedMessages
}