-
Notifications
You must be signed in to change notification settings - Fork 63
/
paraprogress.go
254 lines (212 loc) · 5.72 KB
/
paraprogress.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// 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 except in compliance with the License.
package paraprogress
import (
"context"
"fmt"
"os"
"time"
"github.com/LastPossum/kamino"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"golang.org/x/term"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
"kraftkit.sh/tui"
)
var tprog *tea.Program
type ParaProgress struct {
processes []*Process
quitting bool
width int
timerWidth int
parallel bool
ctx context.Context
norender bool
maxConcurrent int
curr int
err error
errChan chan error
failFast bool
nameWidth int
timeout time.Duration
oldOut iostreams.FileWriter
}
func NewParaProgress(ctx context.Context, processes []*Process, opts ...ParaProgressOption) (*ParaProgress, error) {
if len(processes) == 0 {
return nil, fmt.Errorf("no processes to perform")
}
md := &ParaProgress{
processes: processes,
errChan: make(chan error),
curr: 0,
ctx: ctx,
// -1 represents the default mode where ecah individual
// process's name's width is checked and the maximum of all
// is used.
nameWidth: -1,
oldOut: iostreams.G(ctx).Out,
}
for _, opt := range opts {
if err := opt(md); err != nil {
return nil, err
}
}
maxNameLen := md.nameWidth
if maxNameLen <= 0 {
for _, process := range processes {
if nameLen := len(process.Name); nameLen > maxNameLen {
maxNameLen = nameLen
}
}
}
for _, process := range processes {
process.norender = md.norender
process.NameWidth = maxNameLen
process.timeout = md.timeout
if md.norender {
process.ctx = ctx
} else {
pctx := ctx
logger, err := kamino.Clone(log.G(pctx),
kamino.WithZeroUnexported(),
)
if err != nil {
return nil, err
}
// Update formatter when using KraftKit's TextFormatter. The
// TextFormatter recognises that this is a non-standard terminal and
// changes the output to a more machine readable format. Instead we want
// to force the formatting so that the output looks seamless with the
// style of the TUI.
if formatter, ok := logger.Formatter.(*log.TextFormatter); ok {
formatter.ForceColors = termenv.DefaultOutput().ColorProfile() != termenv.Ascii
formatter.ForceFormatting = true
logger.Formatter = formatter
}
logger.Out = process
pctx = log.WithLogger(pctx, logger)
ios, err := kamino.Clone(iostreams.G(pctx),
kamino.WithZeroUnexported(),
)
if err != nil {
return nil, err
}
ios.Out = iostreams.NewNoTTYWriter(process, iostreams.G(ctx).Out.Fd())
ios.ErrOut = process
ios.In = iostreams.G(ctx).In
pctx = iostreams.WithIOStreams(pctx, ios)
process.ctx = pctx
}
}
return md, nil
}
func (pd *ParaProgress) Start() error {
teaOpts := []tea.ProgramOption{
tea.WithInput(nil),
tea.WithContext(pd.ctx),
}
if pd.norender {
teaOpts = append(teaOpts, tea.WithoutRenderer())
} else {
// Set this super early (even before bubbletea), as fast exiting processes
// may not have received the window size update and therefore pd.width is
// set to zero.
pd.width, pd.maxConcurrent, _ = term.GetSize(int(os.Stdout.Fd()))
}
tprog = tea.NewProgram(pd, teaOpts...)
// Restore the old output for the IOStreams which is manipulated per process.
defer func() {
iostreams.G(pd.ctx).Out = pd.oldOut
log.G(pd.ctx).Out = pd.oldOut
}()
if _, err := tprog.Run(); err != nil {
return err
}
return pd.err
}
func (md ParaProgress) Init() tea.Cmd {
var cmds []tea.Cmd
for _, process := range md.processes {
cmds = append(cmds, process.Init())
if md.parallel {
cmds = append(cmds, process.Start())
}
}
// Start the first download if not in parallel mode
if !md.parallel {
cmds = append(cmds, md.processes[0].Start())
}
return tea.Batch(cmds...)
}
func (md *ParaProgress) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
// tea.KeyMsg is sent whenever there is a keyboard interrupt
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
md.quitting = true
md.err = fmt.Errorf("force quit")
return md, tea.Quit
}
case StatusMsg:
if msg.err != nil {
md.err = msg.err
}
if msg.err != nil && md.failFast {
md.quitting = true
cmds = append(cmds, tea.Quit)
} else if (!md.failFast || msg.status == StatusSuccess) && !md.parallel {
md.curr++
if len(md.processes) > md.curr {
cmds = append(cmds, md.processes[md.curr].Start())
}
}
}
var cmd tea.Cmd
complete := 0
for i := range md.processes {
md.processes[i], cmd = md.processes[i].Update(msg)
cmds = append(cmds, cmd)
if md.processes[i].timerWidth > md.timerWidth {
md.timerWidth = md.processes[i].timerWidth
}
if md.processes[i].Status == StatusFailed ||
md.processes[i].Status == StatusSuccess ||
md.processes[i].percent == 1 {
complete++
}
}
// Update each process to have the same width
for i := range md.processes {
md.processes[i].timerMax = md.timerWidth
}
if complete == len(md.processes) {
md.quitting = true
batch := tea.Batch(cmds...)
if batch == nil {
return md, tea.Quit
}
return md, tea.Sequence(batch, tea.Quit)
}
return md, tea.Batch(cmds...)
}
func (md ParaProgress) View() string {
if md.norender {
return ""
}
var content []string
for _, process := range md.processes {
content = append(content, process.View())
}
if md.quitting {
content = append(content, "")
} else {
content = append(content, tui.TextLightGray("ctrl+c to cancel"))
}
return lipgloss.JoinVertical(lipgloss.Left, content...)
}