-
Notifications
You must be signed in to change notification settings - Fork 63
/
update.go
106 lines (86 loc) · 2.53 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file expect in compliance with the License.
package processtree
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
)
func (pt *ProcessTree) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
// Update the global timer
pt.timer, cmd = pt.timer.Update(msg)
cmds = append(cmds, cmd)
// Update timers on all active items and their parents
_ = pt.traverseTreeAndCall(pt.tree, func(pti *ProcessTreeItem) error {
if pti.status == StatusRunning ||
pti.status == StatusRunningChild ||
pti.status == StatusRunningButAChildHasFailed {
pti.timer, cmd = pti.timer.Update(msg)
cmds = append(cmds, cmd)
}
return nil
})
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
pt.quitting = true
pt.err = fmt.Errorf("force quit")
return pt, tea.Quit
}
case spinner.TickMsg:
_ = pt.traverseTreeAndCall(pt.tree, func(pti *ProcessTreeItem) error {
if pti.timeout != 0 && pti.timer.Elapsed() > pti.timeout {
pti.err = fmt.Errorf("process timedout after %s", pti.timeout.String())
pti.status = StatusFailed
} else {
pti.spinner, cmd = pti.spinner.Update(msg)
}
if pti.status == StatusRunning ||
pti.status == StatusRunningChild ||
pti.status == StatusRunningButAChildHasFailed {
pti.ellipsis = strings.Repeat(".", int(pti.timer.Elapsed().Seconds())%4)
} else {
pti.ellipsis = ""
}
cmds = append(cmds, cmd)
return nil
})
return pt, tea.Batch(cmds...)
case processExitMsg:
cmds = append(cmds, msg.timer.Stop())
if msg.status == StatusSuccess ||
msg.status == StatusFailed ||
msg.status == StatusFailedChild {
pt.finished++
}
// No more processes then exit
if pt.total == pt.finished {
pt.quitting = true
cmds = append(cmds, tea.Quit)
} else {
_ = pt.traverseTreeAndCall(pt.tree, func(pti *ProcessTreeItem) error {
if !pti.timer.Running() {
cmds = append(cmds, pti.timer.Init())
}
return nil
})
children := pt.getNextReadyChildren(pt.tree)
for _, pti := range children {
pti := pti
cmds = append(cmds, pt.waitForProcessCmd(pti))
}
cmds = append(cmds, waitForProcessExit(pt.channel))
}
return pt, tea.Batch(cmds...)
case tea.WindowSizeMsg:
pt.width = msg.Width
return pt, nil
}
return pt, tea.Batch(cmds...)
}