diff options
| author | Castor Gemini <[email protected]> | 2025-08-22 05:35:20 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-08-22 05:35:20 -0500 |
| commit | e26d8dd9b30be7401c82f249ed2f9f4895591c60 (patch) | |
| tree | 270b46f18bc0af1042ba0ad9f8491f7b0bef3214 | |
| parent | 684503fd07eb77955271f6497a8365a89e5a027b (diff) | |
feat(playback): Add right-alignment for user messages
- Refactor the 'printContent' function to align user messages to
the right side of the terminal.
- Gemini messages remain left-aligned.
- This provides a clearer visual distinction in the conversation flow.
| -rw-r--r-- | prettyFormat.go | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/prettyFormat.go b/prettyFormat.go index 905aca5..3476dbc 100644 --- a/prettyFormat.go +++ b/prettyFormat.go @@ -56,14 +56,21 @@ func prettyFormatChat(chat *chatpb.Chat) { // printContent handles the wrapping for the main conversational text. func printContent(author, timestamp, content string) { + // Right-align USER messages, left-align GEMINI messages. + if author == "USER" { + printRightAligned(author, timestamp, content) + } else { + printLeftAligned(author, timestamp, content) + } +} + +func printLeftAligned(author, timestamp, content string) { prefix := fmt.Sprintf("✦ %s (%s):", author, timestamp) fmt.Println(prefix) indent := "\t" - // The available width for text on each line. contentWidth := termWidth - 8 // 8 spaces for a standard tab - // Process each paragraph separately to preserve empty lines between them. paragraphs := strings.Split(content, "\n") for _, paragraph := range paragraphs { @@ -73,25 +80,62 @@ func printContent(author, timestamp, content string) { continue } - // Start the first line of the paragraph. currentLine := indent + words[0] - for _, word := range words[1:] { - // If the next word fits, add it. if len(currentLine)+1+len(word) <= contentWidth { currentLine += " " + word } else { - // The word doesn't fit, so print the completed line. fmt.Println(currentLine) - // Start a new line with the word that didn't fit. currentLine = indent + word } } - // Print the last remaining line of the paragraph. fmt.Println(currentLine) } } +func printRightAligned(author, timestamp, content string) { + prefix := fmt.Sprintf(":(%s) %s ✦", timestamp, author) + + // The available width for the text. + contentWidth := termWidth - 8 // Leave a tab's worth of margin on the left + + lines := strings.Split(content, "\n") + var formattedLines []string + + // First, wrap all the text into lines of the correct width. + for _, line := range lines { + words := strings.Fields(line) + if len(words) == 0 { + formattedLines = append(formattedLines, "") + continue + } + currentLine := words[0] + for _, word := range words[1:] { + if len(currentLine)+1+len(word) <= contentWidth { + currentLine += " " + word + } else { + formattedLines = append(formattedLines, currentLine) + currentLine = word + } + } + formattedLines = append(formattedLines, currentLine) + } + + // Now, print the formatted lines with the correct right alignment. + // The prefix is printed last and right-aligned. + for i, line := range formattedLines { + padding := termWidth - len(line) + if i == len(formattedLines)-1 { // Last line gets the prefix + padding = termWidth - len(line) - len(prefix) + } + if padding < 0 { + padding = 0 + } + fmt.Printf("%s%s\n", strings.Repeat(" ", padding), line) + } + fmt.Printf("%*s\n", termWidth, prefix) +} + func printTable(table *chatpb.Table) { if table == nil || len(table.GetRows()) == 0 { return |
