on main help sign in
chore(messages): pre-allocate user message attachment slice
16cf0bdChristian Rocha
internal/tui/components/chat/messages/messages.go+7−3
@@ -193,17 +193,19 @@ func (m *messageCmp) renderAssistantMessage() string {
193 return m.style().Render(joined)
193 return m.style().Render(joined)
194}
194}
195
195
196−// renderUserMessage renders user messages with file attachments.
196+// renderUserMessage renders user messages with file attachments. It displays
197−// Displays message content and any attached files with appropriate icons.
197+// message content and any attached files with appropriate icons.
198func (m *messageCmp) renderUserMessage() string {
198func (m *messageCmp) renderUserMessage() string {
199 t := styles.CurrentTheme()
199 t := styles.CurrentTheme()
200 parts := []string{
200 parts := []string{
201 m.toMarkdown(m.message.Content().String()),
201 m.toMarkdown(m.message.Content().String()),
202 }
202 }
203+
203 attachmentStyles := t.S().Text.
204 attachmentStyles := t.S().Text.
204 MarginLeft(1).
205 MarginLeft(1).
205 Background(t.BgSubtle)
206 Background(t.BgSubtle)
207+
206− attachments := []string{}
208+ attachments := make([]string, len(m.message.BinaryContent()))
207 for _, attachment := range m.message.BinaryContent() {
209 for _, attachment := range m.message.BinaryContent() {
208 file := filepath.Base(attachment.Path)
210 file := filepath.Base(attachment.Path)
209 var filename string
211 var filename string
@@ -214,9 +216,11 @@ func (m *messageCmp) renderUserMessage() string {
214 }
216 }
215 attachments = append(attachments, attachmentStyles.Render(filename))
217 attachments = append(attachments, attachmentStyles.Render(filename))
216 }
218 }
219+
217 if len(attachments) > 0 {
220 if len(attachments) > 0 {
218 parts = append(parts, "", strings.Join(attachments, ""))
221 parts = append(parts, "", strings.Join(attachments, ""))
219 }
222 }
223+
220 joined := lipgloss.JoinVertical(lipgloss.Left, parts...)
224 joined := lipgloss.JoinVertical(lipgloss.Left, parts...)
221 return m.style().Render(joined)
225 return m.style().Render(joined)
222}
226}