149 lines
5.0 KiB
Go
149 lines
5.0 KiB
Go
// This file contains functions for formatting and printing chat logs to the terminal.
|
|
// These functions operate exclusively on the in-memory protobuf data structures.
|
|
// They do NOT access the filesystem; all content (from content files, snippets, etc.)
|
|
// is expected to have been loaded into the 'Content' fields of the protobuf messages
|
|
// before these functions are called.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"go.wit.com/lib/protobuf/chatpb"
|
|
)
|
|
|
|
const termWidth = 100 // The target width for the formatted output boxes.
|
|
|
|
// prettyFormatChat is the main entry point to print a detailed view of a Chat topic.
|
|
func prettyFormatChat(chat *chatpb.Chat) {
|
|
fmt.Printf("\n========================================================\n")
|
|
fmt.Printf("== Chat Topic: %s (UUID: %s)\n", chat.GetChatName(), chat.GetUuid())
|
|
fmt.Printf("========================================================\n\n")
|
|
|
|
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 Timestamp"
|
|
}
|
|
|
|
// Use the in-memory Content field directly.
|
|
content := entry.GetContent()
|
|
|
|
if content != "" {
|
|
printContent(author, formattedTime, content)
|
|
}
|
|
|
|
if table := entry.GetTable(); table != nil {
|
|
printTable(table)
|
|
}
|
|
for _, toolCall := range entry.GetToolCalls() {
|
|
printToolCallBox(toolCall)
|
|
}
|
|
if snippets := entry.GetSnippets(); snippets != nil {
|
|
for _, snippet := range snippets {
|
|
printCodeSnippet(snippet)
|
|
}
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
// printContent handles the wrapping for the main conversational text.
|
|
func printContent(author, timestamp, content string) {
|
|
prefix := fmt.Sprintf("✦ %s (%s): ", author, timestamp)
|
|
|
|
// The available width for the text is the total width minus the prefix length.
|
|
contentWidth := termWidth - len(prefix)
|
|
if contentWidth < 10 { // Ensure we have a reasonable minimum width
|
|
contentWidth = 10
|
|
}
|
|
|
|
lines := strings.Split(strings.TrimSpace(content), "\n")
|
|
|
|
// Print the first line with the prefix.
|
|
firstLine := lines[0]
|
|
fmt.Printf("%s", prefix)
|
|
for len(firstLine) > contentWidth {
|
|
fmt.Printf("%s\n", firstLine[:contentWidth])
|
|
firstLine = firstLine[contentWidth:]
|
|
// Subsequent wrapped lines need an indent matching the prefix length.
|
|
fmt.Printf("%s", strings.Repeat(" ", len(prefix)))
|
|
}
|
|
fmt.Printf("%s\n", firstLine)
|
|
|
|
// Print subsequent paragraphs with the same wrapping logic.
|
|
for _, line := range lines[1:] {
|
|
fmt.Printf("%s", strings.Repeat(" ", len(prefix)))
|
|
for len(line) > contentWidth {
|
|
fmt.Printf("%s\n", line[:contentWidth])
|
|
line = line[contentWidth:]
|
|
fmt.Printf("%s", strings.Repeat(" ", len(prefix)))
|
|
}
|
|
fmt.Printf("%s\n", line)
|
|
}
|
|
}
|
|
|
|
func printTable(table *chatpb.Table) {
|
|
if table == nil || len(table.GetRows()) == 0 {
|
|
return
|
|
}
|
|
fmt.Println("┌─[ Table Data ]──────────────────────────────────────────")
|
|
for _, row := range table.GetRows() {
|
|
fmt.Printf("│ %s\n", strings.Join(row.GetFields(), " │ "))
|
|
}
|
|
fmt.Printf("└─────────────────────────────────────────────────────────\n\n")
|
|
}
|
|
|
|
func printCodeSnippet(snippet *chatpb.CodeSnippet) {
|
|
// Use the in-memory Content field directly.
|
|
code := snippet.GetContent()
|
|
language := filepath.Base(snippet.GetFilename()) // Still useful for display
|
|
fmt.Printf("┌─[ Code Snippet: %s ]──────────────────────────────────\n", language)
|
|
for _, line := range strings.Split(strings.TrimSpace(code), "\n") {
|
|
fmt.Printf("│ %s\n", line)
|
|
}
|
|
fmt.Printf("└─────────────────────────────────────────────────────────\n\n")
|
|
}
|
|
|
|
func printToolCallBox(tc *chatpb.ToolCall) {
|
|
boxWidth := termWidth - 2
|
|
fmt.Printf(" ╭%s╮\n", strings.Repeat("─", boxWidth))
|
|
header := fmt.Sprintf(" ✔ %s %s (%s)", tc.GetName(), tc.GetInput(), tc.GetDescription())
|
|
printWrappedLine(header, boxWidth)
|
|
printEmptyLine(boxWidth)
|
|
if stdout := tc.GetOutputStdout(); stdout != "" {
|
|
for _, line := range strings.Split(stdout, "\n") {
|
|
printWrappedLine(" "+line, boxWidth)
|
|
}
|
|
}
|
|
if stderr := tc.GetOutputStderr(); stderr != "" {
|
|
for _, line := range strings.Split(stderr, "\n") {
|
|
printWrappedLine(" "+line, boxWidth)
|
|
}
|
|
}
|
|
printEmptyLine(boxWidth)
|
|
fmt.Printf(" ╰%s╯\n", strings.Repeat("─", boxWidth))
|
|
}
|
|
|
|
func printWrappedLine(text string, width int) {
|
|
if len(text) == 0 {
|
|
printEmptyLine(width)
|
|
return
|
|
}
|
|
for len(text) > width {
|
|
fmt.Printf(" │ %-*s │\n", width, text[:width])
|
|
text = text[width:]
|
|
}
|
|
fmt.Printf(" │ %-*s │\n", width, text)
|
|
}
|
|
|
|
func printEmptyLine(width int) {
|
|
fmt.Printf(" │ %*s │\n", width, "")
|
|
}
|