-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtask.go
178 lines (154 loc) · 3.19 KB
/
task.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
// Copyright © 2022 Atonal Authors
//
package progressbar
import (
"io"
"sync"
"sync/atomic"
)
// NewTasks creates a Tasks container which you can add the tasks
// in it.
//
// tasks := progressbar.NewTasks(progressbar.New())
// defer tasks.Close()
//
// max := count
// _, h, _ := terminal.GetSize(int(os.Stdout.Fd()))
// if max >= h {
// max = h
// }
//
// for i := whichStepper; i < whichStepper+max; i++ {
// tasks.Add(
// progressbar.WithTaskAddBarOptions(
// progressbar.WithBarStepper(i),
// progressbar.WithBarUpperBound(100),
// progressbar.WithBarWidth(32),
// ),
// progressbar.WithTaskAddBarTitle("Task "+strconv.Itoa(i)), // fmt.Sprintf("Task %v", i)),
// progressbar.WithTaskAddOnTaskProgressing(func(bar progressbar.PB, exitCh <-chan struct{}) {
// for max, ix := bar.UpperBound(), int64(0); ix < max; ix++ {
// ms := time.Duration(10 + rand.Intn(300)) //nolint:gosec //just a demo
// time.Sleep(time.Millisecond * ms)
// bar.Step(1)
// }
// }),
// )
// }
//
// tasks.Wait()
//
// Above.
func NewTasks(bar MultiPB) *Tasks {
return &Tasks{bar: bar}
}
type Tasks struct {
bar MultiPB
tasks []*DownloadTask
wg sync.WaitGroup
}
type taskOptions struct {
onStart OnStart
onStop OnCompleted
onWork Worker
title string
barOptions []Opt
}
type TaskOpt func(s *taskOptions)
func WithTaskAddBarTitle(title string) TaskOpt {
return func(s *taskOptions) {
s.title = title
}
}
func WithTaskAddBarOptions(opts ...Opt) TaskOpt {
return func(s *taskOptions) {
s.barOptions = opts
}
}
func WithTaskAddOnTaskInitializing(cb OnStart) TaskOpt {
return func(s *taskOptions) {
s.onStart = cb
}
}
func WithTaskAddOnTaskCompleted(cb OnCompleted) TaskOpt {
return func(s *taskOptions) {
s.onStop = cb
}
}
func WithTaskAddOnTaskProgressing(cb Worker) TaskOpt {
return func(s *taskOptions) {
s.onWork = cb
}
}
func (s *Tasks) Close() {
s.bar.Close()
}
func (s *Tasks) Add(opts ...TaskOpt) *Tasks {
to := new(taskOptions)
for _, opt := range opts {
opt(to)
}
task := &sTask{
wg: &s.wg,
w: s.bar,
buf: nil,
doneCount: 0,
onStartProc: to.onStart,
onCompletedProc: to.onStop,
onStepProc: to.onWork,
}
var o []Opt
o = append(o,
WithBarWorker(task.onStep),
WithBarOnCompleted(task.onCompleted),
WithBarOnStart(task.onStart),
)
o = append(o, to.barOptions...)
s.bar.Add(
100,
to.title,
o...,
)
s.wg.Add(1)
return s
}
func (s *Tasks) Wait() {
s.wg.Wait()
}
type sTask struct {
w io.Writer
wg *sync.WaitGroup
onStartProc OnStart
onCompletedProc OnCompleted
onStepProc Worker
buf []byte
doneCount int32
}
func (s *sTask) Close() {
if wg := s.wg; wg != nil {
wg.Done()
}
}
// type Stepper interface {
// Step(delta int64)
// }
func (s *sTask) onStep(bar PB, exitCh <-chan struct{}) {
if s.onStepProc != nil {
s.onStepProc(bar, exitCh)
}
return
}
func (s *sTask) onStart(bar PB) {
if s.onStartProc != nil {
s.onStartProc(bar)
}
}
func (s *sTask) onCompleted(bar PB) {
if s.onCompletedProc != nil {
s.onCompletedProc(bar)
}
wg := s.wg
s.wg = nil
wg.Done()
atomic.AddInt32(&s.doneCount, 1)
}