diff --git a/pkg/cli/cmd.go b/pkg/cli/cmd.go index 427a192..9116010 100644 --- a/pkg/cli/cmd.go +++ b/pkg/cli/cmd.go @@ -205,11 +205,7 @@ var viewCmd = &cobra.Command{ Fatal("Could not retrieve messages for conversation: %s\n", conversation.Title) } - l := len(messages) - for i, message := range messages { - message.RenderTTY(i < l-1) - } - fmt.Println() + RenderConversation(messages, false) }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { compMode := cobra.ShellCompDirectiveNoFileComp @@ -260,15 +256,13 @@ var replyCmd = &cobra.Command{ } messages = append(messages, userReply) - for _, message := range messages { - message.RenderTTY(true) - } + RenderConversation(messages, true) assistantReply := Message{ ConversationID: conversation.ID, Role: "assistant", } - assistantReply.RenderTTY(false) + assistantReply.RenderTTY() receiver := make(chan string) response := make(chan string) @@ -340,15 +334,12 @@ var newCmd = &cobra.Command{ } } - for _, message := range messages { - message.RenderTTY(true) - } - + RenderConversation(messages, true) reply := Message{ ConversationID: conversation.ID, Role: "assistant", } - reply.RenderTTY(false) + reply.RenderTTY() receiver := make(chan string) response := make(chan string) diff --git a/pkg/cli/tty.go b/pkg/cli/tty.go index 220a09d..bb00174 100644 --- a/pkg/cli/tty.go +++ b/pkg/cli/tty.go @@ -60,7 +60,21 @@ func HandleDelayedResponse(response chan string) string { return sb.String() } -func (m *Message) RenderTTY(paddingDown bool) { +// RenderConversation renders the given messages, with optional space for a +// subsequent message. spaceForResponse controls how many newlines are printed +// after the final message (1 newline if false, 2 if true) +func RenderConversation(messages []Message, spaceForResponse bool) { + l := len(messages) + for i, message := range messages { + message.RenderTTY() + if i < l-1 || spaceForResponse { + // print an additional space before the next message + fmt.Println() + } + } +} + +func (m *Message) RenderTTY() { var messageAge string if m.CreatedAt.IsZero() { messageAge = "now" @@ -89,9 +103,6 @@ func (m *Message) RenderTTY(paddingDown bool) { fmt.Printf("%s %s - %s %s\n\n", separator, role, timestamp, separator) if m.OriginalContent != "" { - fmt.Print(m.OriginalContent) - } - if paddingDown { - fmt.Print("\n\n") + fmt.Println(m.OriginalContent) } }