Codebase list golang-github-vbauerster-mpb / 63215cd
Sort implementation Vladimir Bauer 9 years ago
2 changed file(s) with 77 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
3333 leftEnd byte
3434 rightEnd byte
3535
36 incrCh chan int
37 redrawRequestCh chan *redrawRequest
38 decoratorCh chan *decorator
39 flushedCh chan struct{}
40 stopCh chan struct{}
41 done chan struct{}
36 incrCh chan int
37 redrawReqCh chan chan []byte
38 progressReqCh chan chan int
39 decoratorCh chan *decorator
40 flushedCh chan struct{}
41 stopCh chan struct{}
42 done chan struct{}
4243
4344 timePerItemEstimate time.Duration
4445 }
4849 TimePerItemEstimate time.Duration
4950 }
5051
51 type redrawRequest struct {
52 bufCh chan []byte
53 }
52 // type redrawRequest struct {
53 // respCh chan []byte
54 // }
55
56 // type progressRequest struct {
57 // respCh chan int
58 // }
5459
5560 func newBar(total, width int, wg *sync.WaitGroup) *Bar {
5661 b := &Bar{
57 fill: '=',
58 empty: '-',
59 tip: '>',
60 leftEnd: '[',
61 rightEnd: ']',
62 alpha: 0.25,
63 total: total,
64 width: width,
65 incrCh: make(chan int),
66 redrawRequestCh: make(chan *redrawRequest),
67 decoratorCh: make(chan *decorator),
68 flushedCh: make(chan struct{}),
69 stopCh: make(chan struct{}),
70 done: make(chan struct{}),
62 fill: '=',
63 empty: '-',
64 tip: '>',
65 leftEnd: '[',
66 rightEnd: ']',
67 alpha: 0.25,
68 total: total,
69 width: width,
70 incrCh: make(chan int),
71 redrawReqCh: make(chan chan []byte),
72 progressReqCh: make(chan chan int),
73 decoratorCh: make(chan *decorator),
74 flushedCh: make(chan struct{}),
75 stopCh: make(chan struct{}),
76 done: make(chan struct{}),
7177 }
7278 go b.server(wg)
7379 return b
127133
128134 // String returns the string representation of the bar
129135 func (b *Bar) String() string {
130 bufCh := make(chan []byte)
131 b.redrawRequestCh <- &redrawRequest{bufCh}
132 return string(<-bufCh)
136 respCh := make(chan []byte)
137 b.redrawReqCh <- respCh
138 return string(<-respCh)
133139 }
134140
135141 func (b *Bar) Incr(n int) {
227233 case decoratorPrepend:
228234 prependFuncs = append(prependFuncs, d.f)
229235 }
230 case r := <-b.redrawRequestCh:
231 r.bufCh <- b.draw(buf, completed, appendFuncs, prependFuncs)
236 case respCh := <-b.redrawReqCh:
237 respCh <- b.draw(buf, completed, appendFuncs, prependFuncs)
238 case respCh := <-b.progressReqCh:
239 respCh <- int(100 * float64(completed) / float64(b.total))
232240 case <-b.flushedCh:
233241 if done && !b.IsCompleted() {
234242 // fmt.Fprintln(os.Stderr, "flushedCh: wg.Done")
286294 lastItemEstimate := float64(lastBlockTime) / float64(items)
287295 b.timePerItemEstimate = time.Duration((b.alpha * lastItemEstimate) + (1-b.alpha)*float64(b.timePerItemEstimate))
288296 }
297
298 func (b *Bar) progress() int {
299 respCh := make(chan int)
300 b.progressReqCh <- respCh
301 return <-respCh
302 }
303
304 type SortableBarSlice []*Bar
305
306 func (p SortableBarSlice) Len() int { return len(p) }
307
308 func (p SortableBarSlice) Less(i, j int) bool { return p[i].progress() < p[j].progress() }
309
310 func (p SortableBarSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
33 "fmt"
44 "io"
55 "os"
6 "sort"
67 "sync"
78 "time"
89
1213 type opType uint
1314
1415 const (
15 barAdd opType = iota
16 barRemove
16 opBarAdd opType = iota
17 opBarRemove
18 )
19
20 type SortType uint
21
22 const (
23 SortNone SortType = iota
24 SortTop
25 SortBottom
1726 )
1827
1928 const refreshRate = 60
2231 type Progress struct {
2332 out io.Writer
2433 width int
34 sort SortType
2535 stopped bool
2636
2737 op chan *operation
7383 return p
7484 }
7585
86 func (p *Progress) Sort(sort SortType) *Progress {
87 p.sort = sort
88 return p
89 }
90
7691 // AddBar creates a new progress bar and adds to the container
7792 func (p *Progress) AddBar(total int) *Bar {
7893 p.wg.Add(1)
7994 bar := newBar(total, p.width, p.wg)
80 p.op <- &operation{barAdd, bar, nil}
95 p.op <- &operation{opBarAdd, bar, nil}
8196 return bar
8297 }
8398
8499 func (p *Progress) RemoveBar(b *Bar) bool {
85100 result := make(chan bool)
86 p.op <- &operation{barRemove, b, result}
101 p.op <- &operation{opBarRemove, b, result}
87102 return <-result
88103 }
89104
114129 return
115130 }
116131 switch op.kind {
117 case barAdd:
132 case opBarAdd:
118133 bars = append(bars, op.bar)
119 case barRemove:
134 case opBarRemove:
120135 var ok bool
121136 for i, b := range bars {
122137 if b == op.bar {
129144 op.result <- ok
130145 }
131146 case <-t.C:
147 switch p.sort {
148 case SortTop:
149 sort.Sort(sort.Reverse(SortableBarSlice(bars)))
150 case SortBottom:
151 sort.Sort(SortableBarSlice(bars))
152 }
132153 for _, b := range bars {
133154 // cannot parallel this, because order matters
134155 fmt.Fprintln(lw, b)