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.Pills.QueueIconBase, "▶▶▶▶▶▶▶▶▶", false, t.Pills.QueueGradFromColor, t.Pills.QueueGradToColor)
60 if queue < len(triangles) {
61 triangles = triangles[:queue]
62 }
63
64 text := t.Pills.QueueLabel.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.Pills.TodoLabel.Render("To-Do")
91 progress := t.Pills.TodoProgress.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.Pills.TodoCurrentTask.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.Pills.QueueItemText.Render(text))
132 }
133
134 return strings.Join(lines, "\n")
135}
136
137// pillsHeightReasonableTerminalHeight is the minimum terminal height at which
138// we auto-expand pills when there are incomplete todos.
139const pillsHeightReasonableTerminalHeight = 40
140
141// autoExpandPillsIfReasonable expands the pills panel if the terminal has
142// enough vertical space to show the expanded list comfortably.
143func (m *UI) autoExpandPillsIfReasonable() tea.Cmd {
144 if !m.hasSession() {
145 return nil
146 }
147 if m.height < pillsHeightReasonableTerminalHeight {
148 return nil
149 }
150 hasPills := hasIncompleteTodos(m.session.Todos) || m.promptQueue > 0
151 if !hasPills {
152 return nil
153 }
154 if m.pillsExpanded {
155 return nil
156 }
157 m.pillsExpanded = true
158 if hasIncompleteTodos(m.session.Todos) {
159 m.focusedPillSection = pillSectionTodos
160 } else {
161 m.focusedPillSection = pillSectionQueue
162 }
163 m.updateLayoutAndSize()
164 if m.chat.Follow() {
165 m.chat.ScrollToBottom()
166 }
167 return nil
168}
169
170// togglePillsExpanded toggles the pills panel expansion state.
171func (m *UI) togglePillsExpanded() tea.Cmd {
172 if !m.hasSession() {
173 return nil
174 }
175 hasPills := hasIncompleteTodos(m.session.Todos) || m.promptQueue > 0
176 if !hasPills {
177 return nil
178 }
179 m.pillsExpanded = !m.pillsExpanded
180 if m.pillsExpanded {
181 if hasIncompleteTodos(m.session.Todos) {
182 m.focusedPillSection = pillSectionTodos
183 } else {
184 m.focusedPillSection = pillSectionQueue
185 }
186 }
187 m.updateLayoutAndSize()
188
189 // Make sure to follow scroll if follow is enabled when toggling pills.
190 if m.chat.Follow() {
191 m.chat.ScrollToBottom()
192 }
193
194 return nil
195}
196
197// switchPillSection changes focus between todo and queue sections.
198func (m *UI) switchPillSection(dir int) tea.Cmd {
199 if !m.pillsExpanded || !m.hasSession() {
200 return nil
201 }
202 hasIncompleteTodos := hasIncompleteTodos(m.session.Todos)
203 hasQueue := m.promptQueue > 0
204
205 if dir < 0 && m.focusedPillSection == pillSectionQueue && hasIncompleteTodos {
206 m.focusedPillSection = pillSectionTodos
207 m.updateLayoutAndSize()
208 return nil
209 }
210 if dir > 0 && m.focusedPillSection == pillSectionTodos && hasQueue {
211 m.focusedPillSection = pillSectionQueue
212 m.updateLayoutAndSize()
213 return nil
214 }
215 return nil
216}
217
218// pillsAreaHeight calculates the total height needed for the pills area.
219func (m *UI) pillsAreaHeight() int {
220 if !m.hasSession() {
221 return 0
222 }
223 hasIncomplete := hasIncompleteTodos(m.session.Todos)
224 hasQueue := m.promptQueue > 0
225 hasPills := hasIncomplete || hasQueue
226 if !hasPills {
227 return 0
228 }
229
230 pillsAreaHeight := pillHeightWithBorder
231 if m.pillsExpanded {
232 if m.focusedPillSection == pillSectionTodos && hasIncomplete {
233 pillsAreaHeight += len(m.session.Todos)
234 } else if m.focusedPillSection == pillSectionQueue && hasQueue {
235 pillsAreaHeight += m.promptQueue
236 }
237 }
238 return pillsAreaHeight
239}
240
241// renderPills renders the pills panel and stores it in m.pillsView.
242func (m *UI) renderPills() {
243 m.pillsView = ""
244 if !m.hasSession() {
245 return
246 }
247
248 width := m.layout.pills.Dx()
249 if width <= 0 {
250 return
251 }
252
253 paddingLeft := 3
254 contentWidth := max(width-paddingLeft, 0)
255
256 hasIncomplete := hasIncompleteTodos(m.session.Todos)
257 hasQueue := m.promptQueue > 0
258
259 if !hasIncomplete && !hasQueue {
260 return
261 }
262
263 t := m.com.Styles
264 todosFocused := m.pillsExpanded && m.focusedPillSection == pillSectionTodos
265 queueFocused := m.pillsExpanded && m.focusedPillSection == pillSectionQueue
266
267 inProgressIcon := t.Tool.TodoInProgressIcon.Render(styles.SpinnerIcon)
268 if m.todoIsSpinning {
269 inProgressIcon = m.todoSpinner.View()
270 }
271
272 var pills []string
273 if hasIncomplete {
274 pills = append(pills, todoPill(m.session.Todos, inProgressIcon, todosFocused, m.pillsExpanded, t))
275 }
276 if hasQueue {
277 pills = append(pills, queuePill(m.promptQueue, queueFocused, m.pillsExpanded, t))
278 }
279
280 var expandedList string
281 if m.pillsExpanded {
282 if todosFocused && hasIncomplete {
283 expandedList = todoList(m.session.Todos, inProgressIcon, t, contentWidth)
284 } else if queueFocused && hasQueue {
285 if m.com != nil && m.com.Workspace != nil && m.com.Workspace.AgentIsReady() {
286 queueItems := m.com.Workspace.AgentQueuedPromptsList(m.session.ID)
287 expandedList = queueList(queueItems, t)
288 }
289 }
290 }
291
292 if len(pills) == 0 {
293 return
294 }
295
296 pillsRow := lipgloss.JoinHorizontal(lipgloss.Top, pills...)
297
298 helpDesc := "open"
299 if m.pillsExpanded {
300 helpDesc = "close"
301 }
302 helpKey := t.Pills.HelpKey.Render("ctrl+t")
303 helpText := t.Pills.HelpText.Render(helpDesc)
304 helpHint := lipgloss.JoinHorizontal(lipgloss.Center, helpKey, " ", helpText)
305 pillsRow = lipgloss.JoinHorizontal(lipgloss.Center, pillsRow, " ", helpHint)
306
307 pillsArea := pillsRow
308 if expandedList != "" {
309 pillsArea = lipgloss.JoinVertical(lipgloss.Left, pillsRow, expandedList)
310 }
311
312 m.pillsView = t.Pills.Area.MaxWidth(width).PaddingLeft(paddingLeft).Render(pillsArea)
313}