Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit 57ffa7b

Browse files
misutoBios-Marcel
authored andcommitted
ChatView Day delimiters between chats (#181)
The chat now displays a saperator as soon as there's a change of day between two messages.
1 parent 9c61021 commit 57ffa7b

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

times/times.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ func TimeToString(time *time.Time) string {
2626
}
2727
return ""
2828
}
29+
30+
// CompareMessageDates returns true if the dates match, otherwise returns false
31+
func CompareMessageDates(t1, t2 time.Time) bool {
32+
if t1.Year() == t2.Year() && t1.YearDay() == t2.YearDay() {
33+
return true
34+
}
35+
return false
36+
}

ui/chatview.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type ChatView struct {
4949
data []*discordgo.Message
5050
bufferSize int
5151
ownUserID string
52+
format string
5253

5354
shortenLinks bool
5455

@@ -69,6 +70,7 @@ func NewChatView(state *discordgo.State, ownUserID string) *ChatView {
6970
internalTextView: tview.NewTextView(),
7071
state: state,
7172
ownUserID: ownUserID,
73+
format: "2006-01-02",
7274
selection: -1,
7375
bufferSize: 100,
7476
selectionMode: false,
@@ -316,6 +318,11 @@ func (chatView *ChatView) addMessageInternal(message *discordgo.Message) {
316318
//AddMessage add an additional message to the ChatView.
317319
func (chatView *ChatView) AddMessage(message *discordgo.Message) {
318320
wasScrolledToTheEnd := chatView.internalTextView.IsScrolledToEnd()
321+
t1, _ := chatView.data[len(chatView.data)-1].Timestamp.Parse()
322+
t2, _ := message.Timestamp.Parse()
323+
if !times.CompareMessageDates(t1.Local(), t2.Local()) {
324+
fmt.Fprint(chatView.internalTextView, chatView.CreateDateDelimiter(t2.Format(chatView.format)))
325+
}
319326

320327
chatView.addMessageInternal(message)
321328

@@ -325,14 +332,47 @@ func (chatView *ChatView) AddMessage(message *discordgo.Message) {
325332
}
326333
}
327334

335+
// CreateDateDelimiter creates a date delimiter between messages to mark the date and returns it
336+
func (chatView *ChatView) CreateDateDelimiter(date string) string {
337+
_, _, width, _ := chatView.internalTextView.GetInnerRect()
338+
dashes := (width - len(chatView.format)) / 2
339+
padding := strings.Repeat("\u2500", dashes-1)
340+
dateDelimiterLine := "\n[\"" + "\"]" + padding + " " + date + " " + padding
341+
return dateDelimiterLine
342+
}
343+
344+
// ReturnDateDelimiter creates datedelimiters between two messages and returns them
345+
func (chatView *ChatView) ReturnDateDelimiter(messages []*discordgo.Message, index int) string {
346+
var res string
347+
if index > 0 {
348+
t1, _ := messages[index-1].Timestamp.Parse()
349+
t2, _ := messages[index].Timestamp.Parse()
350+
351+
if !times.CompareMessageDates(t1.Local(), t2.Local()) {
352+
res = chatView.CreateDateDelimiter(t2.Local().Format(chatView.format))
353+
}
354+
} else if index == 0 {
355+
time, _ := messages[index].Timestamp.Parse()
356+
date := time.Format(chatView.format)
357+
res = chatView.CreateDateDelimiter(date)
358+
}
359+
return res
360+
}
361+
362+
// WriteDateDelimiter runs ReturnDateDelimiter and writes it to chatView.internalTextView
363+
func (chatView *ChatView) WriteDateDelimiter(messages []*discordgo.Message, index int) {
364+
fmt.Fprint(chatView.internalTextView, chatView.ReturnDateDelimiter(messages, index))
365+
}
366+
328367
// AddMessages is the same as AddMessage, but for an array of messages instead
329368
// of a single message. Calling this method will not repeat certain actions and
330369
// therefore be slightly more performant than calling AddMessage multiple
331370
// times.
332371
func (chatView *ChatView) AddMessages(messages []*discordgo.Message) {
333372
wasScrolledToTheEnd := chatView.internalTextView.IsScrolledToEnd()
334373

335-
for _, message := range messages {
374+
for index, message := range messages {
375+
chatView.WriteDateDelimiter(messages, index)
336376
chatView.addMessageInternal(message)
337377
}
338378

@@ -350,6 +390,7 @@ func (chatView *ChatView) Rerender() {
350390
formattedMessage, contains := chatView.formattedMessages[message.ID]
351391
//Should always be true, otherwise we got ourselves a bug.
352392
if contains {
393+
newContent += chatView.ReturnDateDelimiter(chatView.data, index)
353394
newContent = newContent + "\n[\"" + intToString(index) + "\"]" + formattedMessage
354395
} else {
355396
panic("Bug in chatview, a message could not be found.")

0 commit comments

Comments
 (0)