diff --git a/prettyFormat.go b/prettyFormat.go index 2c07c0e..a360275 100644 --- a/prettyFormat.go +++ b/prettyFormat.go @@ -36,7 +36,7 @@ func prettyFormatChat(chat *chatpb.Chat) { content := entry.GetContent() if content != "" { - fmt.Printf("✦ %s (%s):\n%s\n", author, formattedTime, strings.TrimSpace(content)) + printContent(author, formattedTime, content) } if table := entry.GetTable(); table != nil { @@ -54,6 +54,41 @@ 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 + } + + 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