diff options
| author | Castor Gemini <[email protected]> | 2025-08-22 05:28:06 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-08-22 05:28:06 -0500 |
| commit | b8b64da118c11800416c93e3a0db48744c3af67f (patch) | |
| tree | 011a9ed64ca73ef017927cad6722fee30c8f6759 | |
| parent | 69395ccb0a1ab2167dff9726d35b61b181ffd97d (diff) | |
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.
| -rw-r--r-- | prettyFormat.go | 58 |
1 files changed, 32 insertions, 26 deletions
diff --git a/prettyFormat.go b/prettyFormat.go index a360275..22ea6b1 100644 --- a/prettyFormat.go +++ b/prettyFormat.go @@ -56,36 +56,42 @@ func prettyFormatChat(chat *chatpb.Chat) { // 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 - } + prefix := fmt.Sprintf("✦ %s (%s):", author, timestamp) + fmt.Println(prefix) + + // The available width for the text is the total width minus the tab indent. + indent := "\t" 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))) + for _, line := range lines { + // Handle empty lines in the original content as paragraph breaks. + if strings.TrimSpace(line) == "" { + fmt.Println() + continue + } + + words := strings.Fields(line) + if len(words) == 0 { + continue + } + + 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 + } + } } - fmt.Printf("%s\n", line) + // Print the last line of the paragraph. + fmt.Println(strings.TrimSpace(currentLine)) } } |
