| package chat |
|
|
| import ( |
| "context" |
| "strings" |
| "time" |
|
|
| "github.com/atotto/clipboard" |
| "github.com/charmbracelet/bubbles/v2/key" |
| tea "github.com/charmbracelet/bubbletea/v2" |
| "github.com/charmbracelet/crush/internal/app" |
| "github.com/charmbracelet/crush/internal/llm/agent" |
| "github.com/charmbracelet/crush/internal/message" |
| "github.com/charmbracelet/crush/internal/permission" |
| "github.com/charmbracelet/crush/internal/pubsub" |
| "github.com/charmbracelet/crush/internal/session" |
| "github.com/charmbracelet/crush/internal/tui/components/chat/messages" |
| "github.com/charmbracelet/crush/internal/tui/components/core/layout" |
| "github.com/charmbracelet/crush/internal/tui/exp/list" |
| "github.com/charmbracelet/crush/internal/tui/styles" |
| "github.com/charmbracelet/crush/internal/tui/util" |
| ) |
|
|
| type SendMsg struct { |
| Text string |
| Attachments []message.Attachment |
| } |
|
|
| type SessionSelectedMsg = session.Session |
|
|
| type SessionClearedMsg struct{} |
|
|
| type SelectionCopyMsg struct { |
| clickCount int |
| endSelection bool |
| x, y int |
| } |
|
|
| const ( |
| NotFound = -1 |
| ) |
|
|
| |
| |
| type MessageListCmp interface { |
| util.Model |
| layout.Sizeable |
| layout.Focusable |
| layout.Help |
|
|
| SetSession(session.Session) tea.Cmd |
| GoToBottom() tea.Cmd |
| GetSelectedText() string |
| CopySelectedText(bool) tea.Cmd |
| } |
|
|
| |
| |
| |
| type messageListCmp struct { |
| app *app.App |
| width, height int |
| session session.Session |
| listCmp list.List[list.Item] |
| previousSelected string |
|
|
| lastUserMessageTime int64 |
| defaultListKeyMap list.KeyMap |
|
|
| |
| lastClickTime time.Time |
| lastClickX int |
| lastClickY int |
| clickCount int |
| promptQueue int |
| } |
|
|
| |
| |
| func New(app *app.App) MessageListCmp { |
| defaultListKeyMap := list.DefaultKeyMap() |
| listCmp := list.New( |
| []list.Item{}, |
| list.WithGap(1), |
| list.WithDirectionBackward(), |
| list.WithFocus(false), |
| list.WithKeyMap(defaultListKeyMap), |
| list.WithEnableMouse(), |
| ) |
| return &messageListCmp{ |
| app: app, |
| listCmp: listCmp, |
| previousSelected: "", |
| defaultListKeyMap: defaultListKeyMap, |
| } |
| } |
|
|
| |
| func (m *messageListCmp) Init() tea.Cmd { |
| return m.listCmp.Init() |
| } |
|
|
| |
| func (m *messageListCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| var cmds []tea.Cmd |
| if m.session.ID != "" && m.app.CoderAgent != nil { |
| queueSize := m.app.CoderAgent.QueuedPrompts(m.session.ID) |
| if queueSize != m.promptQueue { |
| m.promptQueue = queueSize |
| cmds = append(cmds, m.SetSize(m.width, m.height)) |
| } |
| } |
| switch msg := msg.(type) { |
| case tea.KeyPressMsg: |
| if m.listCmp.IsFocused() && m.listCmp.HasSelection() { |
| switch { |
| case key.Matches(msg, messages.CopyKey): |
| cmds = append(cmds, m.CopySelectedText(true)) |
| return m, tea.Batch(cmds...) |
| case key.Matches(msg, messages.ClearSelectionKey): |
| cmds = append(cmds, m.SelectionClear()) |
| return m, tea.Batch(cmds...) |
| } |
| } |
| case tea.MouseClickMsg: |
| x := msg.X - 1 |
| y := msg.Y - 1 |
| if x < 0 || y < 0 || x >= m.width-2 || y >= m.height-1 { |
| return m, nil |
| } |
| if msg.Button == tea.MouseLeft { |
| cmds = append(cmds, m.handleMouseClick(x, y)) |
| return m, tea.Batch(cmds...) |
| } |
| return m, tea.Batch(cmds...) |
| case tea.MouseMotionMsg: |
| x := msg.X - 1 |
| y := msg.Y - 1 |
| if x < 0 || y < 0 || x >= m.width-2 || y >= m.height-1 { |
| if y < 0 { |
| cmds = append(cmds, m.listCmp.MoveUp(1)) |
| return m, tea.Batch(cmds...) |
| } |
| if y >= m.height-1 { |
| cmds = append(cmds, m.listCmp.MoveDown(1)) |
| return m, tea.Batch(cmds...) |
| } |
| return m, nil |
| } |
| if msg.Button == tea.MouseLeft { |
| m.listCmp.EndSelection(x, y) |
| } |
| return m, tea.Batch(cmds...) |
| case tea.MouseReleaseMsg: |
| x := msg.X - 1 |
| y := msg.Y - 1 |
| if msg.Button == tea.MouseLeft { |
| clickCount := m.clickCount |
| if x < 0 || y < 0 || x >= m.width-2 || y >= m.height-1 { |
| tick := tea.Tick(doubleClickThreshold, func(time.Time) tea.Msg { |
| return SelectionCopyMsg{ |
| clickCount: clickCount, |
| endSelection: false, |
| } |
| }) |
|
|
| cmds = append(cmds, tick) |
| return m, tea.Batch(cmds...) |
| } |
| tick := tea.Tick(doubleClickThreshold, func(time.Time) tea.Msg { |
| return SelectionCopyMsg{ |
| clickCount: clickCount, |
| endSelection: true, |
| x: x, |
| y: y, |
| } |
| }) |
| cmds = append(cmds, tick) |
| return m, tea.Batch(cmds...) |
| } |
| return m, nil |
| case SelectionCopyMsg: |
| if msg.clickCount == m.clickCount && time.Since(m.lastClickTime) >= doubleClickThreshold { |
| |
| if msg.endSelection { |
| m.listCmp.EndSelection(msg.x, msg.y) |
| } |
| m.listCmp.SelectionStop() |
| cmds = append(cmds, m.CopySelectedText(true)) |
| return m, tea.Batch(cmds...) |
| } |
| case pubsub.Event[permission.PermissionNotification]: |
| cmds = append(cmds, m.handlePermissionRequest(msg.Payload)) |
| return m, tea.Batch(cmds...) |
| case SessionSelectedMsg: |
| if msg.ID != m.session.ID { |
| cmds = append(cmds, m.SetSession(msg)) |
| } |
| return m, tea.Batch(cmds...) |
| case SessionClearedMsg: |
| m.session = session.Session{} |
| cmds = append(cmds, m.listCmp.SetItems([]list.Item{})) |
| return m, tea.Batch(cmds...) |
|
|
| case pubsub.Event[message.Message]: |
| cmds = append(cmds, m.handleMessageEvent(msg)) |
| return m, tea.Batch(cmds...) |
|
|
| case tea.MouseWheelMsg: |
| u, cmd := m.listCmp.Update(msg) |
| m.listCmp = u.(list.List[list.Item]) |
| cmds = append(cmds, cmd) |
| return m, tea.Batch(cmds...) |
| } |
|
|
| u, cmd := m.listCmp.Update(msg) |
| m.listCmp = u.(list.List[list.Item]) |
| cmds = append(cmds, cmd) |
| return m, tea.Batch(cmds...) |
| } |
|
|
| |
| func (m *messageListCmp) View() string { |
| t := styles.CurrentTheme() |
| height := m.height |
| if m.promptQueue > 0 { |
| height -= 4 |
| } |
| view := []string{ |
| t.S().Base. |
| Padding(1, 1, 0, 1). |
| Width(m.width). |
| Height(height). |
| Render( |
| m.listCmp.View(), |
| ), |
| } |
| if m.app.CoderAgent != nil && m.promptQueue > 0 { |
| queuePill := queuePill(m.promptQueue, t) |
| view = append(view, t.S().Base.PaddingLeft(4).PaddingTop(1).Render(queuePill)) |
| } |
| return strings.Join(view, "\n") |
| } |
|
|
| func (m *messageListCmp) handlePermissionRequest(permission permission.PermissionNotification) tea.Cmd { |
| items := m.listCmp.Items() |
| if toolCallIndex := m.findToolCallByID(items, permission.ToolCallID); toolCallIndex != NotFound { |
| toolCall := items[toolCallIndex].(messages.ToolCallCmp) |
| toolCall.SetPermissionRequested() |
| if permission.Granted { |
| toolCall.SetPermissionGranted() |
| } |
| m.listCmp.UpdateItem(toolCall.ID(), toolCall) |
| } |
| return nil |
| } |
|
|
| |
| func (m *messageListCmp) handleChildSession(event pubsub.Event[message.Message]) tea.Cmd { |
| var cmds []tea.Cmd |
| if len(event.Payload.ToolCalls()) == 0 && len(event.Payload.ToolResults()) == 0 { |
| return nil |
| } |
| items := m.listCmp.Items() |
| toolCallInx := NotFound |
| var toolCall messages.ToolCallCmp |
| for i := len(items) - 1; i >= 0; i-- { |
| if msg, ok := items[i].(messages.ToolCallCmp); ok { |
| if msg.GetToolCall().ID == event.Payload.SessionID { |
| toolCallInx = i |
| toolCall = msg |
| } |
| } |
| } |
| if toolCallInx == NotFound { |
| return nil |
| } |
| nestedToolCalls := toolCall.GetNestedToolCalls() |
| for _, tc := range event.Payload.ToolCalls() { |
| found := false |
| for existingInx, existingTC := range nestedToolCalls { |
| if existingTC.GetToolCall().ID == tc.ID { |
| nestedToolCalls[existingInx].SetToolCall(tc) |
| found = true |
| break |
| } |
| } |
| if !found { |
| nestedCall := messages.NewToolCallCmp( |
| event.Payload.ID, |
| tc, |
| m.app.Permissions, |
| messages.WithToolCallNested(true), |
| ) |
| cmds = append(cmds, nestedCall.Init()) |
| nestedToolCalls = append( |
| nestedToolCalls, |
| nestedCall, |
| ) |
| } |
| } |
| for _, tr := range event.Payload.ToolResults() { |
| for nestedInx, nestedTC := range nestedToolCalls { |
| if nestedTC.GetToolCall().ID == tr.ToolCallID { |
| nestedToolCalls[nestedInx].SetToolResult(tr) |
| break |
| } |
| } |
| } |
|
|
| toolCall.SetNestedToolCalls(nestedToolCalls) |
| m.listCmp.UpdateItem( |
| toolCall.ID(), |
| toolCall, |
| ) |
| return tea.Batch(cmds...) |
| } |
|
|
| |
| func (m *messageListCmp) handleMessageEvent(event pubsub.Event[message.Message]) tea.Cmd { |
| switch event.Type { |
| case pubsub.CreatedEvent: |
| if event.Payload.SessionID != m.session.ID { |
| return m.handleChildSession(event) |
| } |
| if m.messageExists(event.Payload.ID) { |
| return nil |
| } |
| return m.handleNewMessage(event.Payload) |
| case pubsub.UpdatedEvent: |
| if event.Payload.SessionID != m.session.ID { |
| return m.handleChildSession(event) |
| } |
| switch event.Payload.Role { |
| case message.Assistant: |
| return m.handleUpdateAssistantMessage(event.Payload) |
| case message.Tool: |
| return m.handleToolMessage(event.Payload) |
| } |
| } |
| return nil |
| } |
|
|
| |
| func (m *messageListCmp) messageExists(messageID string) bool { |
| items := m.listCmp.Items() |
| |
| for i := len(items) - 1; i >= 0; i-- { |
| if msg, ok := items[i].(messages.MessageCmp); ok && msg.GetMessage().ID == messageID { |
| return true |
| } |
| } |
| return false |
| } |
|
|
| |
| func (m *messageListCmp) handleNewMessage(msg message.Message) tea.Cmd { |
| switch msg.Role { |
| case message.User: |
| return m.handleNewUserMessage(msg) |
| case message.Assistant: |
| return m.handleNewAssistantMessage(msg) |
| case message.Tool: |
| return m.handleToolMessage(msg) |
| } |
| return nil |
| } |
|
|
| |
| func (m *messageListCmp) handleNewUserMessage(msg message.Message) tea.Cmd { |
| m.lastUserMessageTime = msg.CreatedAt |
| return m.listCmp.AppendItem(messages.NewMessageCmp(msg)) |
| } |
|
|
| |
| func (m *messageListCmp) handleToolMessage(msg message.Message) tea.Cmd { |
| items := m.listCmp.Items() |
| for _, tr := range msg.ToolResults() { |
| if toolCallIndex := m.findToolCallByID(items, tr.ToolCallID); toolCallIndex != NotFound { |
| toolCall := items[toolCallIndex].(messages.ToolCallCmp) |
| toolCall.SetToolResult(tr) |
| m.listCmp.UpdateItem(toolCall.ID(), toolCall) |
| } |
| } |
| return nil |
| } |
|
|
| |
| |
| func (m *messageListCmp) findToolCallByID(items []list.Item, toolCallID string) int { |
| |
| for i := len(items) - 1; i >= 0; i-- { |
| if toolCall, ok := items[i].(messages.ToolCallCmp); ok && toolCall.GetToolCall().ID == toolCallID { |
| return i |
| } |
| } |
| return NotFound |
| } |
|
|
| |
| |
| func (m *messageListCmp) handleUpdateAssistantMessage(msg message.Message) tea.Cmd { |
| var cmds []tea.Cmd |
| items := m.listCmp.Items() |
|
|
| |
| assistantIndex, existingToolCalls := m.findAssistantMessageAndToolCalls(items, msg.ID) |
|
|
| |
| if cmd := m.updateAssistantMessageContent(msg, assistantIndex); cmd != nil { |
| cmds = append(cmds, cmd) |
| } |
|
|
| |
| if cmd := m.updateToolCalls(msg, existingToolCalls); cmd != nil { |
| cmds = append(cmds, cmd) |
| } |
|
|
| return tea.Batch(cmds...) |
| } |
|
|
| |
| func (m *messageListCmp) findAssistantMessageAndToolCalls(items []list.Item, messageID string) (int, map[int]messages.ToolCallCmp) { |
| assistantIndex := NotFound |
| toolCalls := make(map[int]messages.ToolCallCmp) |
|
|
| |
| for i := len(items) - 1; i >= 0; i-- { |
| item := items[i] |
| if asMsg, ok := item.(messages.MessageCmp); ok { |
| if asMsg.GetMessage().ID == messageID { |
| assistantIndex = i |
| } |
| } else if tc, ok := item.(messages.ToolCallCmp); ok { |
| if tc.ParentMessageID() == messageID { |
| toolCalls[i] = tc |
| } |
| } |
| } |
|
|
| return assistantIndex, toolCalls |
| } |
|
|
| |
| func (m *messageListCmp) updateAssistantMessageContent(msg message.Message, assistantIndex int) tea.Cmd { |
| if assistantIndex == NotFound { |
| return nil |
| } |
|
|
| shouldShowMessage := m.shouldShowAssistantMessage(msg) |
| hasToolCallsOnly := len(msg.ToolCalls()) > 0 && msg.Content().Text == "" |
|
|
| var cmd tea.Cmd |
| if shouldShowMessage { |
| items := m.listCmp.Items() |
| uiMsg := items[assistantIndex].(messages.MessageCmp) |
| uiMsg.SetMessage(msg) |
| m.listCmp.UpdateItem( |
| items[assistantIndex].ID(), |
| uiMsg, |
| ) |
| if msg.FinishPart() != nil && msg.FinishPart().Reason == message.FinishReasonEndTurn { |
| m.listCmp.AppendItem( |
| messages.NewAssistantSection( |
| msg, |
| time.Unix(m.lastUserMessageTime, 0), |
| ), |
| ) |
| } |
| } else if hasToolCallsOnly { |
| items := m.listCmp.Items() |
| m.listCmp.DeleteItem(items[assistantIndex].ID()) |
| } |
|
|
| return cmd |
| } |
|
|
| |
| func (m *messageListCmp) shouldShowAssistantMessage(msg message.Message) bool { |
| return len(msg.ToolCalls()) == 0 || msg.Content().Text != "" || msg.ReasoningContent().Thinking != "" || msg.IsThinking() |
| } |
|
|
| |
| func (m *messageListCmp) updateToolCalls(msg message.Message, existingToolCalls map[int]messages.ToolCallCmp) tea.Cmd { |
| var cmds []tea.Cmd |
|
|
| for _, tc := range msg.ToolCalls() { |
| if cmd := m.updateOrAddToolCall(msg, tc, existingToolCalls); cmd != nil { |
| cmds = append(cmds, cmd) |
| } |
| } |
|
|
| return tea.Batch(cmds...) |
| } |
|
|
| |
| func (m *messageListCmp) updateOrAddToolCall(msg message.Message, tc message.ToolCall, existingToolCalls map[int]messages.ToolCallCmp) tea.Cmd { |
| |
| for _, existingTC := range existingToolCalls { |
| if tc.ID == existingTC.GetToolCall().ID { |
| existingTC.SetToolCall(tc) |
| if msg.FinishPart() != nil && msg.FinishPart().Reason == message.FinishReasonCanceled { |
| existingTC.SetCancelled() |
| } |
| m.listCmp.UpdateItem(tc.ID, existingTC) |
| return nil |
| } |
| } |
|
|
| |
| return m.listCmp.AppendItem(messages.NewToolCallCmp(msg.ID, tc, m.app.Permissions)) |
| } |
|
|
| |
| func (m *messageListCmp) handleNewAssistantMessage(msg message.Message) tea.Cmd { |
| var cmds []tea.Cmd |
|
|
| |
| if m.shouldShowAssistantMessage(msg) { |
| cmd := m.listCmp.AppendItem( |
| messages.NewMessageCmp( |
| msg, |
| ), |
| ) |
| cmds = append(cmds, cmd) |
| } |
|
|
| |
| for _, tc := range msg.ToolCalls() { |
| cmd := m.listCmp.AppendItem(messages.NewToolCallCmp(msg.ID, tc, m.app.Permissions)) |
| cmds = append(cmds, cmd) |
| } |
|
|
| return tea.Batch(cmds...) |
| } |
|
|
| |
| func (m *messageListCmp) SetSession(session session.Session) tea.Cmd { |
| if m.session.ID == session.ID { |
| return nil |
| } |
|
|
| m.session = session |
| sessionMessages, err := m.app.Messages.List(context.Background(), session.ID) |
| if err != nil { |
| return util.ReportError(err) |
| } |
|
|
| if len(sessionMessages) == 0 { |
| return m.listCmp.SetItems([]list.Item{}) |
| } |
|
|
| |
| m.lastUserMessageTime = sessionMessages[0].CreatedAt |
|
|
| |
| toolResultMap := m.buildToolResultMap(sessionMessages) |
|
|
| |
| uiMessages := m.convertMessagesToUI(sessionMessages, toolResultMap) |
|
|
| return m.listCmp.SetItems(uiMessages) |
| } |
|
|
| |
| func (m *messageListCmp) buildToolResultMap(messages []message.Message) map[string]message.ToolResult { |
| toolResultMap := make(map[string]message.ToolResult) |
| for _, msg := range messages { |
| for _, tr := range msg.ToolResults() { |
| toolResultMap[tr.ToolCallID] = tr |
| } |
| } |
| return toolResultMap |
| } |
|
|
| |
| func (m *messageListCmp) convertMessagesToUI(sessionMessages []message.Message, toolResultMap map[string]message.ToolResult) []list.Item { |
| uiMessages := make([]list.Item, 0) |
|
|
| for _, msg := range sessionMessages { |
| switch msg.Role { |
| case message.User: |
| m.lastUserMessageTime = msg.CreatedAt |
| uiMessages = append(uiMessages, messages.NewMessageCmp(msg)) |
| case message.Assistant: |
| uiMessages = append(uiMessages, m.convertAssistantMessage(msg, toolResultMap)...) |
| if msg.FinishPart() != nil && msg.FinishPart().Reason == message.FinishReasonEndTurn { |
| uiMessages = append(uiMessages, messages.NewAssistantSection(msg, time.Unix(m.lastUserMessageTime, 0))) |
| } |
| } |
| } |
|
|
| return uiMessages |
| } |
|
|
| |
| func (m *messageListCmp) convertAssistantMessage(msg message.Message, toolResultMap map[string]message.ToolResult) []list.Item { |
| var uiMessages []list.Item |
|
|
| |
| if m.shouldShowAssistantMessage(msg) { |
| uiMessages = append( |
| uiMessages, |
| messages.NewMessageCmp( |
| msg, |
| ), |
| ) |
| } |
|
|
| |
| for _, tc := range msg.ToolCalls() { |
| options := m.buildToolCallOptions(tc, msg, toolResultMap) |
| uiMessages = append(uiMessages, messages.NewToolCallCmp(msg.ID, tc, m.app.Permissions, options...)) |
| |
| if tc.Name == agent.AgentToolName { |
| nestedMessages, _ := m.app.Messages.List(context.Background(), tc.ID) |
| nestedToolResultMap := m.buildToolResultMap(nestedMessages) |
| nestedUIMessages := m.convertMessagesToUI(nestedMessages, nestedToolResultMap) |
| nestedToolCalls := make([]messages.ToolCallCmp, 0, len(nestedUIMessages)) |
| for _, nestedMsg := range nestedUIMessages { |
| if toolCall, ok := nestedMsg.(messages.ToolCallCmp); ok { |
| toolCall.SetIsNested(true) |
| nestedToolCalls = append(nestedToolCalls, toolCall) |
| } |
| } |
| uiMessages[len(uiMessages)-1].(messages.ToolCallCmp).SetNestedToolCalls(nestedToolCalls) |
| } |
| } |
|
|
| return uiMessages |
| } |
|
|
| |
| func (m *messageListCmp) buildToolCallOptions(tc message.ToolCall, msg message.Message, toolResultMap map[string]message.ToolResult) []messages.ToolCallOption { |
| var options []messages.ToolCallOption |
|
|
| |
| if tr, ok := toolResultMap[tc.ID]; ok { |
| options = append(options, messages.WithToolCallResult(tr)) |
| } |
|
|
| |
| if msg.FinishPart() != nil && msg.FinishPart().Reason == message.FinishReasonCanceled { |
| options = append(options, messages.WithToolCallCancelled()) |
| } |
|
|
| return options |
| } |
|
|
| |
| func (m *messageListCmp) GetSize() (int, int) { |
| return m.width, m.height |
| } |
|
|
| |
| func (m *messageListCmp) SetSize(width int, height int) tea.Cmd { |
| m.width = width |
| m.height = height |
| if m.promptQueue > 0 { |
| queueHeight := 3 + 1 |
| lHight := max(0, height-(1+queueHeight)) |
| return m.listCmp.SetSize(width-2, lHight) |
| } |
| return m.listCmp.SetSize(width-2, max(0, height-1)) |
| } |
|
|
| |
| func (m *messageListCmp) Blur() tea.Cmd { |
| return m.listCmp.Blur() |
| } |
|
|
| |
| func (m *messageListCmp) Focus() tea.Cmd { |
| return m.listCmp.Focus() |
| } |
|
|
| |
| func (m *messageListCmp) IsFocused() bool { |
| return m.listCmp.IsFocused() |
| } |
|
|
| func (m *messageListCmp) Bindings() []key.Binding { |
| return m.defaultListKeyMap.KeyBindings() |
| } |
|
|
| func (m *messageListCmp) GoToBottom() tea.Cmd { |
| return m.listCmp.GoToBottom() |
| } |
|
|
| const ( |
| doubleClickThreshold = 500 * time.Millisecond |
| clickTolerance = 2 |
| ) |
|
|
| |
| func (m *messageListCmp) handleMouseClick(x, y int) tea.Cmd { |
| now := time.Now() |
|
|
| |
| if now.Sub(m.lastClickTime) <= doubleClickThreshold && |
| abs(x-m.lastClickX) <= clickTolerance && |
| abs(y-m.lastClickY) <= clickTolerance { |
| m.clickCount++ |
| } else { |
| m.clickCount = 1 |
| } |
|
|
| m.lastClickTime = now |
| m.lastClickX = x |
| m.lastClickY = y |
|
|
| switch m.clickCount { |
| case 1: |
| |
| m.listCmp.StartSelection(x, y) |
| case 2: |
| |
| m.listCmp.SelectWord(x, y) |
| case 3: |
| |
| m.listCmp.SelectParagraph(x, y) |
| m.clickCount = 0 |
| } |
|
|
| return nil |
| } |
|
|
| |
| func (m *messageListCmp) SelectionClear() tea.Cmd { |
| m.listCmp.SelectionClear() |
| m.previousSelected = "" |
| m.lastClickX, m.lastClickY = 0, 0 |
| m.lastClickTime = time.Time{} |
| m.clickCount = 0 |
| return nil |
| } |
|
|
| |
| func (m *messageListCmp) HasSelection() bool { |
| return m.listCmp.HasSelection() |
| } |
|
|
| |
| func (m *messageListCmp) GetSelectedText() string { |
| return m.listCmp.GetSelectedText(3) |
| } |
|
|
| |
| |
| func (m *messageListCmp) CopySelectedText(clear bool) tea.Cmd { |
| if !m.listCmp.HasSelection() { |
| return nil |
| } |
|
|
| selectedText := m.GetSelectedText() |
| if selectedText == "" { |
| return util.ReportInfo("No text selected") |
| } |
|
|
| if clear { |
| defer func() { m.SelectionClear() }() |
| } |
|
|
| return tea.Sequence( |
| |
| |
| tea.SetClipboard(selectedText), |
| func() tea.Msg { |
| _ = clipboard.WriteAll(selectedText) |
| return nil |
| }, |
| util.ReportInfo("Selected text copied to clipboard"), |
| ) |
| } |
|
|
| |
| func abs(x int) int { |
| if x < 0 { |
| return -x |
| } |
| return x |
| } |
|
|