-
Notifications
You must be signed in to change notification settings - Fork 2
/
bar.go
124 lines (103 loc) · 2.74 KB
/
bar.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
package main
import (
"fmt"
"time"
"github.com/DeathKing/pico"
"github.com/mattn/go-runewidth"
"github.com/vbauerster/mpb/v7"
"github.com/vbauerster/mpb/v7/decor"
)
// Marquee is useful when displaying long text
func Marquee(textGetter func() string, ws uint, wcc ...decor.WC) decor.Decorator {
var count uint
f := func(s decor.Statistics) string {
text := textGetter()
runes := []rune(text)
msg := string(runes[int(count)%len(runes):])
count++
return runewidth.FillRight(
runewidth.Truncate(msg, int(ws), ""),
int(ws))
}
return decor.Any(f, wcc...)
}
func Bar(task interface{}) *mpb.Progress {
switch t := task.(type) {
case *pico.SingleTask:
return singleTaskBar(t)
case *pico.BatchTask:
return batchTaskBar(t)
default:
panic("unknown task type")
}
}
var _txtDone = "\x1b[32mDone!\x1b[0m"
var _txtAbort = "\x1b[31mAborted\x1b[0m"
func singleTaskBar(t *pico.SingleTask) *mpb.Progress {
p := mpb.New()
for id, convertor := range t.Convertors {
worker := fmt.Sprintf("Worker#%02d:", id)
c := convertor
status := Marquee(func() string {
return c.Filename()
}, 30)
status = decor.OnComplete(status, _txtDone)
status = decor.OnAbort(status, _txtAbort)
bar := p.AddBar(0,
mpb.PrependDecorators(
decor.Name(worker, decor.WC{W: len(worker) + 1, C: decor.DidentRight}),
status,
decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
),
mpb.AppendDecorators(decor.Percentage(decor.WC{W: 5})),
)
go complete(bar, convertor)
}
return p
}
func batchTaskBar(t *pico.BatchTask) *mpb.Progress {
p := mpb.New()
// total file count
name := "Total file"
bar := p.AddBar(0,
mpb.PrependDecorators(
decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),
decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
),
mpb.AppendDecorators(decor.Percentage(decor.WC{W: 5})),
)
go complete(bar, t)
for id, convertor := range t.Convertors {
worker := fmt.Sprintf("Worker#%02d:", id)
c := convertor
status := Marquee(func() string {
return c.Filename()
}, 30)
status = decor.OnComplete(status, _txtDone)
status = decor.OnAbort(status, _txtAbort)
bar := p.AddBar(int64(convertor.Total()),
mpb.PrependDecorators(
decor.Name(worker, decor.WC{W: len(worker) + 1, C: decor.DidentRight}),
status,
decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
),
mpb.AppendDecorators(decor.Percentage(decor.WC{W: 5})),
)
go complete(bar, convertor)
}
return p
}
func complete(bar *mpb.Bar, o pico.Observable) {
for !bar.Completed() {
time.Sleep(time.Duration(500))
switch {
case o.Aborted():
bar.Abort(false)
case o.Completed():
bar.SetTotal(int64(o.Total()), true)
default:
bar.SetTotal(int64(o.Total()), false)
bar.SetCurrent(int64(o.Finished()))
}
}
}