|
| 1 | +package editor |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "charm.land/lipgloss/v2" |
| 7 | + "github.com/mattn/go-runewidth" |
| 8 | + |
| 9 | + "github.com/docker/cagent/pkg/tui/styles" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + bannerSeparatorText = " • " |
| 14 | + // BorderLeft + PaddingLeft from banner style |
| 15 | + bannerContentOffset = 2 |
| 16 | +) |
| 17 | + |
| 18 | +type attachmentBanner struct { |
| 19 | + items []bannerItem |
| 20 | + height int // cached height after last render |
| 21 | + regions []bannerRegion |
| 22 | +} |
| 23 | + |
| 24 | +type bannerItem struct { |
| 25 | + label string |
| 26 | + placeholder string |
| 27 | +} |
| 28 | + |
| 29 | +type bannerRegion struct { |
| 30 | + start int |
| 31 | + end int |
| 32 | + item bannerItem |
| 33 | +} |
| 34 | + |
| 35 | +func newAttachmentBanner() *attachmentBanner { |
| 36 | + return &attachmentBanner{} |
| 37 | +} |
| 38 | + |
| 39 | +func (b *attachmentBanner) SetItems(items []bannerItem) { |
| 40 | + b.items = items |
| 41 | + b.updateHeight() |
| 42 | +} |
| 43 | + |
| 44 | +// Height returns the number of lines this banner will take when rendered. |
| 45 | +func (b *attachmentBanner) Height() int { |
| 46 | + return b.height |
| 47 | +} |
| 48 | + |
| 49 | +// updateHeight recalculates the banner height based on current state. |
| 50 | +func (b *attachmentBanner) updateHeight() { |
| 51 | + if len(b.items) == 0 { |
| 52 | + b.height = 0 |
| 53 | + return |
| 54 | + } |
| 55 | + // Banner takes 1 line when visible |
| 56 | + b.height = 1 |
| 57 | +} |
| 58 | + |
| 59 | +func (b *attachmentBanner) View() string { |
| 60 | + if len(b.items) == 0 { |
| 61 | + return "" |
| 62 | + } |
| 63 | + |
| 64 | + // Build pill-style badges for each attachment |
| 65 | + var pills []string |
| 66 | + for _, item := range b.items { |
| 67 | + name, size := parseLabel(item.label) |
| 68 | + |
| 69 | + // Create a nice pill: icon + name + size |
| 70 | + pill := styles.AttachmentIconStyle.Render("📎 ") + |
| 71 | + styles.AttachmentBadgeStyle.Render(name) |
| 72 | + if size != "" { |
| 73 | + pill += " " + styles.AttachmentSizeStyle.Render(size) |
| 74 | + } |
| 75 | + pills = append(pills, pill) |
| 76 | + } |
| 77 | + |
| 78 | + separator := styles.MutedStyle.Render(bannerSeparatorText) |
| 79 | + content := strings.Join(pills, separator) |
| 80 | + |
| 81 | + b.buildRegions(pills, separator) |
| 82 | + |
| 83 | + // Wrap in banner style with subtle left border |
| 84 | + banner := lipgloss.NewStyle(). |
| 85 | + BorderLeft(true). |
| 86 | + BorderStyle(lipgloss.ThickBorder()). |
| 87 | + BorderForeground(styles.Info). |
| 88 | + PaddingLeft(1). |
| 89 | + PaddingRight(1). |
| 90 | + Foreground(styles.TextSecondary). |
| 91 | + Render(content) |
| 92 | + |
| 93 | + return styles.AttachmentBannerStyle.Render(banner) |
| 94 | +} |
| 95 | + |
| 96 | +func (b *attachmentBanner) buildRegions(pills []string, separator string) { |
| 97 | + b.regions = b.regions[:0] |
| 98 | + if len(pills) == 0 { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + pos := 0 |
| 103 | + sepWidth := runewidth.StringWidth(stripANSI(separator)) |
| 104 | + |
| 105 | + for i, pill := range pills { |
| 106 | + if i > 0 { |
| 107 | + pos += sepWidth |
| 108 | + } |
| 109 | + width := runewidth.StringWidth(stripANSI(pill)) |
| 110 | + b.regions = append(b.regions, bannerRegion{ |
| 111 | + start: pos, |
| 112 | + end: pos + width, |
| 113 | + item: b.items[i], |
| 114 | + }) |
| 115 | + pos += width |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func (b *attachmentBanner) HitTest(x int) (bannerItem, bool) { |
| 120 | + if len(b.regions) == 0 { |
| 121 | + return bannerItem{}, false |
| 122 | + } |
| 123 | + |
| 124 | + rel := x - bannerContentOffset |
| 125 | + if rel < 0 { |
| 126 | + return bannerItem{}, false |
| 127 | + } |
| 128 | + |
| 129 | + for _, region := range b.regions { |
| 130 | + if rel >= region.start && rel < region.end { |
| 131 | + return region.item, true |
| 132 | + } |
| 133 | + } |
| 134 | + return bannerItem{}, false |
| 135 | +} |
| 136 | + |
| 137 | +// parseLabel splits a label like "paste-1 (21.1 KB)" into name and size parts. |
| 138 | +func parseLabel(label string) (name, size string) { |
| 139 | + // Find the last opening parenthesis for size |
| 140 | + idx := strings.LastIndex(label, " (") |
| 141 | + if idx > 0 && strings.HasSuffix(label, ")") { |
| 142 | + return label[:idx], label[idx+1:] |
| 143 | + } |
| 144 | + return label, "" |
| 145 | +} |
0 commit comments