@@ -49,6 +49,7 @@ type ChatView struct {
49
49
data []* discordgo.Message
50
50
bufferSize int
51
51
ownUserID string
52
+ format string
52
53
53
54
shortenLinks bool
54
55
@@ -69,6 +70,7 @@ func NewChatView(state *discordgo.State, ownUserID string) *ChatView {
69
70
internalTextView : tview .NewTextView (),
70
71
state : state ,
71
72
ownUserID : ownUserID ,
73
+ format : "2006-01-02" ,
72
74
selection : - 1 ,
73
75
bufferSize : 100 ,
74
76
selectionMode : false ,
@@ -316,6 +318,11 @@ func (chatView *ChatView) addMessageInternal(message *discordgo.Message) {
316
318
//AddMessage add an additional message to the ChatView.
317
319
func (chatView * ChatView ) AddMessage (message * discordgo.Message ) {
318
320
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
+ }
319
326
320
327
chatView .addMessageInternal (message )
321
328
@@ -325,14 +332,47 @@ func (chatView *ChatView) AddMessage(message *discordgo.Message) {
325
332
}
326
333
}
327
334
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
+
328
367
// AddMessages is the same as AddMessage, but for an array of messages instead
329
368
// of a single message. Calling this method will not repeat certain actions and
330
369
// therefore be slightly more performant than calling AddMessage multiple
331
370
// times.
332
371
func (chatView * ChatView ) AddMessages (messages []* discordgo.Message ) {
333
372
wasScrolledToTheEnd := chatView .internalTextView .IsScrolledToEnd ()
334
373
335
- for _ , message := range messages {
374
+ for index , message := range messages {
375
+ chatView .WriteDateDelimiter (messages , index )
336
376
chatView .addMessageInternal (message )
337
377
}
338
378
@@ -350,6 +390,7 @@ func (chatView *ChatView) Rerender() {
350
390
formattedMessage , contains := chatView .formattedMessages [message .ID ]
351
391
//Should always be true, otherwise we got ourselves a bug.
352
392
if contains {
393
+ newContent += chatView .ReturnDateDelimiter (chatView .data , index )
353
394
newContent = newContent + "\n [\" " + intToString (index ) + "\" ]" + formattedMessage
354
395
} else {
355
396
panic ("Bug in chatview, a message could not be found." )
0 commit comments