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