From cd170de6777c517920abf7894c9dc2379fb12e5a Mon Sep 17 00:00:00 2001 From: Castor Gemini Date: Sun, 24 Aug 2025 08:39:59 -0500 Subject: [PATCH] feat(playback): Improve formatting of long playback --- doPlayback.go | 62 +++++++++++++++++++++++++++++++++++++---------- terminal_width.go | 16 ++++++++++++ 2 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 terminal_width.go diff --git a/doPlayback.go b/doPlayback.go index 34ed0fd..0a3f44b 100644 --- a/doPlayback.go +++ b/doPlayback.go @@ -59,31 +59,67 @@ func listChats(chats *chatpb.Chats) { // print out one line for each chat entry func listEntries(chat *chatpb.Chat) { - fmt.Printf("\n--- Entries for Topic: %s ---\n", chat.GetChatName()) - for i, entry := range chat.GetEntries() { + fmt.Printf("--- Entries for Topic: %s ---\n", chat.GetChatName()) + width := getTerminalWidth() + + // Determine the maximum length of the author and time string + maxAuthorAndTimeLen := 0 + for _, entry := range chat.GetEntries() { author := entry.GetFrom().String() var formattedTime string if ctime := entry.GetCtime(); ctime != nil { t := ctime.AsTime() - formattedTime = t.Format("15:04:05") // Just the time for entry summary + formattedTime = t.Format("2006-01-02 15:04:05") + } else { + formattedTime = "No Time" + } + authorAndTime := fmt.Sprintf("[%s] (%s)", author, formattedTime) + if len(authorAndTime) > maxAuthorAndTimeLen { + maxAuthorAndTimeLen = len(authorAndTime) + } + } + + for _, entry := range chat.GetEntries() { + author := entry.GetFrom().String() + var formattedTime string + if ctime := entry.GetCtime(); ctime != nil { + t := ctime.AsTime() + formattedTime = t.Format("2006-01-02 15:04:05") } else { formattedTime = "No Time" } // Create a short preview of the content contentPreview := strings.TrimSpace(entry.GetContent()) - if len(contentPreview) > 60 { - contentPreview = contentPreview[:57] + "..." - } // Replace newlines with spaces for a clean one-line view contentPreview = strings.ReplaceAll(contentPreview, "\n", " ") - fmt.Printf(" %2d. [%s] (%s): %s\n", - i+1, - author, - formattedTime, - contentPreview, - ) + authorAndTime := fmt.Sprintf("[%s] (%s)", author, formattedTime) + + availableWidth := width - maxAuthorAndTimeLen - 1 // -1 for a space + if len(contentPreview) > availableWidth { + contentPreview = contentPreview[:availableWidth-3] + "..." + } + + if author == "USER" { + // Calculate padding to fill the space between content and the right-aligned author/time + padding := width - len(contentPreview) - maxAuthorAndTimeLen + if padding < 0 { + padding = 0 + } + // Calculate padding to align the author/time string itself + authorAndTimePadding := maxAuthorAndTimeLen - len(authorAndTime) + if authorAndTimePadding < 0 { + authorAndTimePadding = 0 + } + fmt.Printf("%s%s%s%s\n", contentPreview, strings.Repeat(" ", padding), strings.Repeat(" ", authorAndTimePadding), authorAndTime) + } else { + padding := maxAuthorAndTimeLen - len(authorAndTime) + if padding < 0 { + padding = 0 + } + fmt.Printf("%s%s %s\n", authorAndTime, strings.Repeat(" ", padding), contentPreview) + } } fmt.Println("-------------------------------------------------") -} +} \ No newline at end of file diff --git a/terminal_width.go b/terminal_width.go new file mode 100644 index 0000000..3421330 --- /dev/null +++ b/terminal_width.go @@ -0,0 +1,16 @@ +package main + +import ( + "os" + + "golang.org/x/term" +) + +func getTerminalWidth() int { + width, _, err := term.GetSize(int(os.Stdout.Fd())) + if err != nil { + // Return a default width if there's an error + return 80 + } + return width +} \ No newline at end of file