gunk-dev/the-valley

dashboard_clear.go

source30e24c82839bmain's published tip history
3.0 KiB
1package cmd
2
3import (
4 "context"
5 "fmt"
6 "io"
7 "time"
8
9 tea "github.com/charmbracelet/bubbletea"
10 "the-valley/flow/internal/git"
11 "the-valley/flow/internal/run"
12)
13
14// dashClear is the clear the x key asks the operator to confirm: the runs
15// klaus cleanup --done would remove, judged by the dashboard's rows, and the
16// finished jobs it drops from the jobs section.
17type dashClear struct {
18 remove []string // run IDs
19 jobs int
20 prose int
21}
22
23// clearedMsg reports a clear's removals once they are done.
24type clearedMsg struct{ failed []string }
25
26// planClear picks what the x key clears, as klaus cleanup --done would:
27// every run of a finished PR or change and the older runs of an open one,
28// but never a running run, one that needs attention, or a change's or PR's
29// latest open run.
30func (m dashboardModel) planClear(now time.Time) *dashClear {
31 v := m.resolve(now)
32 running := make(map[*run.State]bool)
33 var groups []doneGroup
34 var loose []*run.State
35 for _, r := range v.all {
36 for _, s := range r.running {
37 running[s] = true
38 }
39 if r.prNum == "" && r.changeID == "" && r.finished == "" {
40 loose = append(loose, r.runs...)
41 continue
42 }
43 groups = append(groups, doneGroup{label: r.key, finished: r.finished, runs: r.runs})
44 }
45 handedOff := supersededRuns(m.states, func(s *run.State) bool { return running[s] })
46 c := &dashClear{}
47 for _, d := range planDone(groups, loose,
48 func(s *run.State) bool { return running[s] },
49 func(s *run.State) bool { return needsAttention(s) && !handedOff[s.ID] || s.SharedGitChanged() }) {
50 if d.remove {
51 c.remove = append(c.remove, d.state.ID)
52 }
53 }
54 listed, prose := m.recentJobView(now)
55 for _, j := range listed {
56 if j.Finished() {
57 c.jobs++
58 }
59 }
60 c.prose = prose.total
61 return c
62}
63
64// prompts returns the confirmation line in full and compact variants so the
65// y/n controls stay visible on narrow panes.
66func (c *dashClear) prompts() []string {
67 if c.prose > 0 {
68 noun := "prose passes"
69 if c.prose == 1 {
70 noun = "prose pass"
71 }
72 return []string{
73 fmt.Sprintf(" clear finished: remove %d runs and hide %d finished jobs, %d %s? y/n", len(c.remove), c.jobs, c.prose, noun),
74 fmt.Sprintf(" clear: %d runs, %d jobs, %d prose? y/n", len(c.remove), c.jobs, c.prose),
75 }
76 }
77 return []string{
78 fmt.Sprintf(" clear finished: remove %d runs and hide %d finished jobs? y/n", len(c.remove), c.jobs),
79 fmt.Sprintf(" clear: %d runs, %d jobs? y/n", len(c.remove), c.jobs),
80 }
81}
82
83// clearCmd removes the clear's runs as klaus cleanup does, leaving each
84// one's spend in the session's ledger.
85func (m dashboardModel) clearCmd(c *dashClear) tea.Cmd {
86 store, tc := m.store, m.tmux
87 return func() tea.Msg {
88 ctx := context.Background()
89 root, _ := git.RepoRoot()
90 deps := DefaultCleanupDeps(tc)
91 var failed []string
92 for _, id := range c.remove {
93 if err := cleanupOneTo(io.Discard, ctx, root, store, git.NewExecClient(), id, false, deps, tc); err != nil {
94 failed = append(failed, id)
95 }
96 }
97 return clearedMsg{failed: failed}
98 }
99}