on main help sign in
fix(messages): properly measure width when rendering attachment paths
83d9cc3Christian Rocha
internal/tui/components/chat/messages/messages.go+18−9
@@ -207,18 +207,27 @@ func (m *messageCmp) renderUserMessage() string {
207
207
208 attachments := make([]string, len(m.message.BinaryContent()))
208 attachments := make([]string, len(m.message.BinaryContent()))
209 for i, attachment := range m.message.BinaryContent() {
209 for i, attachment := range m.message.BinaryContent() {
210− file := filepath.Base(attachment.Path)
210+ filename := filepath.Base(attachment.Path)
211− var filename string
211+ var displayFilename string
212− runes := []rune(file)
212+ runes := []rune(filename)
213−
213+
214− const truncatePathAt = 7
214+ const truncateAtChars = 7
215− if len(runes) > truncatePathAt {
215+ const truncateSuffix = "..."
216+
217+ // If the filename is too long, truncate it to fit within the maximum
218+ // width we’ve chosen.
219+ if lipgloss.Width(filename) > truncateAtChars+lipgloss.Width(truncateSuffix) {
220+ displayFilename = fmt.Sprintf(
221+ " %s %s%s ",
216− filename = fmt.Sprintf(" %s %s... ", styles.DocumentIcon, string(runes[0:truncatePathAt]))
222+ styles.DocumentIcon,
223+ string(runes[0:truncateAtChars]),
224+ truncateSuffix,
225+ )
217 } else {
226 } else {
218− filename = fmt.Sprintf(" %s %s ", styles.DocumentIcon, file)
227+ displayFilename = fmt.Sprintf(" %s %s ", styles.DocumentIcon, filename)
219 }
228 }
220
229
221− attachments[i] = attachmentStyles.Render(filename)
230+ attachments[i] = attachmentStyles.Render(displayFilename)
222 }
231 }
223
232
224 if len(attachments) > 0 {
233 if len(attachments) > 0 {