1package model
2
3import (
4 "fmt"
5 "strings"
6
7 tea "charm.land/bubbletea/v2"
8 "charm.land/lipgloss/v2"
9 "github.com/charmbracelet/crush/internal/session"
10 "github.com/charmbracelet/crush/internal/ui/chat"
11 "github.com/charmbracelet/crush/internal/ui/styles"
12)
13
14// pillStyle returns the appropriate style for a pill based on focus state.
15func pillStyle(focused, panelFocused bool, t *styles.Styles) lipgloss.Style {
16 if !panelFocused || focused {
17 return t.Pills.Focused
18 }
19 return t.Pills.Blurred
20}
21
22const (
23 // pillHeightWithBorder is the height of a pill including its border.
24 pillHeightWithBorder = 3
25 // maxTaskDisplayLength is the maximum length of a task name in the pill.
26 maxTaskDisplayLength = 40
27 // maxQueueDisplayLength is the maximum length of a queue item in the list.
28 maxQueueDisplayLength = 60
29)
30
31// pillSection represents which section of the pills panel is focused.
32type pillSection int
33
34const (
35 pillSectionTodos pillSection = iota
36 pillSectionQueue
37)
38
39// hasIncompleteTodos returns true if there are any non-completed todos.
40func hasIncompleteTodos(todos []session.Todo) bool {
41 return session.HasIncompleteTodos(todos)
42}
43
44// hasInProgressTodo returns true if there is at least one in-progress todo.
45func hasInProgressTodo(todos []session.Todo) bool {
46 for _, todo := range todos {
47 if todo.Status == session.TodoStatusInProgress {
48 return true
49 }
50 }
51 return false
52}
53
54// queuePill renders the queue count pill with gradient triangles.
55func queuePill(queue int, focused, panelFocused bool, t *styles.Styles) string {
56 if queue <= 0 {
57 return ""
58 }
59 triangles := styles.ForegroundGrad(t, "▶▶▶▶▶▶▶▶▶", false, t.RedDark, t.Secondary)
60 if queue < len(triangles) {
61 triangles = triangles[:queue]
62 }
63
64 text := t.Base.Render(fmt.Sprintf("%d Queued", queue))
65 content := fmt.Sprintf("%s %s", strings.Join(triangles, ""), text)
66 return pillStyle(focused, panelFocused, t).Render(content)
67}
68
69// todoPill renders the todo progress pill with optional spinner and task name.
70func todoPill(todos []session.Todo, spinnerView string, focused, panelFocused bool, t *styles.Styles) string {
71 if !hasIncompleteTodos(todos) {
72 return ""
73 }
74
75 completed := 0
76 var currentTodo *session.Todo
77 for i := range todos {
78 switch todos[i].Status {
79 case session.TodoStatusCompleted:
80 completed++
81 case session.TodoStatusInProgress:
82 if currentTodo == nil {
83 currentTodo = &todos[i]
84 }
85 }
86 }
87
88 total := len(todos)
89
90 label := t.Base.Render("To-Do")
91 progress := t.Muted.Render(fmt.Sprintf("%d/%d", completed, total))
92
93 var content string
94 if panelFocused {
95 content = fmt.Sprintf("%s %s", label, progress)
96 } else if currentTodo != nil {
97 taskText := currentTodo.Content
98 if currentTodo.ActiveForm != "" {
99 taskText = currentTodo.ActiveForm
100 }
101 if len(taskText) > maxTaskDisplayLength {
102 taskText = taskText[:maxTaskDisplayLength-1] + "…"
103 }
104 task := t.Subtle.Render(taskText)
105 content = fmt.Sprintf("%s %s %s %s", spinnerView, label, progress, task)
106 } else {
107 content = fmt.Sprintf("%s %s", label, progress)
108 }
109
110 return pillStyle(focused, panelFocused, t).Render(content)
111}
112
113// todoList renders the expanded todo list.
114func todoList(sessionTodos []session.Todo, spinnerView string, t *styles.Styles, width int) string {
115 return chat.FormatTodosList(t, sessionTodos, spinnerView, width)
116}
117
118// queueList renders the expanded queue items list.
119func queueList(queueItems []string, t *styles.Styles) string {
120 if len(queueItems) == 0 {
121 return ""
122 }
123
124 var lines []string
125 for _, item := range queueItems {
126 text := item
127 if len(text) > maxQueueDisplayLength {
128 text = text[:maxQueueDisplayLength-1] + "…"
129 }
130 prefix := t.Pills.QueueItemPrefix.Render() + " "
131 lines = append(lines, prefix+t.Muted.Render(text))
132 }
133
134 return strings.Join(lines, "\n")
135}
136
137// togglePillsExpanded toggles the pills panel expansion state.
138func (m *UI) togglePillsExpanded() tea.Cmd {
139 if !m.hasSession() {
140 return nil
141 }
142 hasPills := hasIncompleteTodos(m.session.Todos) || m.promptQueue > 0
143 if !hasPills {
144 return nil
145 }
146 m.pillsExpanded = !m.pillsExpanded
147 if m.pillsExpanded {
148 if hasIncompleteTodos(m.session.Todos) {
149 m.focusedPillSection = pillSectionTodos
150 } else {
151 m.focusedPillSection = pillSectionQueue
152 }
153 }
154 m.updateLayoutAndSize()
155
156 // Make sure to follow scroll if follow is enabled when toggling pills.
157 if m.chat.Follow() {
158 m.chat.ScrollToBottom()
159 }
160
161 return nil
162}
163
164// switchPillSection changes focus between todo and queue sections.
165func (m *UI) switchPillSection(dir int) tea.Cmd {
166 if !m.pillsExpanded || !m.hasSession() {
167 return nil
168 }
169 hasIncompleteTodos := hasIncompleteTodos(m.session.Todos)
170 hasQueue := m.promptQueue > 0
171
172 if dir < 0 && m.focusedPillSection == pillSectionQueue && hasIncompleteTodos {
173 m.focusedPillSection = pillSectionTodos
174 m.updateLayoutAndSize()
175 return nil
176 }
177 if dir > 0 && m.focusedPillSection == pillSectionTodos && hasQueue {
178 m.focusedPillSection = pillSectionQueue
179 m.updateLayoutAndSize()
180 return nil
181 }
182 return nil
183}
184
185// pillsAreaHeight calculates the total height needed for the pills area.
186func (m *UI) pillsAreaHeight() int {
187 if !m.hasSession() {
188 return 0
189 }
190 hasIncomplete := hasIncompleteTodos(m.session.Todos)
191 hasQueue := m.promptQueue > 0
192 hasPills := hasIncomplete || hasQueue
193 if !hasPills {
194 return 0
195 }
196
197 pillsAreaHeight := pillHeightWithBorder
198 if m.pillsExpanded {
199 if m.focusedPillSection == pillSectionTodos && hasIncomplete {
200 pillsAreaHeight += len(m.session.Todos)
201 } else if m.focusedPillSection == pillSectionQueue && hasQueue {
202 pillsAreaHeight += m.promptQueue
203 }
204 }
205 return pillsAreaHeight
206}
207
208// renderPills renders the pills panel and stores it in m.pillsView.
209func (m *UI) renderPills() {
210 m.pillsView = ""
211 if !m.hasSession() {
212 return
213 }
214
215 width := m.layout.pills.Dx()
216 if width <= 0 {
217 return
218 }
219
220 paddingLeft := 3
221 contentWidth := max(width-paddingLeft, 0)
222
223 hasIncomplete := hasIncompleteTodos(m.session.Todos)
224 hasQueue := m.promptQueue > 0
225
226 if !hasIncomplete && !hasQueue {
227 return
228 }
229
230 t := m.com.Styles
231 todosFocused := m.pillsExpanded && m.focusedPillSection == pillSectionTodos
232 queueFocused := m.pillsExpanded && m.focusedPillSection == pillSectionQueue
233
234 inProgressIcon := t.Tool.TodoInProgressIcon.Render(styles.SpinnerIcon)
235 if m.todoIsSpinning {
236 inProgressIcon = m.todoSpinner.View()
237 }
238
239 var pills []string
240 if hasIncomplete {
241 pills = append(pills, todoPill(m.session.Todos, inProgressIcon, todosFocused, m.pillsExpanded, t))
242 }
243 if hasQueue {
244 pills = append(pills, queuePill(m.promptQueue, queueFocused, m.pillsExpanded, t))
245 }
246
247 var expandedList string
248 if m.pillsExpanded {
249 if todosFocused && hasIncomplete {
250 expandedList = todoList(m.session.Todos, inProgressIcon, t, contentWidth)
251 } else if queueFocused && hasQueue {
252 if m.com.App != nil && m.com.App.AgentCoordinator != nil {
253 queueItems := m.com.App.AgentCoordinator.QueuedPromptsList(m.session.ID)
254 expandedList = queueList(queueItems, t)
255 }
256 }
257 }
258
259 if len(pills) == 0 {
260 return
261 }
262
263 pillsRow := lipgloss.JoinHorizontal(lipgloss.Top, pills...)
264
265 helpDesc := "open"
266 if m.pillsExpanded {
267 helpDesc = "close"
268 }
269 helpKey := t.Pills.HelpKey.Render("ctrl+t")
270 helpText := t.Pills.HelpText.Render(helpDesc)
271 helpHint := lipgloss.JoinHorizontal(lipgloss.Center, helpKey, " ", helpText)
272 pillsRow = lipgloss.JoinHorizontal(lipgloss.Center, pillsRow, " ", helpHint)
273
274 pillsArea := pillsRow
275 if expandedList != "" {
276 pillsArea = lipgloss.JoinVertical(lipgloss.Left, pillsRow, expandedList)
277 }
278
279 m.pillsView = t.Pills.Area.MaxWidth(width).PaddingLeft(paddingLeft).Render(pillsArea)
280}