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:
parent
69395ccb0a
commit
b8b64da118
|
@ -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.
|
|
||||||
contentWidth := termWidth - len(prefix)
|
// The available width for the text is the total width minus the tab indent.
|
||||||
if contentWidth < 10 { // Ensure we have a reasonable minimum width
|
indent := "\t"
|
||||||
contentWidth = 10
|
|
||||||
}
|
|
||||||
|
|
||||||
lines := strings.Split(strings.TrimSpace(content), "\n")
|
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 {
|
||||||
for _, line := range lines[1:] {
|
// Handle empty lines in the original content as paragraph breaks.
|
||||||
fmt.Printf("%s", strings.Repeat(" ", len(prefix)))
|
if strings.TrimSpace(line) == "" {
|
||||||
for len(line) > contentWidth {
|
fmt.Println()
|
||||||
fmt.Printf("%s\n", line[:contentWidth])
|
continue
|
||||||
line = line[contentWidth:]
|
|
||||||
fmt.Printf("%s", strings.Repeat(" ", len(prefix)))
|
|
||||||
}
|
}
|
||||||
fmt.Printf("%s\n", line)
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Print the last line of the paragraph.
|
||||||
|
fmt.Println(strings.TrimSpace(currentLine))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue