-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.go
More file actions
78 lines (65 loc) · 1.87 KB
/
Copy pathprogress.go
File metadata and controls
78 lines (65 loc) · 1.87 KB
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
package progress
import (
"fmt"
"io"
"time"
"github.com/containerd/console"
"golang.org/x/time/rate"
)
type progressRenderer interface {
update(te *TaskEvent)
render(w io.Writer, width int, showError bool)
}
// Processes events from a channel and renders them to the console or trace. The
// mode can be "auto", "tty" or "plain". In "auto" mode, the console is used if
// available. In "tty" mode, the console is used and an error is returned if it
// is not available. In "plain" mode, the trace is used.
// When the events channel is closed, the last state is rendered and the
// function returns. The returned channel is closed when the rendering is
// complete.
// You'll most likely want to use [DisplayProgress] instead of this function.
func ProcessEvents(f console.File, name, mode string, events <-chan *TaskEvent) (<-chan struct{}, error) {
var renderer progressRenderer = newTraceRenderer(name)
var cons console.Console = noopConsole{}
switch mode {
case "auto", "tty":
if c, err := console.ConsoleFromFile(f); err == nil {
cons = c
renderer = newConsoleRenderer(name)
} else if mode == "tty" {
return nil, fmt.Errorf("failed to open console: %s", err)
}
case "plain":
default:
return nil, fmt.Errorf("unknown mode %q", mode)
}
tickRate := 150 * time.Millisecond
rateLimit := 100 * time.Millisecond
t := time.NewTicker(tickRate)
r := rate.NewLimiter(rate.Every(rateLimit), 1)
doneChan := make(chan struct{})
go func() {
for done := false; !done; {
select {
case <-t.C:
case e, ok := <-events:
if !ok {
done = true
} else {
renderer.update(e)
}
}
if done || r.Allow() {
size, err := cons.Size()
if err != nil {
size = console.WinSize{Width: 80}
}
renderer.render(f, int(size.Width), done)
t.Stop()
t = time.NewTicker(tickRate)
}
}
close(doneChan)
}()
return doneChan, nil
}