on main help sign in
fix(messages): properly measure width when rendering attachment paths
commit83d9cc342746eee815672345b5fe6a05cbaca1c6 authorChristian Rocha <christian@rocha.is> dateSun 20 Jul 2025, 00:03 (1 year ago) parentc37fe53
1 file changed+18−9
internal/tui/components/chat/messages/messages.go+18−9
internal/tui/components/chat/messages/messages.go+18−9
@@ -207,18 +207,27 @@ func (m *messageCmp) renderUserMessage() string {
207207
208208 attachments := make([]string, len(m.message.BinaryContent()))
209209 for i, attachment := range m.message.BinaryContent() {
210− file := filepath.Base(attachment.Path)
211− var filename string
212− runes := []rune(file)
213−
214− const truncatePathAt = 7
215− if len(runes) > truncatePathAt {
216− filename = fmt.Sprintf(" %s %s... ", styles.DocumentIcon, string(runes[0:truncatePathAt]))
210+ filename := filepath.Base(attachment.Path)
211+ var displayFilename string
212+ runes := []rune(filename)
213+
214+ const truncateAtChars = 7
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 ",
222+ styles.DocumentIcon,
223+ string(runes[0:truncateAtChars]),
224+ truncateSuffix,
225+ )
217226 } else {
218− filename = fmt.Sprintf(" %s %s ", styles.DocumentIcon, file)
227+ displayFilename = fmt.Sprintf(" %s %s ", styles.DocumentIcon, filename)
219228 }
220229
221− attachments[i] = attachmentStyles.Render(filename)
230+ attachments[i] = attachmentStyles.Render(displayFilename)
222231 }
223232
224233 if len(attachments) > 0 {