fix(playback): Correct right-alignment of user messages
- Refactor the 'printRightAligned' function to print the author prefix line *before* the content. - This fixes a bug where the prefix was incorrectly appearing after the message body.
This commit is contained in:
parent
7b65b5b237
commit
9b71be25d0
|
@ -94,46 +94,36 @@ func printLeftAligned(author, timestamp, content string) {
|
|||
}
|
||||
|
||||
func printRightAligned(author, timestamp, content string) {
|
||||
prefix := fmt.Sprintf(":(%s) %s ✦", timestamp, author)
|
||||
prefix := fmt.Sprintf(":(%%s) %%s ✦", timestamp, author)
|
||||
|
||||
// Print the prefix first, right-aligned.
|
||||
fmt.Printf("%*s\n", termWidth, prefix)
|
||||
|
||||
// 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
|
||||
paragraphs := strings.Split(content, "\n")
|
||||
|
||||
// First, wrap all the text into lines of the correct width.
|
||||
for _, line := range lines {
|
||||
words := strings.Fields(line)
|
||||
for _, paragraph := range paragraphs {
|
||||
words := strings.Fields(paragraph)
|
||||
if len(words) == 0 {
|
||||
formattedLines = append(formattedLines, "")
|
||||
fmt.Println() // Preserve paragraph breaks
|
||||
continue
|
||||
}
|
||||
|
||||
currentLine := words[0]
|
||||
for _, word := range words[1:] {
|
||||
if len(currentLine)+1+len(word) <= contentWidth {
|
||||
currentLine += " " + word
|
||||
} else {
|
||||
formattedLines = append(formattedLines, currentLine)
|
||||
// Print the completed line, right-aligned.
|
||||
fmt.Printf("%*s\n", termWidth, currentLine)
|
||||
currentLine = word
|
||||
}
|
||||
}
|
||||
formattedLines = append(formattedLines, currentLine)
|
||||
// Print the last remaining line of the paragraph, right-aligned.
|
||||
fmt.Printf("%*s\n", termWidth, 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) {
|
||||
|
|
Loading…
Reference in New Issue