ChatView Day delimiters between chats
The chat now displays a saperator as soon as there's a change of day between two messages.
MODIFY
times/times.go
times/times.go
@@ -26,3 +26,11 @@ func TimeToString(time *time.Time) string {
}
return ""
}
+
+// CompareMessageDates returns true if the dates match, otherwise returns false
+func CompareMessageDates(t1, t2 time.Time) bool {
+ if t1.Year() == t2.Year() && t1.YearDay() == t2.YearDay() {
+ return true
+ }
+ return false
+}

MODIFY
ui/chatview.go
ui/chatview.go
@@ -49,6 +49,7 @@ type ChatView struct {
data []*discordgo.Message
bufferSize int
ownUserID string
+ format string
shortenLinks bool
@@ -69,6 +70,7 @@ func NewChatView(state *discordgo.State, ownUserID string) *ChatView {
internalTextView: tview.NewTextView(),
state: state,
ownUserID: ownUserID,
+ format: "2006-01-02",
selection: -1,
bufferSize: 100,
selectionMode: false,
@@ -316,6 +318,11 @@ func (chatView *ChatView) addMessageInternal(message *discordgo.Message) {
//AddMessage add an additional message to the ChatView.
func (chatView *ChatView) AddMessage(message *discordgo.Message) {
wasScrolledToTheEnd := chatView.internalTextView.IsScrolledToEnd()
+ t1, _ := chatView.data[len(chatView.data)-1].Timestamp.Parse()
+ t2, _ := message.Timestamp.Parse()
+ if !times.CompareMessageDates(t1.Local(), t2.Local()) {
+ fmt.Fprint(chatView.internalTextView, chatView.CreateDateDelimiter(t2.Format(chatView.format)))
+ }
chatView.addMessageInternal(message)
@@ -325,6 +332,38 @@ func (chatView *ChatView) AddMessage(message *discordgo.Message) {
}
}
+// CreateDateDelimiter creates a date delimiter between messages to mark the date and returns it
+func (chatView *ChatView) CreateDateDelimiter(date string) string {
+ _, _, width, _ := chatView.internalTextView.GetInnerRect()
+ dashes := (width - len(chatView.format)) / 2
+ padding := strings.Repeat("\u2500", dashes-1)
+ dateDelimiterLine := "\n[\"" + "\"]" + padding + " " + date + " " + padding
+ return dateDelimiterLine
+}
+
+// ReturnDateDelimiter creates datedelimiters between two messages and returns them
+func (chatView *ChatView) ReturnDateDelimiter(messages []*discordgo.Message, index int) string {
+ var res string
+ if index > 0 {
+ t1, _ := messages[index-1].Timestamp.Parse()
+ t2, _ := messages[index].Timestamp.Parse()
+
+ if !times.CompareMessageDates(t1.Local(), t2.Local()) {
+ res = chatView.CreateDateDelimiter(t2.Local().Format(chatView.format))
+ }
+ } else if index == 0 {
+ time, _ := messages[index].Timestamp.Parse()
+ date := time.Format(chatView.format)
+ res = chatView.CreateDateDelimiter(date)
+ }
+ return res
+}
+
+// WriteDateDelimiter runs ReturnDateDelimiter and writes it to chatView.internalTextView
+func (chatView *ChatView) WriteDateDelimiter(messages []*discordgo.Message, index int) {
+ fmt.Fprint(chatView.internalTextView, chatView.ReturnDateDelimiter(messages, index))
+}
+
// AddMessages is the same as AddMessage, but for an array of messages instead
// of a single message. Calling this method will not repeat certain actions and
// therefore be slightly more performant than calling AddMessage multiple
@@ -332,7 +371,8 @@ func (chatView *ChatView) AddMessage(message *discordgo.Message) {
func (chatView *ChatView) AddMessages(messages []*discordgo.Message) {
wasScrolledToTheEnd := chatView.internalTextView.IsScrolledToEnd()
- for _, message := range messages {
+ for index, message := range messages {
+ chatView.WriteDateDelimiter(messages, index)
chatView.addMessageInternal(message)
}
@@ -350,6 +390,7 @@ func (chatView *ChatView) Rerender() {
formattedMessage, contains := chatView.formattedMessages[message.ID]
//Should always be true, otherwise we got ourselves a bug.
if contains {
+ newContent += chatView.ReturnDateDelimiter(chatView.data, index)
newContent = newContent + "\n[\"" + intToString(index) + "\"]" + formattedMessage
} else {
panic("Bug in chatview, a message could not be found.")

ChatView Day delimiters between chats (#181)

The chat now displays a saperator as soon as there's a change of day between two messages.