summaryrefslogtreecommitdiff
path: root/doPlayback.go
blob: 14898794e62b07fc3bc44aae56543bfe47099e7e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main

import (
	"strings"

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

func doPlayback() error {
	if argv.Uuid != "" {
		showChat(argv.Uuid)
		return nil
	}

	if argv.Playback.Purge != nil {
		doPurge()
		return nil
	}

	listChats(me.chats)
	return nil
}

func doPurge() {
	changed := false
	for _, chat := range me.chats.GetChats() {
		if len(chat.GetEntries()) == 0 {
			me.chats.Delete(chat)
			changed = true
		}
	}
	if changed {
		me.chats.ConfigSave()
	}
}

func showChat(uuid string) {
	chat := me.chats.FindByUuid(uuid)
	if chat == nil {
		log.Info("unknown uuid", uuid)
		return
	}
	prettyFormatChat(chat)
}

func listChats(chats *chatpb.Chats) {
	if argv.Playback.Long != nil {
		for _, chat := range chats.GetChats() {
			listEntries(chat)
		}
		return
	}

	log.Info("Found %d chat topic(s) in the log.", len(chats.GetChats()))
	log.Println("-------------------------------------------------")

	for _, chat := range chats.GetChats() {
		entryCount := len(chat.GetEntries())
		var formattedTime string
		if ctime := chat.GetCtime(); ctime != nil {
			t := ctime.AsTime()
			formattedTime = t.Format("2006-01-02 15:04:05")
		} else {
			formattedTime = "No Timestamp"
		}

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

	// Get the last chat
	numChats := len(chats.GetChats())
	if numChats > 0 {
		me.lastChat = chats.GetChats()[numChats-1]
		log.Printf("The current Gemini API session is UUID: %s\n", me.lastChat.GetUuid())
		dumpchat(me.lastChat)
	}
}

// print out one line for each chat entry
func listEntries(chat *chatpb.Chat) {
	log.Printf("--- Entries for Topic: %s ---\n", chat.GetChatName())
	width, _ := getTerminalWidth()

	// Determine the maximum length of the author and time string
	maxAuthorAndTimeLen := 0
	for _, entry := range chat.GetEntries() {
		author := entry.GetFrom().String()
		var formattedTime string
		if ctime := entry.GetCtime(); ctime != nil {
			t := ctime.AsTime()
			formattedTime = t.Format("2006-01-02 15:04:05")
		} else {
			formattedTime = "No Time"
		}
		authorAndTime := log.Sprintf("[%s] (%s)", author, formattedTime)
		if len(authorAndTime) > maxAuthorAndTimeLen {
			maxAuthorAndTimeLen = len(authorAndTime)
		}
	}

	for _, entry := range chat.GetEntries() {
		author := entry.GetFrom().String()
		var formattedTime string
		if ctime := entry.GetCtime(); ctime != nil {
			t := ctime.AsTime()
			formattedTime = t.Format("2006-01-02 15:04:05")
		} else {
			formattedTime = "No Time"
		}

		// Create a short preview of the content
		contentPreview := strings.TrimSpace(entry.GetContent())
		// Replace newlines with spaces for a clean one-line view
		contentPreview = strings.ReplaceAll(contentPreview, "\n", " ")

		authorAndTime := log.Sprintf("[%s] (%s)", author, formattedTime)

		availableWidth := width - maxAuthorAndTimeLen - 1 // -1 for a space
		if len(contentPreview) > availableWidth {
			contentPreview = contentPreview[:availableWidth-3] + "..."
		}

		if author == "USER" {
			// Calculate padding to fill the space between content and the right-aligned author/time
			padding := width - len(contentPreview) - maxAuthorAndTimeLen
			if padding < 0 {
				padding = 0
			}
			// Calculate padding to align the author/time string itself
			authorAndTimePadding := maxAuthorAndTimeLen - len(authorAndTime)
			if authorAndTimePadding < 0 {
				authorAndTimePadding = 0
			}
			log.Printf("%s%s%s%s\n", contentPreview, strings.Repeat(" ", padding), strings.Repeat(" ", authorAndTimePadding), authorAndTime)
		} else {
			padding := maxAuthorAndTimeLen - len(authorAndTime)
			if padding < 0 {
				padding = 0
			}
			log.Printf("%s%s %s\n", authorAndTime, strings.Repeat(" ", padding), contentPreview)
		}
	}
	log.Println("-------------------------------------------------")
}