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.
|
||||
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
|
||||
}
|
||||
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