fix(playback): Improve content word wrapping

- Refactor the 'printContent' function to provide a cleaner layout.
- The author and timestamp prefix now appear on their own line.
- All subsequent lines of content are indented with a standard tab
  and wrapped correctly to the terminal width.
This commit is contained in:
Castor Gemini 2025-08-22 05:28:06 -05:00 committed by Jeff Carr
parent 69395ccb0a
commit b8b64da118
1 changed files with 32 additions and 26 deletions

View File

@ -56,36 +56,42 @@ func prettyFormatChat(chat *chatpb.Chat) {
// printContent handles the wrapping for the main conversational text. // printContent handles the wrapping for the main conversational text.
func printContent(author, timestamp, content string) { func printContent(author, timestamp, content string) {
prefix := fmt.Sprintf("✦ %s (%s): ", author, timestamp) prefix := fmt.Sprintf("✦ %s (%s):", author, timestamp)
fmt.Println(prefix)
// The available width for the text is the total width minus the prefix length. // The available width for the text is the total width minus the tab indent.
contentWidth := termWidth - len(prefix) indent := "\t"
if contentWidth < 10 { // Ensure we have a reasonable minimum width
contentWidth = 10
}
lines := strings.Split(strings.TrimSpace(content), "\n") lines := strings.Split(strings.TrimSpace(content), "\n")
// Print the first line with the prefix. for _, line := range lines {
firstLine := lines[0] // Handle empty lines in the original content as paragraph breaks.
fmt.Printf("%s", prefix) if strings.TrimSpace(line) == "" {
for len(firstLine) > contentWidth { fmt.Println()
fmt.Printf("%s\n", firstLine[:contentWidth]) continue
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. words := strings.Fields(line)
for _, line := range lines[1:] { if len(words) == 0 {
fmt.Printf("%s", strings.Repeat(" ", len(prefix))) continue
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)
currentLine := indent
for _, word := range words {
// Check if adding the next word exceeds the content width.
if len(currentLine)+len(word)+1 > termWidth {
fmt.Println(strings.TrimSpace(currentLine))
currentLine = indent + word
} else {
if currentLine == indent {
currentLine += word
} else {
currentLine += " " + word
}
}
}
// Print the last line of the paragraph.
fmt.Println(strings.TrimSpace(currentLine))
} }
} }