1package model
2
3import (
4 "fmt"
5 "strings"
6
7 tea "charm.land/bubbletea/v2"
8 "charm.land/lipgloss/v2"
9 "git.secluded.site/crush/internal/session"
10 "git.secluded.site/crush/internal/ui/chat"
11 "git.secluded.site/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 if m.layout.pills.Dy() > 0 {
143 if cmd := m.chat.ScrollByAndAnimate(0); cmd != nil {
144 return cmd
145 }
146 }
147 hasPills := hasIncompleteTodos(m.session.Todos) || m.promptQueue > 0
148 if !hasPills {
149 return nil
150 }
151 m.pillsExpanded = !m.pillsExpanded
152 if m.pillsExpanded {
153 if hasIncompleteTodos(m.session.Todos) {
154 m.focusedPillSection = pillSectionTodos
155 } else {
156 m.focusedPillSection = pillSectionQueue
157 }
158 }
159 m.updateLayoutAndSize()
160 return nil
161}
162
163// switchPillSection changes focus between todo and queue sections.
164func (m *UI) switchPillSection(dir int) tea.Cmd {
165 if !m.pillsExpanded || !m.hasSession() {
166 return nil
167 }
168 hasIncompleteTodos := hasIncompleteTodos(m.session.Todos)
169 hasQueue := m.promptQueue > 0
170
171 if dir < 0 && m.focusedPillSection == pillSectionQueue && hasIncompleteTodos {
172 m.focusedPillSection = pillSectionTodos
173 m.updateLayoutAndSize()
174 return nil
175 }
176 if dir > 0 && m.focusedPillSection == pillSectionTodos && hasQueue {
177 m.focusedPillSection = pillSectionQueue
178 m.updateLayoutAndSize()
179 return nil
180 }
181 return nil
182}
183
184// pillsAreaHeight calculates the total height needed for the pills area.
185func (m *UI) pillsAreaHeight() int {
186 if !m.hasSession() {
187 return 0
188 }
189 hasIncomplete := hasIncompleteTodos(m.session.Todos)
190 hasQueue := m.promptQueue > 0
191 hasPills := hasIncomplete || hasQueue
192 if !hasPills {
193 return 0
194 }
195
196 pillsAreaHeight := pillHeightWithBorder
197 if m.pillsExpanded {
198 if m.focusedPillSection == pillSectionTodos && hasIncomplete {
199 pillsAreaHeight += len(m.session.Todos)
200 } else if m.focusedPillSection == pillSectionQueue && hasQueue {
201 pillsAreaHeight += m.promptQueue
202 }
203 }
204 return pillsAreaHeight
205}
206
207// renderPills renders the pills panel and stores it in m.pillsView.
208func (m *UI) renderPills() {
209 m.pillsView = ""
210 if !m.hasSession() {
211 return
212 }
213
214 width := m.layout.pills.Dx()
215 if width <= 0 {
216 return
217 }
218
219 paddingLeft := 3
220 contentWidth := max(width-paddingLeft, 0)
221
222 hasIncomplete := hasIncompleteTodos(m.session.Todos)
223 hasQueue := m.promptQueue > 0
224
225 if !hasIncomplete && !hasQueue {
226 return
227 }
228
229 t := m.com.Styles
230 todosFocused := m.pillsExpanded && m.focusedPillSection == pillSectionTodos
231 queueFocused := m.pillsExpanded && m.focusedPillSection == pillSectionQueue
232
233 inProgressIcon := t.Tool.TodoInProgressIcon.Render(styles.SpinnerIcon)
234 if m.todoIsSpinning {
235 inProgressIcon = m.todoSpinner.View()
236 }
237
238 var pills []string
239 if hasIncomplete {
240 pills = append(pills, todoPill(m.session.Todos, inProgressIcon, todosFocused, m.pillsExpanded, t))
241 }
242 if hasQueue {
243 pills = append(pills, queuePill(m.promptQueue, queueFocused, m.pillsExpanded, t))
244 }
245
246 var expandedList string
247 if m.pillsExpanded {
248 if todosFocused && hasIncomplete {
249 expandedList = todoList(m.session.Todos, inProgressIcon, t, contentWidth)
250 } else if queueFocused && hasQueue {
251 if m.com.App != nil && m.com.App.AgentCoordinator != nil {
252 queueItems := m.com.App.AgentCoordinator.QueuedPromptsList(m.session.ID)
253 expandedList = queueList(queueItems, t)
254 }
255 }
256 }
257
258 if len(pills) == 0 {
259 return
260 }
261
262 pillsRow := lipgloss.JoinHorizontal(lipgloss.Top, pills...)
263
264 helpDesc := "open"
265 if m.pillsExpanded {
266 helpDesc = "close"
267 }
268 helpKey := t.Pills.HelpKey.Render("ctrl+t")
269 helpText := t.Pills.HelpText.Render(helpDesc)
270 helpHint := lipgloss.JoinHorizontal(lipgloss.Center, helpKey, " ", helpText)
271 pillsRow = lipgloss.JoinHorizontal(lipgloss.Center, pillsRow, " ", helpHint)
272
273 pillsArea := pillsRow
274 if expandedList != "" {
275 pillsArea = lipgloss.JoinVertical(lipgloss.Left, pillsRow, expandedList)
276 }
277
278 m.pillsView = t.Pills.Area.MaxWidth(width).PaddingLeft(paddingLeft).Render(pillsArea)
279}