on main help sign in
Merge pull request #1938 from charmbracelet/ui-hi-copy
49c37c6Ayman Bagabas
internal/ui/chat/assistant.go+13−8
@@ -77,13 +77,9 @@ func (a *AssistantMessageItem) ID() string {
77 return a.message.ID
77 return a.message.ID
78}
78}
79
79
80−// Render implements MessageItem.
80+// RawRender implements [MessageItem].
81−func (a *AssistantMessageItem) Render(width int) string {
81+func (a *AssistantMessageItem) RawRender(width int) string {
82 cappedWidth := cappedMessageWidth(width)
82 cappedWidth := cappedMessageWidth(width)
83− style := a.sty.Chat.Message.AssistantBlurred
84− if a.focused {
85− style = a.sty.Chat.Message.AssistantFocused
86− }
87
83
88 var spinner string
84 var spinner string
89 if a.isSpinning() {
85 if a.isSpinning() {
@@ -103,10 +99,19 @@ func (a *AssistantMessageItem) Render(width int) string {
103 if highlightedContent != "" {
99 if highlightedContent != "" {
104 highlightedContent += "\n\n"
100 highlightedContent += "\n\n"
105 }
101 }
106− return style.Render(highlightedContent + spinner)
102+ return highlightedContent + spinner
107 }
103 }
108
104
109− return style.Render(highlightedContent)
105+ return highlightedContent
106+}
107+
108+// Render implements MessageItem.
109+func (a *AssistantMessageItem) Render(width int) string {
110+ style := a.sty.Chat.Message.AssistantBlurred
111+ if a.focused {
112+ style = a.sty.Chat.Message.AssistantFocused
113+ }
114+ return style.Render(a.RawRender(width))
110}
115}
111
116
112// renderMessageContent renders the message content including thinking, main content, and finish reason.
117// renderMessageContent renders the message content including thinking, main content, and finish reason.
internal/ui/chat/messages.go+17−8
@@ -1,6 +1,3 @@
1−// Package chat provides UI components for displaying and managing chat messages.
2−// It defines message item types that can be rendered in a list view, including
3−// support for highlighting, focusing, and caching rendered content.
4package chat
1package chat
5
2
6import (
3import (
@@ -48,6 +45,7 @@ type Expandable interface {
48// UI and be part of a [list.List] identifiable by a unique ID.
45// UI and be part of a [list.List] identifiable by a unique ID.
49type MessageItem interface {
46type MessageItem interface {
50 list.Item
47 list.Item
48+ list.RawRenderable
51 Identifiable
49 Identifiable
52}
50}
53
51
@@ -77,6 +75,8 @@ type highlightableMessageItem struct {
77 highlighter list.Highlighter
75 highlighter list.Highlighter
78}
76}
79
77
78+var _ list.Highlightable = (*highlightableMessageItem)(nil)
79+
80// isHighlighted returns true if the item has a highlight range set.
80// isHighlighted returns true if the item has a highlight range set.
81func (h *highlightableMessageItem) isHighlighted() bool {
81func (h *highlightableMessageItem) isHighlighted() bool {
82 return h.startLine != -1 || h.endLine != -1
82 return h.startLine != -1 || h.endLine != -1
@@ -91,8 +91,8 @@ func (h *highlightableMessageItem) renderHighlighted(content string, width, heig
91 return list.Highlight(content, area, h.startLine, h.startCol, h.endLine, h.endCol, h.highlighter)
91 return list.Highlight(content, area, h.startLine, h.startCol, h.endLine, h.endCol, h.highlighter)
92}
92}
93
93
94−// Highlight implements MessageItem.
94+// SetHighlight implements list.Highlightable.
95−func (h *highlightableMessageItem) Highlight(startLine int, startCol int, endLine int, endCol int) {
95+func (h *highlightableMessageItem) SetHighlight(startLine int, startCol int, endLine int, endCol int) {
96 // Adjust columns for the style's left inset (border + padding) since we
96 // Adjust columns for the style's left inset (border + padding) since we
97 // highlight the content only.
97 // highlight the content only.
98 offset := messageLeftPaddingTotal
98 offset := messageLeftPaddingTotal
@@ -106,6 +106,11 @@ func (h *highlightableMessageItem) Highlight(startLine int, startCol int, endLin
106 }
106 }
107}
107}
108
108
109+// Highlight implements list.Highlightable.
110+func (h *highlightableMessageItem) Highlight() (startLine int, startCol int, endLine int, endCol int) {
111+ return h.startLine, h.startCol, h.endLine, h.endCol
112+}
113+
109func defaultHighlighter(sty *styles.Styles) *highlightableMessageItem {
114func defaultHighlighter(sty *styles.Styles) *highlightableMessageItem {
110 return &highlightableMessageItem{
115 return &highlightableMessageItem{
111 startLine: -1,
116 startLine: -1,
@@ -193,8 +198,8 @@ func (a *AssistantInfoItem) ID() string {
193 return a.id
198 return a.id
194}
199}
195
200
196−// Render implements MessageItem.
201+// RawRender implements MessageItem.
197−func (a *AssistantInfoItem) Render(width int) string {
202+func (a *AssistantInfoItem) RawRender(width int) string {
198 innerWidth := max(0, width-messageLeftPaddingTotal)
203 innerWidth := max(0, width-messageLeftPaddingTotal)
199 content, _, ok := a.getCachedRender(innerWidth)
204 content, _, ok := a.getCachedRender(innerWidth)
200 if !ok {
205 if !ok {
@@ -202,8 +207,12 @@ func (a *AssistantInfoItem) Render(width int) string {
202 height := lipgloss.Height(content)
207 height := lipgloss.Height(content)
203 a.setCachedRender(content, innerWidth, height)
208 a.setCachedRender(content, innerWidth, height)
204 }
209 }
210+ return content
211+}
205
212
213+// Render implements MessageItem.
214+func (a *AssistantInfoItem) Render(width int) string {
206− return a.sty.Chat.Message.SectionHeader.Render(content)
215+ return a.sty.Chat.Message.SectionHeader.Render(a.RawRender(width))
207}
216}
208
217
209func (a *AssistantInfoItem) renderContent(width int) string {
218func (a *AssistantInfoItem) renderContent(width int) string {
internal/ui/chat/tools.go+17−12
@@ -287,20 +287,12 @@ func (t *baseToolMessageItem) Animate(msg anim.StepMsg) tea.Cmd {
287 return t.anim.Animate(msg)
287 return t.anim.Animate(msg)
288}
288}
289
289
290−// Render renders the tool message item at the given width.
290+// RawRender implements [MessageItem].
291−func (t *baseToolMessageItem) Render(width int) string {
291+func (t *baseToolMessageItem) RawRender(width int) string {
292 toolItemWidth := width - messageLeftPaddingTotal
292 toolItemWidth := width - messageLeftPaddingTotal
293 if t.hasCappedWidth {
293 if t.hasCappedWidth {
294 toolItemWidth = cappedMessageWidth(width)
294 toolItemWidth = cappedMessageWidth(width)
295 }
295 }
296− style := t.sty.Chat.Message.ToolCallBlurred
297− if t.focused {
298− style = t.sty.Chat.Message.ToolCallFocused
299− }
300−
301− if t.isCompact {
302− style = t.sty.Chat.Message.ToolCallCompact
303− }
304
296
305 content, height, ok := t.getCachedRender(toolItemWidth)
297 content, height, ok := t.getCachedRender(toolItemWidth)
306 // if we are spinning or there is no cache rerender
298 // if we are spinning or there is no cache rerender
@@ -319,8 +311,21 @@ func (t *baseToolMessageItem) Render(width int) string {
319 t.setCachedRender(content, toolItemWidth, height)
311 t.setCachedRender(content, toolItemWidth, height)
320 }
312 }
321
313
322− highlightedContent := t.renderHighlighted(content, toolItemWidth, height)
314+ return t.renderHighlighted(content, toolItemWidth, height)
315+}
316+
317+// Render renders the tool message item at the given width.
318+func (t *baseToolMessageItem) Render(width int) string {
319+ style := t.sty.Chat.Message.ToolCallBlurred
320+ if t.focused {
321+ style = t.sty.Chat.Message.ToolCallFocused
322+ }
323+
324+ if t.isCompact {
325+ style = t.sty.Chat.Message.ToolCallCompact
326+ }
327+
323− return style.Render(highlightedContent)
328+ return style.Render(t.RawRender(width))
324}
329}
325
330
326// ToolCall returns the tool call associated with this message item.
331// ToolCall returns the tool call associated with this message item.
internal/ui/chat/user.go+13−9
@@ -33,19 +33,14 @@ func NewUserMessageItem(sty *styles.Styles, message *message.Message, attachment
33 }
33 }
34}
34}
35
35
36−// Render implements MessageItem.
36+// RawRender implements [MessageItem].
37−func (m *UserMessageItem) Render(width int) string {
37+func (m *UserMessageItem) RawRender(width int) string {
38 cappedWidth := cappedMessageWidth(width)
38 cappedWidth := cappedMessageWidth(width)
39
39
40− style := m.sty.Chat.Message.UserBlurred
41− if m.focused {
42− style = m.sty.Chat.Message.UserFocused
43− }
44−
45 content, height, ok := m.getCachedRender(cappedWidth)
40 content, height, ok := m.getCachedRender(cappedWidth)
46 // cache hit
41 // cache hit
47 if ok {
42 if ok {
48− return style.Render(m.renderHighlighted(content, cappedWidth, height))
43+ return m.renderHighlighted(content, cappedWidth, height)
49 }
44 }
50
45
51 renderer := common.MarkdownRenderer(m.sty, cappedWidth)
46 renderer := common.MarkdownRenderer(m.sty, cappedWidth)
@@ -69,7 +64,16 @@ func (m *UserMessageItem) Render(width int) string {
69
64
70 height = lipgloss.Height(content)
65 height = lipgloss.Height(content)
71 m.setCachedRender(content, cappedWidth, height)
66 m.setCachedRender(content, cappedWidth, height)
72− return style.Render(m.renderHighlighted(content, cappedWidth, height))
67+ return m.renderHighlighted(content, cappedWidth, height)
68+}
69+
70+// Render implements MessageItem.
71+func (m *UserMessageItem) Render(width int) string {
72+ style := m.sty.Chat.Message.UserBlurred
73+ if m.focused {
74+ style = m.sty.Chat.Message.UserFocused
75+ }
76+ return style.Render(m.RawRender(width))
73}
77}
74
78
75// ID implements MessageItem.
79// ID implements MessageItem.
internal/ui/dialog/models_list.go+1
@@ -26,6 +26,7 @@ func NewModelsList(sty *styles.Styles, groups ...ModelGroup) *ModelsList {
26 groups: groups,
26 groups: groups,
27 t: sty,
27 t: sty,
28 }
28 }
29+ f.RegisterRenderCallback(list.FocusedRenderCallback(f.List))
29 return f
30 return f
30}
31}
31
32
internal/ui/list/filterable.go+1
@@ -31,6 +31,7 @@ func NewFilterableList(items ...FilterableItem) *FilterableList {
31 List: NewList(),
31 List: NewList(),
32 items: items,
32 items: items,
33 }
33 }
34+ f.RegisterRenderCallback(FocusedRenderCallback(f.List))
34 f.SetItems(items...)
35 f.SetItems(items...)
35 return f
36 return f
36}
37}
internal/ui/list/focus.gonew file+13
@@ -0,0 +1,13 @@
1+package list
2+
3+// FocusedRenderCallback is a helper function that returns a render callback
4+// that marks items as focused during rendering.
5+func FocusedRenderCallback(list *List) RenderCallback {
6+ return func(idx, selectedIdx int, item Item) Item {
7+ if focusable, ok := item.(Focusable); ok {
8+ focusable.SetFocused(list.Focused() && idx == selectedIdx)
9+ return focusable.(Item)
10+ }
11+ return item
12+ }
13+}
internal/ui/list/highlight.go+49−9
@@ -2,25 +2,60 @@ package list
2
2
3import (
3import (
4 "image"
4 "image"
5+ "strings"
5
6
6 "charm.land/lipgloss/v2"
7 "charm.land/lipgloss/v2"
7 uv "github.com/charmbracelet/ultraviolet"
8 uv "github.com/charmbracelet/ultraviolet"
8)
9)
9
10
10// DefaultHighlighter is the default highlighter function that applies inverse style.
11// DefaultHighlighter is the default highlighter function that applies inverse style.
11−var DefaultHighlighter Highlighter = func(s uv.Style) uv.Style {
12+var DefaultHighlighter Highlighter = func(x, y int, c *uv.Cell) *uv.Cell {
13+ if c == nil {
14+ return c
15+ }
12− s.Attrs |= uv.AttrReverse
16+ c.Style.Attrs |= uv.AttrReverse
13− return s
17+ return c
14}
18}
15
19
16// Highlighter represents a function that defines how to highlight text.
20// Highlighter represents a function that defines how to highlight text.
17−type Highlighter func(uv.Style) uv.Style
21+type Highlighter func(x, y int, c *uv.Cell) *uv.Cell
22+
23+// HighlightContent returns the content with highlighted regions based on the specified parameters.
24+func HighlightContent(content string, area image.Rectangle, startLine, startCol, endLine, endCol int) string {
25+ var sb strings.Builder
26+ pos := image.Pt(-1, -1)
27+ HighlightBuffer(content, area, startLine, startCol, endLine, endCol, func(x, y int, c *uv.Cell) *uv.Cell {
28+ pos.X = x
29+ if pos.Y == -1 {
30+ pos.Y = y
31+ } else if y > pos.Y {
32+ sb.WriteString(strings.Repeat("\n", y-pos.Y))
33+ pos.Y = y
34+ }
35+ sb.WriteString(c.Content)
36+ return c
37+ })
38+ if sb.Len() > 0 {
39+ sb.WriteString("\n")
40+ }
41+ return sb.String()
42+}
18
43
19// Highlight highlights a region of text within the given content and region.
44// Highlight highlights a region of text within the given content and region.
20func Highlight(content string, area image.Rectangle, startLine, startCol, endLine, endCol int, highlighter Highlighter) string {
45func Highlight(content string, area image.Rectangle, startLine, startCol, endLine, endCol int, highlighter Highlighter) string {
21− if startLine < 0 || startCol < 0 {
46+ buf := HighlightBuffer(content, area, startLine, startCol, endLine, endCol, highlighter)
47+ if buf == nil {
22 return content
48 return content
23 }
49 }
50+ return buf.Render()
51+}
52+
53+// HighlightBuffer highlights a region of text within the given content and
54+// region, returning a [uv.ScreenBuffer].
55+func HighlightBuffer(content string, area image.Rectangle, startLine, startCol, endLine, endCol int, highlighter Highlighter) *uv.ScreenBuffer {
56+ if startLine < 0 || startCol < 0 {
57+ return nil
58+ }
24
59
25 if highlighter == nil {
60 if highlighter == nil {
26 highlighter = DefaultHighlighter
61 highlighter = DefaultHighlighter
@@ -87,17 +122,22 @@ func Highlight(content string, area image.Rectangle, startLine, startCol, endLin
87 continue
122 continue
88 }
123 }
89 cell := line.At(x)
124 cell := line.At(x)
125+ if cell != nil {
90− cell.Style = highlighter(cell.Style)
126+ line.Set(x, highlighter(x, y, cell))
127+ }
91 }
128 }
92 }
129 }
93
130
94− return buf.Render()
131+ return &buf
95}
132}
96
133
97// ToHighlighter converts a [lipgloss.Style] to a [Highlighter].
134// ToHighlighter converts a [lipgloss.Style] to a [Highlighter].
98func ToHighlighter(lgStyle lipgloss.Style) Highlighter {
135func ToHighlighter(lgStyle lipgloss.Style) Highlighter {
99− return func(uv.Style) uv.Style {
136+ return func(_ int, _ int, c *uv.Cell) *uv.Cell {
137+ if c != nil {
100− return ToStyle(lgStyle)
138+ c.Style = ToStyle(lgStyle)
139+ }
140+ return c
101 }
141 }
102}
142}
103
143
internal/ui/list/item.go+13−3
@@ -13,6 +13,14 @@ type Item interface {
13 Render(width int) string
13 Render(width int) string
14}
14}
15
15
16+// RawRenderable represents an item that can provide a raw rendering
17+// without additional styling.
18+type RawRenderable interface {
19+ // RawRender returns the raw rendered string without any additional
20+ // styling.
21+ RawRender(width int) string
22+}
23+
16// Focusable represents an item that can be aware of focus state changes.
24// Focusable represents an item that can be aware of focus state changes.
17type Focusable interface {
25type Focusable interface {
18 // SetFocused sets the focus state of the item.
26 // SetFocused sets the focus state of the item.
@@ -21,9 +29,11 @@ type Focusable interface {
21
29
22// Highlightable represents an item that can highlight a portion of its content.
30// Highlightable represents an item that can highlight a portion of its content.
23type Highlightable interface {
31type Highlightable interface {
24− // Highlight highlights the content from the given start to end positions.
32+ // SetHighlight highlights the content from the given start to end
25− // Use -1 for no highlight.
33+ // positions. Use -1 for no highlight.
34+ SetHighlight(startLine, startCol, endLine, endCol int)
35+ // Highlight returns the current highlight positions within the item.
26− Highlight(startLine, startCol, endLine, endCol int)
36+ Highlight() (startLine, startCol, endLine, endCol int)
27}
37}
28
38
29// MouseClickable represents an item that can handle mouse click events.
39// MouseClickable represents an item that can handle mouse click events.
internal/ui/list/list.go+15−5
@@ -49,9 +49,13 @@ func NewList(items ...Item) *List {
49 return l
49 return l
50}
50}
51
51
52+// RenderCallback defines a function that can modify an item before it is
53+// rendered.
54+type RenderCallback func(idx, selectedIdx int, item Item) Item
55+
52// RegisterRenderCallback registers a callback to be called when rendering
56// RegisterRenderCallback registers a callback to be called when rendering
53// items. This can be used to modify items before they are rendered.
57// items. This can be used to modify items before they are rendered.
54−func (l *List) RegisterRenderCallback(cb func(idx, selectedIdx int, item Item) Item) {
58+func (l *List) RegisterRenderCallback(cb RenderCallback) {
55 l.renderCallbacks = append(l.renderCallbacks, cb)
59 l.renderCallbacks = append(l.renderCallbacks, cb)
56}
60}
57
61
@@ -66,6 +70,11 @@ func (l *List) SetGap(gap int) {
66 l.gap = gap
70 l.gap = gap
67}
71}
68
72
73+// Gap returns the gap between items.
74+func (l *List) Gap() int {
75+ return l.gap
76+}
77+
69// SetReverse shows the list in reverse order.
78// SetReverse shows the list in reverse order.
70func (l *List) SetReverse(reverse bool) {
79func (l *List) SetReverse(reverse bool) {
71 l.reverse = reverse
80 l.reverse = reverse
@@ -101,10 +110,6 @@ func (l *List) getItem(idx int) renderedItem {
101 }
110 }
102 }
111 }
103
112
104− if focusable, isFocusable := item.(Focusable); isFocusable {
105− focusable.SetFocused(l.focused && idx == l.selectedIdx)
106− }
107−
108 rendered := item.Render(l.width)
113 rendered := item.Render(l.width)
109 rendered = strings.TrimRight(rendered, "\n")
114 rendered = strings.TrimRight(rendered, "\n")
110 height := countLines(rendered)
115 height := countLines(rendered)
@@ -348,6 +353,11 @@ func (l *List) RemoveItem(idx int) {
348 }
353 }
349}
354}
350
355
356+// Focused returns whether the list is focused.
357+func (l *List) Focused() bool {
358+ return l.focused
359+}
360+
351// Focus sets the focus state of the list.
361// Focus sets the focus state of the list.
352func (l *List) Focus() {
362func (l *List) Focus() {
353 l.focused = true
363 l.focused = true
internal/ui/model/chat.go+46−4
@@ -1,7 +1,10 @@
1package model
1package model
2
2
3import (
3import (
4+ "strings"
5+
4 tea "charm.land/bubbletea/v2"
6 tea "charm.land/bubbletea/v2"
7+ "charm.land/lipgloss/v2"
5 "github.com/charmbracelet/crush/internal/ui/anim"
8 "github.com/charmbracelet/crush/internal/ui/anim"
6 "github.com/charmbracelet/crush/internal/ui/chat"
9 "github.com/charmbracelet/crush/internal/ui/chat"
7 "github.com/charmbracelet/crush/internal/ui/common"
10 "github.com/charmbracelet/crush/internal/ui/common"
@@ -43,6 +46,7 @@ func NewChat(com *common.Common) *Chat {
43 l := list.NewList()
46 l := list.NewList()
44 l.SetGap(1)
47 l.SetGap(1)
45 l.RegisterRenderCallback(c.applyHighlightRange)
48 l.RegisterRenderCallback(c.applyHighlightRange)
49+ l.RegisterRenderCallback(list.FocusedRenderCallback(l))
46 c.list = l
50 c.list = l
47 c.mouseDownItem = -1
51 c.mouseDownItem = -1
48 c.mouseDragItem = -1
52 c.mouseDragItem = -1
@@ -445,9 +449,6 @@ func (m *Chat) HandleMouseUp(x, y int) bool {
445 return false
449 return false
446 }
450 }
447
451
448− // TODO: Handle the behavior when mouse is released after a drag selection
449− // (e.g., copy selected text to clipboard)
450−
451 m.mouseDown = false
452 m.mouseDown = false
452 return true
453 return true
453}
454}
@@ -474,6 +475,47 @@ func (m *Chat) HandleMouseDrag(x, y int) bool {
474 return true
475 return true
475}
476}
476
477
478+// HasHighlight returns whether there is currently highlighted content.
479+func (m *Chat) HasHighlight() bool {
480+ startItemIdx, startLine, startCol, endItemIdx, endLine, endCol := m.getHighlightRange()
481+ return startItemIdx >= 0 && endItemIdx >= 0 && (startLine != endLine || startCol != endCol)
482+}
483+
484+// HighlighContent returns the currently highlighted content based on the mouse
485+// selection. It returns an empty string if no content is highlighted.
486+func (m *Chat) HighlighContent() string {
487+ startItemIdx, startLine, startCol, endItemIdx, endLine, endCol := m.getHighlightRange()
488+ if startItemIdx < 0 || endItemIdx < 0 || startLine == endLine && startCol == endCol {
489+ return ""
490+ }
491+
492+ var sb strings.Builder
493+ for i := startItemIdx; i <= endItemIdx; i++ {
494+ item := m.list.ItemAt(i)
495+ if hi, ok := item.(list.Highlightable); ok {
496+ startLine, startCol, endLine, endCol := hi.Highlight()
497+ listWidth := m.list.Width()
498+ var rendered string
499+ if rr, ok := item.(list.RawRenderable); ok {
500+ rendered = rr.RawRender(listWidth)
501+ } else {
502+ rendered = item.Render(listWidth)
503+ }
504+ sb.WriteString(list.HighlightContent(
505+ rendered,
506+ uv.Rect(0, 0, listWidth, lipgloss.Height(rendered)),
507+ startLine,
508+ startCol,
509+ endLine,
510+ endCol,
511+ ))
512+ sb.WriteString(strings.Repeat("\n", m.list.Gap()))
513+ }
514+ }
515+
516+ return strings.TrimSpace(sb.String())
517+}
518+
477// ClearMouse clears the current mouse interaction state.
519// ClearMouse clears the current mouse interaction state.
478func (m *Chat) ClearMouse() {
520func (m *Chat) ClearMouse() {
479 m.mouseDown = false
521 m.mouseDown = false
@@ -515,7 +557,7 @@ func (m *Chat) applyHighlightRange(idx, selectedIdx int, item list.Item) list.It
515 }
557 }
516 }
558 }
517
559
518− hi.Highlight(sLine, sCol, eLine, eCol)
560+ hi.SetHighlight(sLine, sCol, eLine, eCol)
519 return hi.(list.Item)
561 return hi.(list.Item)
520 }
562 }
521
563
internal/ui/model/ui.go+38−2
@@ -23,6 +23,7 @@ import (
23 "charm.land/bubbles/v2/textarea"
23 "charm.land/bubbles/v2/textarea"
24 tea "charm.land/bubbletea/v2"
24 tea "charm.land/bubbletea/v2"
25 "charm.land/lipgloss/v2"
25 "charm.land/lipgloss/v2"
26+ "github.com/atotto/clipboard"
26 "github.com/charmbracelet/catwalk/pkg/catwalk"
27 "github.com/charmbracelet/catwalk/pkg/catwalk"
27 "github.com/charmbracelet/crush/internal/agent/tools/mcp"
28 "github.com/charmbracelet/crush/internal/agent/tools/mcp"
28 "github.com/charmbracelet/crush/internal/app"
29 "github.com/charmbracelet/crush/internal/app"
@@ -106,6 +107,9 @@ type (
106
107
107 // closeDialogMsg is sent to close the current dialog.
108 // closeDialogMsg is sent to close the current dialog.
108 closeDialogMsg struct{}
109 closeDialogMsg struct{}
110+
111+ // copyChatHighlightMsg is sent to copy the current chat highlight to clipboard.
112+ copyChatHighlightMsg struct{}
109)
113)
110
114
111// UI represents the main user interface model.
115// UI represents the main user interface model.
@@ -203,6 +207,9 @@ type UI struct {
203 // Todo spinner
207 // Todo spinner
204 todoSpinner spinner.Model
208 todoSpinner spinner.Model
205 todoIsSpinning bool
209 todoIsSpinning bool
210+
211+ // mouse highlighting related state
212+ lastClickTime time.Time
206}
213}
207
214
208// New creates a new instance of the [UI] model.
215// New creates a new instance of the [UI] model.
@@ -484,6 +491,8 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
484 m.keyMap.Models.SetHelp("ctrl+m", "models")
491 m.keyMap.Models.SetHelp("ctrl+m", "models")
485 m.keyMap.Editor.Newline.SetHelp("shift+enter", "newline")
492 m.keyMap.Editor.Newline.SetHelp("shift+enter", "newline")
486 }
493 }
494+ case copyChatHighlightMsg:
495+ cmds = append(cmds, m.copyChatHighlight())
487 case tea.MouseClickMsg:
496 case tea.MouseClickMsg:
488 switch m.state {
497 switch m.state {
489 case uiChat:
498 case uiChat:
@@ -491,7 +500,9 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
491 // Adjust for chat area position
500 // Adjust for chat area position
492 x -= m.layout.main.Min.X
501 x -= m.layout.main.Min.X
493 y -= m.layout.main.Min.Y
502 y -= m.layout.main.Min.Y
494− m.chat.HandleMouseDown(x, y)
503+ if m.chat.HandleMouseDown(x, y) {
504+ m.lastClickTime = time.Now()
505+ }
495 }
506 }
496
507
497 case tea.MouseMotionMsg:
508 case tea.MouseMotionMsg:
@@ -527,13 +538,22 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
527 }
538 }
528
539
529 case tea.MouseReleaseMsg:
540 case tea.MouseReleaseMsg:
541+ const doubleClickThreshold = 500 * time.Millisecond
542+
530 switch m.state {
543 switch m.state {
531 case uiChat:
544 case uiChat:
532 x, y := msg.X, msg.Y
545 x, y := msg.X, msg.Y
533 // Adjust for chat area position
546 // Adjust for chat area position
534 x -= m.layout.main.Min.X
547 x -= m.layout.main.Min.X
535 y -= m.layout.main.Min.Y
548 y -= m.layout.main.Min.Y
536− m.chat.HandleMouseUp(x, y)
549+ if m.chat.HandleMouseUp(x, y) {
550+ cmds = append(cmds, tea.Tick(doubleClickThreshold, func(t time.Time) tea.Msg {
551+ if time.Since(m.lastClickTime) >= doubleClickThreshold {
552+ return copyChatHighlightMsg{}
553+ }
554+ return nil
555+ }))
556+ }
537 }
557 }
538 case tea.MouseWheelMsg:
558 case tea.MouseWheelMsg:
539 // Pass mouse events to dialogs first if any are open.
559 // Pass mouse events to dialogs first if any are open.
@@ -2844,6 +2864,22 @@ func (m *UI) runMCPPrompt(clientID, promptID string, arguments map[string]string
2844 return tea.Sequence(cmds...)
2864 return tea.Sequence(cmds...)
2845}
2865}
2846
2866
2867+func (m *UI) copyChatHighlight() tea.Cmd {
2868+ text := m.chat.HighlighContent()
2869+ return tea.Sequence(
2870+ tea.SetClipboard(text),
2871+ func() tea.Msg {
2872+ _ = clipboard.WriteAll(text)
2873+ return nil
2874+ },
2875+ func() tea.Msg {
2876+ m.chat.ClearMouse()
2877+ return nil
2878+ },
2879+ uiutil.ReportInfo("Selected text copied to clipboard"),
2880+ )
2881+}
2882+
2847// renderLogo renders the Crush logo with the given styles and dimensions.
2883// renderLogo renders the Crush logo with the given styles and dimensions.
2848func renderLogo(t *styles.Styles, compact bool, width int) string {
2884func renderLogo(t *styles.Styles, compact bool, width int) string {
2849 return logo.Render(version.Version, compact, logo.Opts{
2885 return logo.Render(version.Version, compact, logo.Opts{