gunk-dev/the-valley

dashboard_clear.go

source88a5a486b52fmain's published tip history
2.4 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}
21
22// clearedMsg reports a clear's removals once they are done.
23type clearedMsg struct{ failed []string }
24
25// planClear picks what the x key clears, as klaus cleanup --done would:
26// every run of a finished PR or change and the older runs of an open one,
27// but never a running run, one that needs attention, or a change's or PR's
28// latest open run.
29func (m dashboardModel) planClear(now time.Time) *dashClear {
30 v := m.resolve(now)
31 running := make(map[*run.State]bool)
32 var groups []doneGroup
33 var loose []*run.State
34 for _, r := range v.all {
35 for _, s := range r.running {
36 running[s] = true
37 }
38 if r.prNum == "" && r.changeID == "" && r.finished == "" {
39 loose = append(loose, r.runs...)
40 continue
41 }
42 groups = append(groups, doneGroup{label: r.key, finished: r.finished, runs: r.runs})
43 }
44 handedOff := supersededRuns(m.states, func(s *run.State) bool { return running[s] })
45 c := &dashClear{}
46 for _, d := range planDone(groups, loose,
47 func(s *run.State) bool { return running[s] },
48 func(s *run.State) bool { return needsAttention(s) && !handedOff[s.ID] || s.SharedGitChanged() }) {
49 if d.remove {
50 c.remove = append(c.remove, d.state.ID)
51 }
52 }
53 for _, j := range m.recentJobList(now) {
54 if j.Finished() {
55 c.jobs++
56 }
57 }
58 return c
59}
60
61// prompt is the one line that asks to confirm the clear.
62func (c *dashClear) prompt() string {
63 return fmt.Sprintf(" clear finished: remove %d runs and hide %d finished jobs? y/n", len(c.remove), c.jobs)
64}
65
66// clearCmd removes the clear's runs as klaus cleanup does, leaving each
67// one's spend in the session's ledger.
68func (m dashboardModel) clearCmd(c *dashClear) tea.Cmd {
69 store, tc := m.store, m.tmux
70 return func() tea.Msg {
71 ctx := context.Background()
72 root, _ := git.RepoRoot()
73 deps := DefaultCleanupDeps(tc)
74 var failed []string
75 for _, id := range c.remove {
76 if err := cleanupOneTo(io.Discard, ctx, root, store, git.NewExecClient(), id, false, deps, tc); err != nil {
77 failed = append(failed, id)
78 }
79 }
80 return clearedMsg{failed: failed}
81 }
82}