Add RenderConversation to split out common message rendering logic

This commit is contained in:
Matt Low 2023-11-18 15:57:03 +00:00
parent 965043c908
commit 6426b04e2c
2 changed files with 21 additions and 19 deletions

View File

@ -205,11 +205,7 @@ var viewCmd = &cobra.Command{
Fatal("Could not retrieve messages for conversation: %s\n", conversation.Title) Fatal("Could not retrieve messages for conversation: %s\n", conversation.Title)
} }
l := len(messages) RenderConversation(messages, false)
for i, message := range messages {
message.RenderTTY(i < l-1)
}
fmt.Println()
}, },
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
compMode := cobra.ShellCompDirectiveNoFileComp compMode := cobra.ShellCompDirectiveNoFileComp
@ -260,15 +256,13 @@ var replyCmd = &cobra.Command{
} }
messages = append(messages, userReply) messages = append(messages, userReply)
for _, message := range messages {
message.RenderTTY(true)
}
RenderConversation(messages, true)
assistantReply := Message{ assistantReply := Message{
ConversationID: conversation.ID, ConversationID: conversation.ID,
Role: "assistant", Role: "assistant",
} }
assistantReply.RenderTTY(false) assistantReply.RenderTTY()
receiver := make(chan string) receiver := make(chan string)
response := make(chan string) response := make(chan string)
@ -340,15 +334,12 @@ var newCmd = &cobra.Command{
} }
} }
for _, message := range messages { RenderConversation(messages, true)
message.RenderTTY(true)
}
reply := Message{ reply := Message{
ConversationID: conversation.ID, ConversationID: conversation.ID,
Role: "assistant", Role: "assistant",
} }
reply.RenderTTY(false) reply.RenderTTY()
receiver := make(chan string) receiver := make(chan string)
response := make(chan string) response := make(chan string)

View File

@ -60,7 +60,21 @@ func HandleDelayedResponse(response chan string) string {
return sb.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 var messageAge string
if m.CreatedAt.IsZero() { if m.CreatedAt.IsZero() {
messageAge = "now" messageAge = "now"
@ -89,9 +103,6 @@ func (m *Message) RenderTTY(paddingDown bool) {
fmt.Printf("%s %s - %s %s\n\n", separator, role, timestamp, separator) fmt.Printf("%s %s - %s %s\n\n", separator, role, timestamp, separator)
if m.OriginalContent != "" { if m.OriginalContent != "" {
fmt.Print(m.OriginalContent) fmt.Println(m.OriginalContent)
}
if paddingDown {
fmt.Print("\n\n")
} }
} }