summaryrefslogtreecommitdiff
path: root/doPlayback.go
blob: 2f01c932f3452513983bd8d10e5ce49bba9ff3ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main

import (
	"fmt"
	"os"

	"go.wit.com/lib/protobuf/chatpb"
	"go.wit.com/log"
)

// doPlayback loads a specified log file and prints a one-line summary for each chat topic.
// If no filename is provided, it summarizes the current in-memory chat session.
func doPlayback(filename string) {
	var playbackChats *chatpb.Chats
	var err error

	// If a filename is provided, load that file. Otherwise, use the existing session.
	if filename != "" {
		log.Infof("Loading log file for playback: %s", filename)
		data, err := os.ReadFile(filename)
		if err != nil {
			log.Fatalf("Failed to read playback file: %v", err)
		}
		playbackChats, err = chatpb.UnmarshalChatsTEXT(data)
		if err != nil {
			log.Fatalf("Failed to parse playback file: %v", err)
		}
	} else {
		log.Info("No log file specified. Displaying summary of current conversation state.")
		playbackChats = me.chats
	}

	// Ensure all chats and entries have UUIDs before printing.
	verifyUuids(playbackChats)

	fmt.Println("-------------------------------------------------")
	log.Infof("Found %d chat topic(s).", len(playbackChats.GetChats()))
	fmt.Println("-------------------------------------------------")

	for _, chat := range playbackChats.GetChats() {
		entryCount := len(chat.GetEntries())
		var formattedTime string

		// Use the chat's top-level ctime for the summary.
		if ctime := chat.GetCtime(); ctime != nil {
			t := ctime.AsTime()
			formattedTime = t.Format("2006-01-02 15:04:05")
		} else {
			formattedTime = "No Timestamp"
		}

		fmt.Printf("Topic: %-25s | Entries: %-4d | Started: %s | UUID: %s\n",
			chat.GetChatName(),
			entryCount,
			formattedTime,
			chat.GetUuid(),
		)
	}
	fmt.Println("-------------------------------------------------")
}