| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "time" |
| 7 | |
| 8 | tea "github.com/charmbracelet/bubbletea" |
| 9 | "github.com/charmbracelet/lipgloss" |
| 10 | "github.com/charmbracelet/x/ansi" |
| 11 | "the-valley/flow/internal/jobs" |
| 12 | ) |
| 13 | |
| 14 | // The jobs section lists the session's consults, reviews, and running prose |
| 15 | // passes below the run rows, as klaus status does: every running job, then |
| 16 | // consults and reviews that finished within recentJobs. Finished prose |
| 17 | // passes within recentJobs are counted on the jobs header line. tab moves |
| 18 | // the selection between the runs and the listed jobs; enter on a job opens |
| 19 | // its record and answer (showJob) in place of the list. |
| 20 | |
| 21 | // jobsLoadedMsg carries the session's job records, newest first. |
| 22 | type jobsLoadedMsg struct { |
| 23 | jobs []*jobs.Job |
| 24 | } |
| 25 | |
| 26 | // jobsFSMsg says a job record changed on disk. |
| 27 | type jobsFSMsg struct{} |
| 28 | |
| 29 | // jobDetail is the job whose record and answer the dashboard shows in |
| 30 | // place of the list, scrolled to offset. |
| 31 | type jobDetail struct { |
| 32 | id string |
| 33 | text string // showJob's output, made printable |
| 34 | offset int |
| 35 | } |
| 36 | |
| 37 | // loadJobsCmd reads the job records. Reading judges which running jobs |
| 38 | // died, so a job killed without a trace shows as died on the next load. |
| 39 | func loadJobsCmd(dir string) tea.Cmd { |
| 40 | if dir == "" { |
| 41 | return nil |
| 42 | } |
| 43 | return func() tea.Msg { |
| 44 | list, err := jobs.Store{Dir: dir}.List() |
| 45 | if err != nil { |
| 46 | return errHintMsg{msg: "reading jobs: " + err.Error()} |
| 47 | } |
| 48 | return jobsLoadedMsg{jobs: list} |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // recentJobView is what the jobs section shows: the listed recent jobs |
| 53 | // (consults, reviews, and running prose passes) and a count of finished |
| 54 | // prose passes, excluding any that finished before the x key last cleared |
| 55 | // them. |
| 56 | func (m dashboardModel) recentJobView(now time.Time) ([]*jobs.Job, proseSummary) { |
| 57 | var recent []*jobs.Job |
| 58 | for _, j := range jobs.Recent(m.jobs, now, recentJobs) { |
| 59 | if !j.Finished() || j.Settled().After(m.jobsClearedAt) { |
| 60 | recent = append(recent, j) |
| 61 | } |
| 62 | } |
| 63 | return partitionJobs(recent) |
| 64 | } |
| 65 | |
| 66 | // rowProse counts finished prose passes belonging to any run on row r. |
| 67 | func (m dashboardModel) rowProse(r dashRow) proseSummary { |
| 68 | if len(r.runs) == 0 || len(m.jobs) == 0 { |
| 69 | return proseSummary{} |
| 70 | } |
| 71 | ids := make(map[string]bool, len(r.runs)) |
| 72 | for _, s := range r.runs { |
| 73 | ids[s.ID] = true |
| 74 | } |
| 75 | var p proseSummary |
| 76 | for _, j := range m.jobs { |
| 77 | if j.Kind == jobs.KindProse && j.Finished() && j.Run != "" && ids[j.Run] { |
| 78 | p.add(j) |
| 79 | } |
| 80 | } |
| 81 | return p |
| 82 | } |
| 83 | |
| 84 | // renderJobsHeader renders the line above the job lines, counting the |
| 85 | // running and finished listed jobs, any that don't fit, and the finished |
| 86 | // prose passes in the window. |
| 87 | func renderJobsHeader(v dashView) string { |
| 88 | running := 0 |
| 89 | for _, j := range v.jobs { |
| 90 | if !j.Finished() { |
| 91 | running++ |
| 92 | } |
| 93 | } |
| 94 | parts := []string{" jobs"} |
| 95 | if running > 0 { |
| 96 | parts = append(parts, fmt.Sprintf("%d running", running)) |
| 97 | } |
| 98 | if n := len(v.jobs) - running; n > 0 { |
| 99 | parts = append(parts, fmt.Sprintf("%d finished", n)) |
| 100 | } |
| 101 | if n := len(v.jobs) - v.jobsShown; n > 0 { |
| 102 | parts = append(parts, fmt.Sprintf("%d not shown", n)) |
| 103 | } |
| 104 | if s := v.prose.String(); s != "" { |
| 105 | parts = append(parts, s) |
| 106 | } |
| 107 | return dimStyle.Render(strings.Join(parts, " · ")) |
| 108 | } |
| 109 | |
| 110 | // renderJobLine renders one job: its kind and the tail of its ID, its |
| 111 | // thread or PR, backend and model, status, time and prompt. |
| 112 | func renderJobLine(j *jobs.Job, selected bool, now time.Time) string { |
| 113 | gutter := " " |
| 114 | if selected { |
| 115 | gutter = "> " |
| 116 | } |
| 117 | prefix := fmt.Sprintf("%s%-14s %-14s %-20s", gutter, j.Kind+":"+shortRunID(j.ID), |
| 118 | truncateLine(printable(jobTarget(j)), 14), truncateLine(jobBackendModel(j), 20)) |
| 119 | if selected { |
| 120 | prefix = selectedStyle.Render(prefix) |
| 121 | } |
| 122 | status := fmt.Sprintf("%-11s", strings.ToUpper(string(j.Status))) |
| 123 | return prefix + " " + jobStatusStyle(j.Status).Render(status) + " " + |
| 124 | dimStyle.Render(fmt.Sprintf("%-6s", jobTime(j, now))) + " " + printable(j.Prompt) |
| 125 | } |
| 126 | |
| 127 | func jobStatusStyle(s jobs.Status) lipgloss.Style { |
| 128 | switch s { |
| 129 | case jobs.Running: |
| 130 | return yellowStyle |
| 131 | case jobs.Succeeded: |
| 132 | return greenStyle |
| 133 | case jobs.Interrupted: |
| 134 | return dimStyle |
| 135 | } |
| 136 | return redStyle |
| 137 | } |
| 138 | |
| 139 | // renderJobsFooter is the footer while a job is selected: how the job |
| 140 | // stands (its error and raw output, if it didn't succeed), then key help. |
| 141 | func renderJobsFooter(v dashView, width int) string { |
| 142 | j := v.jobs[v.jobCursor] |
| 143 | note := j.Kind + ":" + shortRunID(j.ID) + " " + jobStatusText(j) |
| 144 | if j.Status == jobs.Running { |
| 145 | note += " for " + jobTime(j, time.Now()) |
| 146 | } |
| 147 | if j.RawOutput != "" { |
| 148 | note += " · raw output: " + j.RawOutput |
| 149 | } |
| 150 | note = " " + printable(note) |
| 151 | help := []string{ |
| 152 | " | enter read · j/k move · tab runs · q quit", |
| 153 | " | enter read · tab runs", |
| 154 | "", |
| 155 | } |
| 156 | footer := note + help[len(help)-1] |
| 157 | for _, h := range help { |
| 158 | if lipgloss.Width(note+h) <= width { |
| 159 | footer = note + h |
| 160 | break |
| 161 | } |
| 162 | } |
| 163 | return jobStatusStyle(j.Status).Render(note) + dimStyle.Render(strings.TrimPrefix(footer, note)) |
| 164 | } |
| 165 | |
| 166 | // jobKey handles a key while a job is selected; handled is false for keys |
| 167 | // that act as they do on the runs (q, r, h). |
| 168 | func (m dashboardModel) jobKey(key string, now time.Time) (dashboardModel, bool) { |
| 169 | v := m.resolve(now) |
| 170 | if !v.jobFocus { |
| 171 | m.jobFocus = false |
| 172 | return m, false |
| 173 | } |
| 174 | move := func(i int) { |
| 175 | m.jobCursor = clampCursor(i, v.jobsShown) |
| 176 | m.jobSel = v.jobs[m.jobCursor].ID |
| 177 | } |
| 178 | switch key { |
| 179 | case "up", "k": |
| 180 | move(v.jobCursor - 1) |
| 181 | case "down", "j": |
| 182 | move(v.jobCursor + 1) |
| 183 | case "g", "home", "pgup": |
| 184 | move(0) |
| 185 | case "G", "end", "pgdown": |
| 186 | move(v.jobsShown - 1) |
| 187 | case "tab", "esc": |
| 188 | m.jobFocus = false |
| 189 | case "enter", "o": |
| 190 | m.openJobDetail(v.jobs[v.jobCursor], now) |
| 191 | case "a", "d": |
| 192 | m.noteError("a job has no PR: tab back to the runs") |
| 193 | default: |
| 194 | return m, false |
| 195 | } |
| 196 | return m, true |
| 197 | } |
| 198 | |
| 199 | // openJobDetail shows j's record and answer in place of the list. |
| 200 | func (m *dashboardModel) openJobDetail(j *jobs.Job, now time.Time) { |
| 201 | var b strings.Builder |
| 202 | if err := showJob(&b, jobs.Store{Dir: m.jobsDir}, j, now); err != nil { |
| 203 | fmt.Fprintf(&b, "\n%v\n", err) |
| 204 | } |
| 205 | offset := 0 |
| 206 | if m.detail != nil && m.detail.id == j.ID { |
| 207 | offset = m.detail.offset |
| 208 | } |
| 209 | m.detail = &jobDetail{id: j.ID, text: printable(b.String()), offset: offset} |
| 210 | } |
| 211 | |
| 212 | // refreshJobDetail re-reads the open job, so its view follows it finishing. |
| 213 | func (m *dashboardModel) refreshJobDetail(now time.Time) { |
| 214 | if m.detail == nil { |
| 215 | return |
| 216 | } |
| 217 | for _, j := range m.jobs { |
| 218 | if j.ID == m.detail.id { |
| 219 | m.openJobDetail(j, now) |
| 220 | return |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // detailLayout wraps the open job's text to the pane and returns its lines |
| 226 | // and how many fit between the header and footer, which a pane under three |
| 227 | // lines goes without. |
| 228 | func (m dashboardModel) detailLayout(width int) (lines []string, bodyH int, chrome bool) { |
| 229 | lines = strings.Split(strings.TrimRight(ansi.Wrap(m.detail.text, max(width-2, 1), ""), "\n"), "\n") |
| 230 | switch { |
| 231 | case m.height <= 0: |
| 232 | return lines, len(lines), true |
| 233 | case m.height < 3: |
| 234 | return lines, m.height, false |
| 235 | } |
| 236 | return lines, m.height - 2, true |
| 237 | } |
| 238 | |
| 239 | // detailKey scrolls or closes the open job's view. |
| 240 | func (m dashboardModel) detailKey(key string) dashboardModel { |
| 241 | width := m.width |
| 242 | if width <= 0 { |
| 243 | width = defaultDashboardWidth |
| 244 | } |
| 245 | lines, bodyH, _ := m.detailLayout(width) |
| 246 | off := m.detail.offset |
| 247 | switch key { |
| 248 | case "up", "k": |
| 249 | off-- |
| 250 | case "down", "j": |
| 251 | off++ |
| 252 | case "pgup": |
| 253 | off -= bodyH |
| 254 | case "pgdown": |
| 255 | off += bodyH |
| 256 | case "g", "home": |
| 257 | off = 0 |
| 258 | case "G", "end": |
| 259 | off = len(lines) |
| 260 | case "esc", "enter", "q", "tab", "backspace": |
| 261 | m.detail = nil |
| 262 | return m |
| 263 | } |
| 264 | d := *m.detail |
| 265 | d.offset = clamp(off, 0, max(len(lines)-bodyH, 0)) |
| 266 | m.detail = &d |
| 267 | return m |
| 268 | } |
| 269 | |
| 270 | // renderJobDetail renders the open job's record and answer. |
| 271 | func (m dashboardModel) renderJobDetail(width int) string { |
| 272 | lines, bodyH, chrome := m.detailLayout(width) |
| 273 | off := clamp(m.detail.offset, 0, max(len(lines)-bodyH, 0)) |
| 274 | end := min(off+bodyH, len(lines)) |
| 275 | var out []string |
| 276 | if chrome { |
| 277 | title := " job " + m.detail.id |
| 278 | pos := fmt.Sprintf("%d–%d of %d", min(off+1, end), end, len(lines)) |
| 279 | pad := max(width-2-lipgloss.Width(title)-lipgloss.Width(pos), 1) |
| 280 | out = append(out, headerStyle.Render(title+strings.Repeat(" ", pad)+pos)) |
| 281 | } |
| 282 | for _, l := range lines[off:end] { |
| 283 | out = append(out, " "+l) |
| 284 | } |
| 285 | if chrome { |
| 286 | out = append(out, dimStyle.Render(" j/k scroll · pgup/pgdn page · g/G top/bottom · esc back")) |
| 287 | } |
| 288 | for i, l := range out { |
| 289 | out[i] = ansi.Truncate(l, width, "…") |
| 290 | } |
| 291 | return strings.Join(out, "\n") |
| 292 | } |
| 293 | |
| 294 | // printable drops escape sequences and control characters other than |
| 295 | // newlines from text a model or a user wrote, so it can't drive the |
| 296 | // terminal; tabs become spaces. |
| 297 | func printable(s string) string { |
| 298 | s = ansi.Strip(s) |
| 299 | return strings.Map(func(r rune) rune { |
| 300 | switch { |
| 301 | case r == '\n': |
| 302 | return r |
| 303 | case r == '\t': |
| 304 | return ' ' |
| 305 | case r < 0x20 || r == 0x7f: |
| 306 | return -1 |
| 307 | } |
| 308 | return r |
| 309 | }, s) |
| 310 | } |